简介
在之前运行一个容器必须使用一段命令如:docker run -itd -p 80:80 -v /var/data/:/var/data/ --volume-from cong1 --net congnet --name mynginx nginx,每次运行都需要写这么一段,有点烦,接下来我们使用Docker compose来解决这个问题, 其实run 后面的都算配置,而yaml文件是目前写配置文件的利器。
使用Docker compose,可以用一个YAML文件定义一组要启用的容器,以及容器运行的属性,Docker compose称这些容器为“服务”。
安装Docker Compose
下载最新的docker-compose,并安装到文件夹/usr/local/bin/docker-compose 下。
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
修改权限,让它变成得可执行
sudo chmod +x /usr/local/bin/docker-compose
检查版本,出现如下界面,说明安装成功了
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose -v
docker-compose version 1.23.1, build b02f1306
使用Docker Compose创建一个image,并运行
创建一个文件夹,像下面这样。
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# ls
docker-compose.yml Dockerfile index.html
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# cat Dockerfile
FROM nginx
WORKDIR /usr/share/nginx/html
COPY . .
#Volume /home/app
EXPOSE 80
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# cat docker-compose.yml
---
version: '3'
services:
web:
build: "/home/cong/DockerDemo/"
ports:
- "5000:80"
redis:
image: "redis"
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# cat index.html
<h1>Hello world</h1>
<h2>my greet</h2>
从上可以看出里面有两个服务:web 和 redis, 运行如下命令进行构建docker image.
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose build
redis uses an image, skipping
Building web
Step 1/4 : FROM nginx
---> dbfc48660aeb
Step 2/4 : WORKDIR /usr/share/nginx/html
---> Using cache
---> 49eb0195ae40
Step 3/4 : COPY . .
---> 45a1d6022ef6
Step 4/4 : EXPOSE 80
---> Running in e18fcf899aff
Removing intermediate container e18fcf899aff
---> 36f9a746361b
Successfully built 36f9a746361b
Successfully tagged dockerdemo_web:latest
这里由于redis服务中使用的是的redis镜像,这里显示为跳过,只生成一个镜像,这个镜像的名字是以你的当前文件夹名+服务名命名的,生成出来的镜像如下:
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerdemo_web latest 36f9a746361b 2 minutes ago 109MB
运行如下命令,运行容器,-d 表示detached在后台运行。
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose up -d
Starting dockerdemo_web_1_4b7b83909e42 ... done
Starting dockerdemo_redis_1_dfd3e5c242ff ... done
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da2cb8d44ee5 dockerdemo_web "nginx -g 'daemon of…" 21 seconds ago Up 3 seconds 0.0.0.0:5000->80/tcp dockerdemo_web_1_4b7b83909e42
a8b63ebd622d redis "docker-entrypoint.s…" 21 seconds ago Up 2 seconds 6379/tcp dockerdemo_redis_1_dfd3e5c242ff
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker network ls
NETWORK ID NAME DRIVER SCOPE
0a5324c3bd83 bridge bridge local
f04d01cfd231 dockerdemo_default bridge local
fa685cfac4f3 host host local
22f6181538a9 none null local
发现这两容器都运行起来了,而且它们都在网络dockerdemo_default中。
接下来我们修改docker-compose.yml文件,将当前路径下的文件挂载到容器中。yaml文件内容如下,然后再执行docker -compose up命令,它会rebuild image, 最终看到文件夹挂载成功了。
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# cat docker-compose.yml
---
version: '3'
services:
web:
volumes:
- .:/usr/share/nginx/html
build: "/home/cong/DockerDemo/"
ports:
- "5000:80"
redis:
image: "redis"
#修改当前路径里index.html文件的内容
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# echo "<h1>hello</h1>" > index.html
#再执行docker-compose up,它会自动更新镜像并运行容器
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose up -d
dockerdemo_redis_1_dfd3e5c242ff is up-to-date
Recreating dockerdemo_web_1_4b7b83909e42 ... done
#检查是否挂载成功
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# curl localhost:5000
<h1>hello</h1>
执行docker-compose ps 看看通过docker-compose 运行起来的容器
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------
dockerdemo_redis_1_dfd3e5c242ff docker-entrypoint.sh redis ... Up 6379/tcp
dockerdemo_web_1_4b7b83909e42 nginx -g daemon off; Up 0.0.0.0:5000->80/tcp
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0020488cb04 dockerdemo_web "nginx -g 'daemon of…" 17 hours ago Up 17 hours 0.0.0.0:5000->80/tcp dockerdemo_web_1_4b7b83909e42
a8b63ebd622d redis "docker-entrypoint.s…" 17 hours ago Up 17 hours 6379/tcp dockerdemo_redis_1_dfd3e5c242ff
78761e99c06f nginx "nginx -g 'daemon of…" 45 hours ago Up 45 hours 0.0.0.0:80->80/tcp mynginx
1b25aef5f610 redis "docker-entrypoint.s…" 8 days ago Up 8 days 0.0.0.0:6379->6379/tcp kind_curran
如果想要执行某个服务里命令,或者说执行某个容器里的命令呢,下面是查看web服务里的环境变量
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose run web env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=5435187b75a1
TERM=xterm
NGINX_VERSION=1.15.5-1~stretch
NJS_VERSION=1.15.5.0.2.4-1~stretch
HOME=/root
使用Docker Compose停止服务
直接看下面的命令
#停止服务
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose down
Stopping dockerdemo_web_1_4b7b83909e42 ... done
Stopping dockerdemo_redis_1_dfd3e5c242ff ... done
Removing dockerdemo_redis_run_1_636fb680637e ... done
Removing dockerdemo_web_run_1_1e57ca097b87 ... done
Removing dockerdemo_web_1_4b7b83909e42 ... done
Removing dockerdemo_redis_1_dfd3e5c242ff ... done
Removing network dockerdemo_default
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose ps
Name Command State Ports
------------------------------
#服务停止后,但镜像还在
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerdemo_web latest 36f9a746361b 17 hours ago 109MB
redis latest 1babb1dde7e1 3 weeks ago 94.9MB
node latest a2b9536415c2 4 weeks ago 674MB
nginx latest dbfc48660aeb 4 weeks ago 109MB
#服务停止后,但卷还在
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker volume ls
DRIVER VOLUME NAME
local 1ff8ca8ce28be5851d217134e90b74cb24c35035284ce93d83363085e37adac4
# 移除卷
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker-compose down --volumes
Removing network dockerdemo_default
WARNING: Network dockerdemo_default not found.
到这里就结束, 这里只演示了使用docker-compose最基本的用法,如深度学习,请参考如下链接。