首先介绍几个术语
可见,一个项目可以由多个服务(容器)关联而成,Compose 面向项目进行管理。
👉 官方文档在这里
我才用 curl
方式为所有用户安装。(也可以用 apt-get
方式,具体参考文档)
# 下载 Docker Compose 的当前稳定版本(安装给所有服务器用户:/usr/local/bin)
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
# 查看是否安装成功
docker-compose -h
docker-compose --version # docker-compose version 1.29.2, build 5becea4c
# 对二进制文件应用可执行权限
chmod +x /usr/local/bin/docker-compose
通过 curl
安装 Compose CLI 插件,使用下面命令卸载
rm /usr/local/bin/docker-compose
docker-compose up # 启动
docker-compose up -d # 后台启动
# 停止应用程序,方法是docker-compose down 在第二个终端的项目目录中运行,或者在启动应用程序的原始终端中按 CTRL+C。
docker-compose down / CTRL + C
docker-compose stop
# 使用以下down 命令关闭所有内容,完全删除容器。传递--volumes给 Redis 容器使用的数据卷
docker-compose down --volumes
示例配置在这里
说明:
# 默认命名规则
# 默认的服务名: 文件名_服务名_num
# _num 代表副本,多个服务器,集群,高可用,高并发,负载均衡
composetest_redis_1
composetest_web_1
docker service ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
通过docker-compose启动的项目(包含多个服务),都在同一个网络下,可以通过服务名字访问
docker-compose
网络[root@--- ~]# 👉👉docker network ls👈👈
NETWORK ID NAME DRIVER SCOPE
8d10d0e8db5e bridge bridge local
3fa1e11e2050 composetest_default bridge local # docker-compose生成的
e8bf4fced9e2 host host local
0096a971fd2c mynet bridge local
6263db0933b9 none null local
0350d84612be redis bridge local
799426d70aa2 test-network bridge local
c143092e025f todo-app bridge local
[root@--- ~]# 👉👉docker network inspect 3fa1e11e2050👈👈
[
{
"Name": "composetest_default",
"Id": "3fa1e11e2050b81eb69691487789dd545d6ce3c36e686035c0a49db54f86b2f7",
"Created": "2021-10-10T15:46:29.084459645+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.21.0.0/16",
"Gateway": "172.21.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"57876e08abc657e884e2f527d930e0ffc34d632928d38ae2e29efa03c91b1980": {
"Name": "composetest_redis_1",
"EndpointID": "b1803007038c048525602a20da0c2aac73f8a87741ce127e84f89084fd2b75f0",
"MacAddress": "02:42:ac:15:00:03",
"IPv4Address": "172.21.0.3/16",
"IPv6Address": ""
},
"994a3c48416ace662a09b1179a695ee63c9a3c33fccefbb0886374260fcd6aab": {
"Name": "composetest_web_1",
"EndpointID": "cd7e479a22bd251c87214e27d033af3a1bf4164664457e9ceb8cf32f690cded7",
"MacAddress": "02:42:ac:15:00:02",
"IPv4Address": "172.21.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "composetest",
"com.docker.compose.version": "1.29.2"
}
}
]
[root@--- ~]#
可以通过 docker inspect containerName 查看容器信息 启停容器并不会拉取最新镜像
docker-compose up -d
或者
docker-compose up -d 服务名
本篇完🎉🎉🎉,后面接着分享 docker-compose 1分钟搭建WordPress
,有兴趣的话,麻烦看官点个赞,支持一下,谢谢啦😊