Deployment of SpringBoot applications using Docker Compose

SpringBoot e-commerce project mall (20k+star) address: https://github.com/macrozheng/mall

abstract

Docker Compose is a tool for defining and running multiple docker container applications. With Compose, you can configure your application services with YAML files, and then with a command, you can deploy all the services you configure.

install

Download Docker Compose:

curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

The permissions to modify this file are enforceable:

chmod +x /usr/local/bin/docker-compose

Check to see if the installation is successful:

docker-compose --version

Steps for using Docker Compose

  • Dockerfile is used to define the application environment, which is usually used only when the initial mirror behavior needs to be modified.
  • Use docker-compose.yml to define application services that need to be deployed in order to execute script deployment at one time.
  • Use the docker-compose up command to deploy all application services at one time.

Common commands of docker-compose.yml

image

Specify the image name of the run

# Running a mirror of MySQL 5.7
image: mysql:5.7

container_name

Configuration container name

# The container name is mysql
container_name: mysql

ports

Specify port mapping for host and container (HOST:CONTAINER)

# Mapping host 3306 port to container 3306 port
ports:
  - 3306:3306

volumes

Mount host files or directories into containers (HOST:CONTAINER)

# Mount external files into myql containers
volumes:
  - /mydata/mysql/log:/var/log/mysql
  - /mydata/mysql/data:/var/lib/mysql
  - /mydata/mysql/conf:/etc/mysql

environment

Configuring environment variables

# Setting the environment variable of mysqlroot account password
environment:
  - MYSQL_ROOT_PASSWORD=root

links

Service Connecting Other Containers (SERVICE:ALIAS)

# Containers that can access service name db with database as domain name
links:
  - db:database

Common commands of Docker Compose

Build, create, and start related containers:

# - d means running in the background
docker-compose up -d

Stop all relevant containers:

docker-compose stop

List all container information:

docker-compose ps

Deploying applications using Docker Compose

Write docker-compose.yml file

Docker Compose divides the managed containers into three layers: engineering, services and containers. In docker-compose.yml, all services are defined as a project, under which services are served and under services are containers. Containers and containers can be accessed directly by using the service name as the domain name. For example, in mall-tiny-docker-compose service, DB mysql://db:3306 can be accessed by jdbc:mysql://db:3306.

version: '3'
services:
  # Specify service name
  db:
    # Specify the mirror used by the service
    image: mysql:5.7
    # Specify container name
    container_name: mysql
    # Specify the port on which the service runs
    ports:
      - 3306:3306
    # Specify the files that need to be mounted in the container
    volumes:
      - /mydata/mysql/log:/var/log/mysql
      - /mydata/mysql/data:/var/lib/mysql
      - /mydata/mysql/conf:/etc/mysql
    # Specify container environment variables
    environment:
      - MYSQL_ROOT_PASSWORD=root
  # Specify service name
  mall-tiny-docker-compose:
    # Specify the mirror used by the service
    image: mall-tiny/mall-tiny-docker-compose:0.0.1-SNAPSHOT
    # Specify container name
    container_name: mall-tiny-docker-compose
    # Specify the port on which the service runs
    ports:
      - 8080:8080
    # Specify the files that need to be mounted in the container
    volumes:
      - /etc/localtime:/etc/localtime
      - /mydata/app/mall-tiny-docker-compose/logs:/var/logs

Note: If the mall-tiny-docker-compose service is unable to connect to mysql, you need to create a mall database in MySQL and import mall.sql scripts. Specific reference Using Docker file to build Docker images for SpringBook applications Run the mysql service and set up the section.

Building mall-tiny-docker-compose image using maven plug-in

Note: Constructing a problem can be referred to Using Maven plug-ins to build Docker images for SpringBook applications

Run the Docker Compose command to start all services

First, upload docker-compose.yml to the Linux server, and then run the following commands in the current directory:

docker-compose up -d

Access Interface Document Address http://192.168.3.101:8080/swagger-ui.html:

Project source address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-docker-compose

Public Number

mall project In the whole series of learning courses, we pay attention to the first time acquisition of the public number.

Keywords: Java Docker MySQL Database github

Added by creatives on Tue, 10 Sep 2019 06:12:52 +0300