As you can see from the previous section, the commands to run containers are relatively simple. However, when there are complex applications, such as setting environment variables, mounting directories, and mapping ports, the commands become longer. If multiple Docker images are configured and used at the same time, executing docker Run alone will display more trouble. Therefore, this article introduces a simple tool for multi-container orchestration: Docker-compose.

The installation

Installation method is as follows:

sudo apt-get install -y docker-compose
Copy the code

The installed version of this article is as follows:

$docker-compose --version docker-compose version 1.21.2, build A133471Copy the code

Specified installation mode:

Sudo curl -l https://github.com/docker/compose/releases/download/1.27.4/docker-compose- ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-composeCopy the code

Note: Glibc_2.28 cannot be found on CentOS 7. The solution is solved when the version is rolled back.

Docker – compose. Yml content

The default docker-compose configuration file is docker-comemage. yml. The content of docker-compose is shown in the following example. Most Of the English names are fixed content, while the Chinese description is based on the actual situation.

Gitlab: # select * from gitlab: # select * from gitlab; Latelee /gitlab # Name of the image, if not present, download it from the network. /dev/ SHM/env_file env_file env_file env_file env_file env_file env_file If this option is enabled, the container will be automatically started after the machine restarts. Otherwise, the container will be started manually. Run the following command to add volumes to the docker run command: -tz =Asia/Shanghai ports: / MNT /gitlab_data/config:/etc/gitlab environment: - mygitlab-Jenkins -net depends_on: "80/80" networks: # mygitlab-Jenkins -net depends_on: -Foo Jenkins: # foo Jenkins: # foo Jenkins: # foo Jenkins: # foo Jenkins: # foo Jenkins: # foo Jenkins: Mygitlab-jenkins-net: # user-defined network name driver: bridge #Copy the code

In my experience, yML files are very strict to check the format and must be aligned. For example, image, container_name, and so on, must be aligned. There can be no more or less space. Otherwise, an unintelligible error will be displayed.

Run, stop

In the docker-comemess. yml directory, run the following command:

docker-compose up -d
Copy the code

If the configuration file is not docker-comemage.yml, it needs to be specified using -f, as follows:

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

In the above command, -d indicates that the docker container is run in the background. After the command is executed, the terminal only prints the startup information and does not print the logs in the container. To display logs, remove -d.

docker-compose up
Copy the code

Stop command:

docker-compose down
Copy the code

Restart command:

docker-compose restart
Copy the code

Note that if you want to run multiple containers at the same time and modify some of them, you can use restart to restart only the modified containers, but as a rule of thumb, unexpected situations sometimes occur, so it is best to stop and start first. To view the running status, run:

docker-compose ps
Copy the code

Of course, docker PS can also be used to view, essentially no difference.

Practical example

Docker-comemage.yml: Docker-comemage.yml: Docker-comemage.yml: docker-comemage.yml: docker-comemage.yml

# gitlab & jenkins# Powered by Late Lee <[email protected]># gitlab gitlab: Image: latelee/gitlab; gitlab restart: gitlab restart: Always # Mount volumes: - /mnt/gitlab_data/config:/etc/gitlab - /mnt/gitlab_data/logs:/var/log/gitlab - /mnt/gitlab_data/data:/var/opt/gitlab # Port mapping ports: - "80:80" - "9443:443" - "2222:22" # hostname change this IP address to your own host IP hostname: "172.18.18.18" #command: /assets/wrapper # Custom net (see below) networks: -mygitlab-jenkins -net latelee/jenkins:maven container_name: jenkins restart: always volumes: # using host docker services (because must run in Jenkins container docker) - / var/run/docker. The sock: / var/run/docker. The sock # using data disk directory as the working directory - Jenkins / MNT /jenkins_home:/var/jenkins_home ports: # Port mapping - "9080:8080" - "500:50000" Networks: -mygitlab-jenkins -net# network configuration network: mygitlab-jenkins-net: # user-defined network name driver: bridge #Copy the code

Execute docker-compose up -D to start the Gitlab and Jenkins containers. Once successful, you can access gitLab’s page by entering the host IP in your browser. Note that Gitlab and Jenkins are more memory intensive, so it is best to run on a machine with more than 4GB of memory, otherwise it will be very slow.

Experience with

Build the image in docker-comemage.yml.

In docker-compose. Yml file, add build field in front of image, specify the directory where Dockerfile is located, so when docker-compose up is run, it will be built automatically. The following is an example file:

version: "2"services:  apache:    build: httpd    image: httpd:alpine    container_name: httpd    volumes:    #   - /home:/home       - ./apache2/htdocs:/usr/local/apache2/htdocs    ports:      - 80:80
Copy the code

After the command is executed, the following information is displayed:

Successfully httpd:alpineWARNING: Image for service singula-oh-accessserver was built because it did not already exist. To rebuild this image you must use  `docker-compose build` or `docker-compose up --build`.Copy the code

The docker-compose build or docker-compose up –build is called if the image does not exist

summary

In my work, I almost always use Docker-compose to start containers. This, combined with your own Docker image (described below), is a huge efficiency boost. Such as deploying the Zen Path system. Using the configuration file I built, download it directly from github.com/latelee/doc… , go to the directory and run docker-compose up -d to access the Zen Path page on port 9070. This process is very convenient because all the environment is already done in Docker. Docker-compose repository address: github.com/latelee/doc… .

The attached

Some container startup commands:

mysql:command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--explicit_defaults_for_timestamp=false']redis: #command: redis-serverCommand: redis-server --requirepass 123456Copy the code