Docker Compose is the official Docker open source project, responsible for the implementation of Docker container cluster rapid choreography. Use a docker-comemess. yml template file to define one or more containers for a software application environment. Docker Compose has three usage scenarios
- Dockerfile file builds the container and customizes the image
- Docker-comemage. yml builds the software environment
- Docker-compose starts the container
The installation
On Windows and Macs, only docker Compose installation will be bundled with docker Compose Installation for Linux users using the following command
Sudo curl - L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname - s) - $(uname -m)" - o /usr/local/bin/docker-composeCopy the code
Executable permissions on binary files
sudo chmod +x /usr/local/bin/docker-compose
Copy the code
Check whether the installation is successful
$docker-compose version docker-compose version 1.29.1, build c34c88B2 docker-py version: 5.0.0 CPython version: 3.9.0 OpenSSL Version: OpenSSL 1.1g 21 Apr 2020Copy the code
Write the docker – compose. Yml
version: "3.9"
Each service has its own name, image to use, data volume to mount, network to which it belongs, which other services it depends on, and so on.
services:
# service name
webapp:
# docker runs in the name, the name seen in docker ps-a
container_name: mynginx
# mirror: tag
image: nginx:syf
Port mapping host port: container port
ports:
- "80:80"
Mount the local path to the container
volumes:
- D:/docker-compose/html:/usr/share/nginx/html
dns:
- 192.168123.1.
Copy the code
Compose has multiple versions that support 2.x and 3.x, and the corresponding docker versions are provided below
Compose file format | Docker Engine release |
---|---|
Compose specification | 19.03.0 + |
3.8 | 19.03.0 |
3.7 | 18.06.0 + |
3.6 | 18.02.0 + |
3.5 | 17.12.0 + |
3.4 | 17.09.0 + |
3.3 | 17.06.0 + |
3.2 | 17.04.0 + |
3.1 | 1.13.1 + |
3.0 | 1.13.0 + |
2.4 | 17.12.0 + |
2.3 | 17.06.0 + |
2.2 | 1.13.0 + |
2.1 | 1.12.0 + |
2.0 | 1.10.0 + |
The docker-comemess. yml file can also be used to add many configurations
Docker – compose command
docker-compose [-f …] [–profile …] [options] [–] [COMMAND] [ARGS…]
Start the container
docker-compose -f docker-compose.yml up -d
Docker-compose often uses commands
- Build Builds or reconstructs the service
- The create to create the service
- Down Stops and deletes container resources
- Exec Executes the command type docker exec command in the container
- Logs View container logs, similar to Docker logs
- Restart Restart container
- Start Start the service.
- Up creates and starts the container