Docker choreographer DockerCompose, which allows you to start a set of services at once with a single command.

For example, a command starts the SpringBoot project and other middleware (MySQL, Redis, etc.) that the SpringBoot project depends on.

Series welcome to visit: www.itwxe.com/posts/9e76d.

1. Install DockerCompose

1. Download Docker Compose

The curl -l https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose- ` ` uname - s - ` uname -m ` > / usr /local/bin/docker-compose
Copy the code

Grant executable permission to the docker-compose file.

chmod +x /usr/local/bin/docker-compose
Copy the code

3. Check whether the installation is successful.

docker-compose --version
Copy the code

For example, DockerCompose

1. Use the DockerCompose step

  • useDockerfileWhen creating a custom image upload server, you need to modify the initial image behavior.
  • usedocker-compose.ymlThe file defines the application services that need to be deployed so that they can run together in an isolated environment so that the script can be deployed once.
  • The last executiondocker-compose upCommand to create and start all services.

Docker-compose. Yml

version

Describe the version information about the Compose file. For details, see the official documentation.

# Specify version 3 syntax
version: '3'
Copy the code

services

Under the “services” node are defined services, and under the “services” is the container. The container and the container can be accessed using the service name as the domain name.

image

Specify the name of the image to run.

# specify mysql5.7 as the image
image: Mysql: 5.7
Copy the code

container_name

Specify the container name.

# Container name
container_name: admin_mysql
Copy the code

ports

Specifies the port mapping between the host and the container.

# Map mysql port
ports:
  - 3306: 3306
Copy the code

volumes

Mount files or directories from the host to the container.

Mount the mysql directory to the container
volumes:
  - /itwxe/dockerData/mysql/data:/var/lib/mysql
  - /itwxe/dockerData/mysql/conf:/etc/mysql
  - /itwxe/dockerData/mysql/log:/var/log/mysql
Copy the code

environment

Configure environment variables.

# Set password for mysql
environment:
  - MYSQL_ROOT_PASSWORD=root
Copy the code

links

Services that connect to other containers.

Mysql > select * from 'db' where 'db' = 'db'
links:
  - db:admin_mysql
Copy the code

3. Docker Compose

Start all relevant containers

docker-compose up -d
Copy the code

Specify file startup

docker-compose -f docker-compose.yml up -d
Copy the code

Stop all associated containers

docker-compose stop
Copy the code

List all container information

docker-compose ps
Copy the code

Stop and delete all containers (note data mounts)

docker-compose down
Copy the code

SpringBoot example

Here is an example of a project I wrote. My project relies on MySQL, Redis, and minio(not necessary).

1, package SpringBoot project image, package can refer to Docker using Maven plug-in to package SpringBoot project.

2. The startup of my project depends on sunny library in MySQL. First, we need to create a container and mount the host directory.

3. The project relies on Redis image custom configuration to start. You can refer to Docker to build Redis5.0 and mount data.

4, modify the project MySQL and Redis to use aliases, MySQL as an example.

# change before
JDBC: mysql: / / 192.168.5.33:3306 / sunny? useUnicode=true&characterEncoding=utf8mb4&serverTimezone=GMT%2B8

# modified
jdbc:mysql://admin_mysql:3306/sunny? useUnicode=true&characterEncoding=utf8mb4&serverTimezone=GMT%2B8
Copy the code

Edit docker-compose. Yml file.

version: '3'
services:
  MySQL > select mysql.service from mysql.service
  mysql:
    Specifies the image used by the service
    image: Mysql: 5.7
    Specifies the container name
    container_name: admin_mysql
    Specifies the port on which the service is running
    ports:
      - 3306: 3306
    # Specifies the file to be mounted in the container
    volumes:
      - /itwxe/dockerData/mysql/data:/var/lib/mysql
      - /itwxe/dockerData/mysql/conf:/etc/mysql
      - /itwxe/dockerData/mysql/log:/var/log/mysql
    # Specifies the container environment variable
    environment:
      - MYSQL_ROOT_PASSWORD=123456
  Specifies the name of the Redis service
  redis:
    Use custom image (specify profile)
    image: Itwxe/redis: 5.0.8
    container_name: admin_redis
    ports:
      - 6379: 6379
    volumes:
      - /itwxe/dockerData/redis/data:/data
  Specifies the name of the project service
  sunny-admin:
    Specifies the image used by the service
    image: Itwxe/sunny - admin: 0.0.1
    Specifies the container name
    container_name: sunny-admin
    Specifies the port on which the service is running
    ports:
      - 9002: 9002
    # Specifies the file to be mounted in the container
    volumes:
      - /itwxe/dockerData/sunny-admin/logs:/var/logs
    Yml can be accessed by an alias. For example:
    links:
      - mysql:admin_mysql
      - redis:admin_redis
Copy the code

6. Go to the docker-compose. Yml file directory and start the service.

docker-compose up -d
Copy the code

You can see that the service is started normally, at this time you can access the project, the project is normal access.

Now that you’ve read this, like, comment, follow, or collect it!

Author: IT wang2 xiao3 er4 starting address: www.itwxe.com/posts/e4feb… Copyright notice: the content of the article is subject to the authorship-non-commercial-no deduction 4.0 international license, except for special notice, all original, reprint please give the author and the original link in the obvious position of the article page.