1.Docker-Compose Overview
The Compose project is Docker's official open source project, responsible for realizing the rapid orchestration of Docker container clusters.
Using the Dockerfile introduced earlier we can easily define a separate application container. However, in daily development work, we often encounter situations where multiple containers need to cooperate with each other to complete a certain task.
For example, to implement a Web project, in addition to the Web service container itself, it is often necessary to add a back-end database service container; for another example, distributed applications generally include several services, and each service generally deploys multiple instances. If each service has to be started and stopped manually, the efficiency will be low and the amount of maintenance will be heavy.
At this time, a tool is needed that can manage a set of associated application containers, which is Docker Compose.
Compose has two important concepts:
- Project: A complete business unit consisting of a set of associated application containers, defined in the docker-compose.yml file.
- Service: An application container can actually include several container instances running the same image.
All yml files in the docker compose running directory form a project. A project contains multiple services. Each service defines the image, parameters, and dependencies of the container running. A service can include multiple container instances. docker-compose is an orchestration tool for docker containers, which mainly solves the management of multiple containers that are dependent on each other.
2. Install docker-compose
There are two ways to install docker-compose. You can choose one of them:
2.1 Install through docker-compose file
First download the latest version of docker-compose file.
Official document address: Install Docker Compose | Docker Documentation
Find your corresponding system version, download and install it
2.2 Install through pip command
Mine is a MAC system. If pip is not installed, you can go here: Mac system: zsh: command not found: pip solution
Enter:
pip install docker-compose
3.Docker-compose actual combat
Take installing the Mysql image as an example:
First create the mysql-compose.yml file:
version: "3"
services:
mysql:
image: mysql:5.7.19
restart: always
container_name: mysql
ports:
- 3306:3306
volumes:
- /data/software/mysql/conf/:/etc/mysql/conf.d
- /data/software/mysql/data:/var/lib/mysql
- /data/software/mysql/log/:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: kangll
MYSQL_USER: kangll
MYSQL_PASSWORD: 123456
MySQL container startup and shutdown
# 关闭
docker-compose -f docker-compose-mysql.yml down
# 启动
docker-compose -f docker-compose-mysql.yml up -d