Docker Compose is an official open source project of Docker, responsible for the rapid orchestration of Docker container clusters. By using a docker-compose.yml
template file to define one or more containers to meet a software application operating environment.
Docker Compose has three usage scenarios
- Dockerfile file to build the container, customize the image
- docker-compose.yml build software environment
- docker-compose starts the container
installation
In window and mac, only installing docker will bundle Docker Compose to install
Linux users use the following command to install
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Executable permissions on binary files
sudo chmod +x /usr/local/bin/docker-compose
Check if 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.1.1g 21 Apr 2020
Write docker-compose.yml
version: "3.9"
# 服务 在它下面可以定义应用需要的一些服务,每个服务都有自己的名字、使用的镜像、挂载的数据卷、所属的网络、依赖哪些其他服务等等。
services:
#服务名称
webapp:
# docker 运行在名字,在docker ps -a 看到的名字
container_name: mynginx
# 镜像 : 标签
image: nginx:syf
# 端口映射 主机端口: 容器端口
ports:
- "80:80"
# 物理卷挂载 将本地路径挂载到容器内
volumes:
- D:/docker-compose/html:/usr/share/nginx/html
dns:
- 192.168.123.1
Compose has multiple versions that support 2.x and 3.x, the version provided below corresponds to the docker version
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+ |
Many configurations can be added to the docker-compose.yml file. If you are interested, see 160d7410521fc0 official website document
docker-compose command
docker-compose [-f <arg>...] [--profile <name>...] [options] [--] [COMMAND] [ARGS...]
Start the container
docker-compose -f docker-compose.yml up -d
docker-compose often uses commands
- build build or refactor service
- create Create service
- down stop and delete container resources
- exec executes the command type docker exec command in the container
- logs View container logs, similar to docker logs
- restart restart the container
- start Start the service
- up creates and starts the container
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。