Docker-Compose学习
介绍
熟悉Linux都知道,我们可以把很多相关的命令写成一个xxx.sh
文件,而且这些步骤也是相对固定的。
这样直接运行sh文件,就可以逐一执行很多相关的Docker命令。这种形式可以减少出错和解决复用问题。Docker很贴心的为我们准备了一个专门的工具docker-compose
,实现类似sh
文件的功能。让我们更加轻松的实现多Docker命令的操作。
你也可以把docker-compose
就是把很多Docker命令写入一个专属的文件docker-compose.yml
,然后执行这个文件,就可以直接启动我们想要的容器。docker-compose
也为我们提供了对应的操作命令。
docker-compose up
docker-compose stop
也就是说,操作docker-compose 会有两个大的部分需要操作:
- 第一部分是
docker-compose.yam
文件 - 输入命令执行构建容器
docker-compose.yaml介绍
yaml
文件里是对启动镜像相关设置的所有描述,下面就逐一讲解一下。
version: "3.8" #表示使用的docker-compose的版本(注意要与docker版本对应)
codehope ~ docker --version
Docker version 20.10.10, build b485636
docs.docker.com/compose/compose-fi...
version: "3.8"
services: # 容器
servicename: # 服务名字,这个名字也是内部 bridge网络可以使用的 DNS name
container_name: xxx
image: xxx # 镜像的名字
command: # 可选,如果设置,则会覆盖默认镜像里的 CMD命令
environment: # 可选,相当于 docker run里的 --env
volumes: # 可选,相当于docker run里的 -v
networks: # 可选,相当于 docker run里的 --network
ports: # 可选,相当于 docker run里的 -p
servicename: # 其他服务名字
...
volumes: # 可选,相当于 docker volume create
networks: # 可选,相当于 docker network create
For exapmle
version: '3.8'
services:
my-nginx: # 服务名字
container_name: cw_my_ngxin # 镜像的名字
image: nginx # 镜像的名字
ports:
- 80:80
初试-通过docker-compose启动nginx
docker-compose.yaml
version: '3.8'
services:
nginx:
image: nginx
ports:
- 80:80
codehope ~/CunWangOwn/docke-file-test/docker-compose-demo docker-compose up
Starting docker-compose-demo_nginx_1 ... done
Attaching to docker-compose-demo_nginx_1
nginx_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1 | 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: using the "epoll" event method
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: nginx/1.21.4
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: OS: Linux 5.10.47-linuxkit
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: start worker processes
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: start worker process 25
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: start worker process 26
nginx_1 | 2021/11/28 13:17:57 [notice] 1#1: start worker process 27
访问宿主机 80端口,也是完全ok的!
命名规则
用docker compose
创建的容器,名字都会加入一个对应文件夹的名字,比如我在的文件夹叫做docker-compose-demo
,而我在yaml
文件中镜像的名字是nginx
。
最终容器的名字就是docker-compose-demo_nginx_1
通过命令查看镜像和容器,拉取了一个nginx的镜像,并且以所在目录名称+镜像名称+序号的格式创建了容器
~/CunWangOwn/docke-file-test/docker-compose-demo docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
...
nginx latest ea335eea17ab 11 days ago 141MB
...
~/CunWangOwn/docke-file-test/docker-compose-demo docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
811924c49ac1 nginx "/docker-entrypoint.…" 5 minutes ago Exited (137) 15 seconds ago docker-compose-demo_nginx_1
这个前缀其实是可以改的,比如我们希望前缀加上CunWangBro
。就可以使用-p
参数。【名称会自动帮我们转小写的】
docker-compose -p CunWangBro up
Creating network "cunwangbro_default" with the default driver
Creating cunwangbro_nginx_1 ... done
Attaching to cunwangbro_nginx_1
查看container ls
~/CunWangOwn/docke-file-test/docker-compose-demo docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7a9cca41daa nginx "/docker-entrypoint.…" 44 seconds ago Exited (137) 3 seconds ago cunwangbro_nginx_1
你也可以在yaml
文件里指定这个名字,方法是使用contaner_name: xxx
但是这样作就会完全省略前缀和后缀。
version: '3.8'
services:
my-nginx:
container_name: cw_my_ngxin
image: nginx
ports:
- 80:80
起!docker-compose up
~/CunWangOwn/docke-file-test/docker-compose-demo docker-compose up
WARNING: Found orphan containers (docker-compose-demo_nginx_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating cw_my_ngxin ... done
Attaching to cw_my_ngxin
cw_my_ngxin | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
cw_my_ngxin | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
cw_my_ngxin | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
cw_my_ngxin | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
查看容器
docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff90b9191e44 nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) 9 seconds ago cw_my_ngxin
docker-compose
构建自己的镜像
version: '3.8'
services:
koa:
image: koa-test:1.0
ports:
- 8181:3000
这里的image: koa-test:1.0
,是我们自己构建的镜像,但是目前还没有。如果用docker compose up
构建会直接报错。我们运行一下,可以看到下面的错误。
~/CunWangOwn/docke-file-test docker-compose up
Pulling koa (koa-test:1.0)...
ERROR: The image for the service you're trying to recreate has been removed. If you continue, volume data could be lost. Consider backing up your data before continuing.
Continue with the new image? [yN]y
Pulling koa (koa-test:1.0)...
ERROR: pull access denied for koa-test, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
意思就是这个my-node
镜像在网上找不到,仓库里没有。
可以修改docker-compose.yml
文件,让docker先构建镜像,然后再启动容器。
修改后的docker-compose.yml
文件
文件目录结构
└── dockerfiledir
├── app.tar.gz
└── dockerfile
docker-compose.yaml
version: '3.8'
services:
cunwangkoa:
container_name: cw_koa_server
build: ./dockerfiledir
image: koa-test:1.0
ports:
- 8181:3000
dockerfile
FROM node:15.10.0-slim ADD ./app.tar.gz /app WORKDIR /app RUN npm install --registry=https://registry.npm.taobao.org CMD node app.js
然后起docker-compose
可以看到是先根据dockerfile
文件 build
的景象,然后再运行的服务,这时候就不会报错了,也可以正常启动容器了
codehope ~/CunWangOwn/docke-file-test docker-compose up
Building cunwangkoa
[+] Building 3.1s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 183B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/node:15.10.0-slim 3.0s
=> [auth] library/node:pull token for registry-1.docker.io 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 80B 0.0s
=> [1/4] FROM docker.io/library/node:15.10.0-slim@sha256:325e465ccc8a02ada4db47e940c93b2734014cbed036db0a53d8ecc68abf0fc6 0.0s
=> CACHED [2/4] ADD ./app.tar.gz /app 0.0s
=> CACHED [3/4] WORKDIR /app 0.0s
=> CACHED [4/4] RUN npm install --registry=https://registry.npm.taobao.org 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:58127741dd2bd1d9427e606a07defe78637b42fb1ac0d273399d1486297019fe 0.0s
=> => naming to docker.io/library/koa-test:1.0 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
WARNING: Image for service cunwangkoa was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating cw_koa_server ... done
Attaching to cw_koa_server
cw_koa_server | 村望老师的Docker测试服务器跑起来啦
查看镜像ls
~/CunWangOwn/docke-file-test docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
koa-test 1.0 58127741dd2b 2 hours ago 164MB
查看容器ls
~/CunWangOwn/docke-file-test docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ca15a2b8d7f koa-test:1.0 "docker-entrypoint.s…" About a minute ago Exited (137) 40 seconds ago cw_koa_server