“This is the 25th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

thinking

When we use Docker, define Dockerfile file, then use Docker build, Docker run -d –name -p and other commands to operate the container. However, the application system of microservice architecture generally contains several microservices, and each microservice will generally deploy multiple instances. If each microservice has to be started and stopped manually, the low efficiency and large maintenance amount can be imagined. Containers can be easily and efficiently managed using Docker Compose, an application tool for defining and running multi-container Dockers

describe

The Compose project is Docker’s official open source project, which is responsible for the rapid choreography of Docker container clusters. Functionally, it is very similar to Heat in OpenStack. The code is currently available at github.com/docker/comp… On open source. Its predecessor was Fig, an open source project.

Compose allows users to define a set of associated application containers as a project from a single docker-comemage. yml template file (YAML format). Effectively solve the problem of multiple containers working together to complete a task.

Compose has two important concepts:

  • Service (service) : An application container can actually contain several instances of containers running the same image.
  • Project (project) : A complete business unit consisting of a set of associated application containers, indocker-compose.ymlFile defined.

Docker Compose installation and uninstallation

(Compose supports Linux, macOS, and Windows 10.)

The installation

#1 Github official website search Docker compose

#2 Find the downloaded binary file according to the following dynamic picture example

Github.com/docker/comp…

#2.1 Drag the downloaded file into Linux and cut it to mv docker-comement-linux-x86_64 /usr/localCopy the code
Docker-compose chmod 777 docker-compose mv docker-compose x86_64 docker-compose /usr/local/bin/Copy the code
#3 Run the following command to download the current stable version of Docker Compose:

sudo curl -L 

"https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copy the code
# 4 Apply executable permissions to binary files:
sudo chmod +x /usr/local/bin/docker-compose
Copy the code
#5 Test the installation$docker-compose --version docker-compose version 1.25.5, build 1110ad01Copy the code

uninstall

If the installation is binary, delete the binary file
sudo rm /usr/local/bin/docker-compose

If PIP is installed, run the following command to delete it:
sudo pip uninstall docker-compose
Copy the code

Docker Compose using

Compose to manage a Tomcat container and MySQL

1. Manage folders and create corresponding directories

mkdir -p /opt/docker_mysql_tomcat/

Docker-comemess. yml: docker-comemess. yml

Compose the docker-comemage. yml file, which is the main template file for Compose.

version: '1.0'
services: 
  mysql:                                        # service name
    restart: always                             Whenever docker is started, the container will start with itImage: daocloud. IO/library/mysql: 5.7.6# set path information (default official mirror address)
    container_name: mysql-3306                   # specify container name --name
    ports:
      - 3306:3306                              # specify port number mapping
    environment: 
      MYSQL_ROOT_PASSWORD: root                 MYSQL ROOT password
      TZ: Asiz/Shanghai                         # specify time zone
    volumes:
      - /opt/docker_mysql_tomcat/mysql/data:/var/lib/mysql # map mysql data directory to host, save data
      - /opt/docker_mysql_tomcat/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
      Map the mysql configuration file to the appropriate directory of the containerTomcat: restart: always image: daocloud. IO/library/tomcat: 8.5.15 - jre8 container_name: tomcat - 8080 ports: - 8080:8080 environment: TZ: Asiz/Shanghai volumes: - /opt/docker_mysql_tomcat/tomcat/webapps:/usr/local/tomcat/webapps
      - /opt/docker_mysql_tomcat/tomcat/logs:/usr/local/tomcat/logs
Copy the code

Added: Description in YML format

1. Yml files are represented by indentation

2, indent not TAB, only space

3. The number of Spaces is not important, as long as elements of the same level are left aligned (2 are recommended)

4. Case sensitivity

5. Data format: name :(space) value

6, support object, array, basic data type three data formats such as:

V: represents a pair of key-value pairs (Spaces cannot be omitted). The hierarchy relationship is controlled by Spaces. As long as left-aligned data is at the same level; Server: port: 8080 path: /hello # array: animal: -cat-dagCopy the code

3. Start the management container

docker-compose up -d

Docker-compose common command

Build Builds or rebuilds the service

Help command help

Kill Kill the container

Logs displays the output of the container

Port Indicates the bound open port

Ps display container

Pull Pulls the service image

Restart Restart the service.

Rm deleted the stopped container

Run runs a one-time command

Scale sets the number of containers for the service

Start Start the service.

Stop Stop the service

Up creates and starts the container