Docker-Compose學習

村望老弟發表於2021-11-29

介紹

參考資料

熟悉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

image-20211128180033859

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的!

image-20211128211856164

命名規則

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
本作品採用《CC 協議》,轉載必須註明作者和本文連結
CunWang@Ch

相關文章