compose配置檔案引數詳解

wadeson發表於2017-11-10

 

本文介紹compose配置檔案引數的使用,熟練編寫compose檔案

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build: /root/docker_demo/nginx/
    ports:
      - "80:80"
    links:
      - php:php
    volumes:
      - "/www:/usr/local/nginx/html"
  php:
    image: php
    expose:
      - "9000"
    volumes:
      - "/www:/usr/local/nginx/html"

上面的一個例子version代表版本

什麼版本的docker-compose對應什麼版本的docker:

services就是compose中定義的服務,compose管理的是project、service、container,project如果沒有指定就是預設的lnmp.yml檔案的lnmp

nginx、php就是屬於services的子集欄,定義了兩個service

nginx服務中有各種引數:

build:和docker中的build構建一樣,後面的路徑指向Dockerfile的位置:

[root@docker lnmp]# ll /root/docker_demo/nginx/
total 832
-rw-r--r--. 1 root root   1133 Nov  6 13:40 Dockerfile
-rw-r--r--. 1 root root   1033 Nov  6 10:17 fastcgi_params
-rw-r--r--. 1 root root 833473 Nov  6 09:35 nginx-1.8.1.tar.gz
-rw-r--r--. 1 root root    776 Nov  6 10:16 nginx.conf
-rw-r--r--. 1 root root    341 Nov  6 16:43 www.conf

ports引數就是docker在docker run啟動容器時-p選項的意思差不多,對映到本機host,前者的埠是本機host,後面是container_port

links引數也是docker run在啟動容器時--link選項的意思差不多,用於與容器間的互聯訪問,php:php,前者php是定義的service名稱,後者的php是給它定義了一個別名

volumes:與docker run在啟動容器時-v選項的意思差不多,用於host與container之間的對映掛載,前者是host_mount_dir,後面是container中的目錄

volumes:
  # Just specify a path and let the Engine create a volume
  - /var/lib/mysql

  # Specify an absolute path mapping
  - /opt/data:/var/lib/mysql

  # Path on the host, relative to the Compose file
  - ./cache:/tmp/cache

  # User-relative path
  - ~/configs:/etc/configs/:ro

  # Named volume
  - datavolume:/var/lib/mysql

在下面的php這個service中:

image:代表的就是使用的docker images中的某個映象名稱去開啟container(預設tag為latest),如果本地沒有映象將會進行pull 

image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd

有了上面的第一個簡單的compose yml檔案,於是進行啟動container:

[root@docker lnmp]# docker-compose -f lnmp.yaml up --build

由於我這裡進行了配置檔案的修改,所以不再單獨的up,而是up --build(If you change a service’s Dockerfile or the contents of its build directory, run docker-compose build to rebuild it.)

單獨的docker-compose -f lnmp.yaml build只會進行build而不會進行啟動容器,之後還需進行docker-compose -f lnmp.yaml up

 build: /root/docker_demo/nginx/,build引數這裡可以指定解決路徑也可以以compose的專案路徑的相對路徑進行指定

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build:
      context: /root/docker_demo/nginx/
      dockerfile: /root/docker_demo/nginx/Dockerfile

build引數的子集也可以根據Dockerfile構建的context來進行build

在build子集中還有args的使用,配合Dockerfile中的ARG結合使用

[root@docker nginx]# cat Dockerfile 
FROM centos_init:v2

MAINTAINER json_hc@163.com
ARG buildno
RUN echo "Build number: $buildno"

上面的Dockerfile中定義了build,但是並沒有賦值,RUN執行列印這個值,而在compose的yml中:

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build:
      context: /root/docker_demo/nginx/
      dockerfile: /root/docker_demo/nginx/Dockerfile
      args:
        buildno: 2

定義了buildno的值,於是在docker-compose執行構建時使用了這個值,下面是過程

如果僅僅在yml檔案中定義了args變數,而沒有在Dockerfile中定義相同的變數,將會報錯:

[Warning] One or more build-args [foo] were not consumed.

ARG在Dockerfile中的用法:

ARG <name>[=<default value>]
設定變數命令,ARG命令定義了一個變數,在docker build建立映象的時候,使用 --build-arg <varname>=<value>來指定引數
ARG user1
ARG buildno
或者定義預設值
ARG user1=someuser
ARG buildno=1

ARG只是在build構建過程中使用,構建完成後,變數將會消失和ENV有著明顯的區別

而在compose的yml檔案中args的定義:

build:
  context: .
  args:
    buildno: 1
    password: secret

build:
  context: .
  args:
    - buildno=1
    - password=secret

上面兩種方式都可以

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build:
      context: /root/docker_demo/nginx/
      dockerfile: /root/docker_demo/nginx/Dockerfile
      args:
        - buildno=2

command引數,覆蓋容器內預設的命令

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build:
      context: /root/docker_demo/nginx/
      dockerfile: /root/docker_demo/nginx/Dockerfile
      args:
        - buildno=2
    command: ["/usr/local/nginx/sbin/nginx"]

這裡定義command的命令,而Dockerfile中的nginx的啟動命令:

CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

所以在[root@docker lnmp]# docker-compose -f lnmp.yaml up --build

構建並啟動時並不會成功:

可以明顯的看到command定義的命令替換掉了Dockerfile中的CMD命令

 

container_name:自定義容器名

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build:
      context: /root/docker_demo/nginx/
      dockerfile: /root/docker_demo/nginx/Dockerfile
      args:
        - buildno=2
    container_name: lnmp-nginx

可以看見nginx服務的容器名已經改為lnmp-nginx了

 

depends_on:定義服務間的依賴關係

比如資料庫db,必須在web啟動之前進行啟動,也就是說web啟動依賴db的啟動

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build:
      context: /root/docker_demo/nginx/
      dockerfile: /root/docker_demo/nginx/Dockerfile
      args:
        - buildno=2
    container_name: lnmp-nginx
    depends_on:
      - db
    ports:
      - "80:80"
    links:
      - php:php
    volumes:
      - "/www:/usr/local/nginx/html"
  php:
    image: php
    expose:
      - "9000"
    volumes:
      - "/www:/usr/local/nginx/html"
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: redhat
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

可以看出nginx服務是最後啟動容器的,由於nginx依賴db,而php與nginx有link,所以nginx最後啟動

 

dns:設定dns

dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9

可以設定是一個單一值,也可以是一個列表

 

dns_search:自定義搜尋域:

dns_search: example.com
dns_search:
  - dc1.example.com
  - dc2.example.com

可以設定為單一值,也可以是一個列表

 

entrypoint:覆蓋Dockerfile中的entrypoint,用法同Dockerfile中的用法

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
  nginx:
    build:
      context: /root/docker_demo/nginx/
      dockerfile: /root/docker_demo/nginx/Dockerfile
      args:
        - buildno=2
    container_name: lnmp-nginx
    entrypoint: ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

[root@docker lnmp]# docker-compose -f lnmp.yaml ps  
   Name                 Command               State           Ports         
----------------------------------------------------------------------------
lnmp-nginx   /usr/local/nginx/sbin/ngin ...   Up      0.0.0.0:80->80/tcp    
lnmp_db_1    docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp
lnmp_php_1   /usr/local/php/sbin/php-fpm      Up      9000/tcp  

 

env_file:將定義的變數編寫在檔案中,然後在yml檔案中進行新增

env_file: .env

env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env

建立env_file檔案mysql_env.txt:

[root@docker lnmp]# ll
total 16
-rw-r--r--. 1 root root 477 Nov  9 17:00 docker-compose.yml.bak
-rw-r--r--. 1 root root 550 Nov 10 15:45 lnmp.yaml
-rw-r--r--. 1 root root 271 Nov 10 10:26 lnmp.yaml.bak
-rw-r--r--. 1 root root  98 Nov 10 15:46 mysql_env.txt

檢視定義的變數:

[root@docker lnmp]# cat mysql_env.txt 
MYSQL_ROOT_PASSWORD=redhat
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress

變數的定義格式為這樣

然後進行docker-compose:

[root@docker lnmp]# docker-compose -f lnmp.yaml up --build

進入到db服務中進行驗證變數是否建立成功:

[root@docker nginx]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml exec db sh
# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

可以看見MySQL的root密碼已經設定,也建立了資料庫wordpress等等

 

environment:新增環境變數,可以是一個字典也可以是陣列

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=redhat
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress

定義完成後進行構建並啟動後,進行驗證:

[root@docker nginx]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml exec db sh
# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

 

expose:暴露container的埠,但是並沒有對映到host主機上,用法類似於Dockerfile中的EXPOSE

  php:
    image: php
    expose:
      - "9000"
    volumes:
      - "/www:/usr/local/nginx/html"

 

external_links:

external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_1:postgresql

 連結到 docker-compose.yml 外部的容器,甚至 並非 Compose 管理的容器。引數格式跟 links 類似。

這個引數下篇文章有介紹

 

extra_hosts:新增hostname對映,類似於docker cli下面的--add-host

    extra_hosts:
      - "www.hcstart.com:192.168.101.14"

然後將這個compose file進行up:

[root@docker lnmp]# docker-compose -f lnmp.yml up --build
Creating network "lnmp_net1" with driver "bridge"
Creating lnmp-php ... 
Creating lnmp-php ... done
Creating lnmp-nginx ... 
Creating lnmp-nginx ... done
Attaching to lnmp-php, lnmp-nginx
[root@docker lnmp]# docker-compose -f lnmp.yml exec nginx cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.101.14  www.hcstart.com
172.19.0.3      35cc14084be9

上面的/etc/hosts就可以看見剛剛加入的www.hctsrat.com對映了

 

healthcheck:配置一個檢查去測試服務中的容器是否執行正常

[root@docker lnmp]# cat lnmp.yml|grep health -A 4
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 1m30s
      timeout: 10s
      retries: 3

檢視healthcheck的狀態輸出:

[root@docker lnmp]# docker inspect lnmp-nginx
            "Health": {
                "Status": "healthy",
                "FailingStreak": 0,
                "Log": [
                    {
                        "Start": "2017-10-27T03:52:37.921723965-04:00",
                        "End": "2017-10-27T03:52:38.021941235-04:00",
                        "ExitCode": 0,
                        "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0<h1>hello nginx and php</h1>\n\r100    29  100    29    0     0   5741      0 --:--:-- --:--:-- --:--:--  7250\n"
                    },

healthcheck中選項的詳解:https://docs.docker.com/engine/reference/builder/#healthcheck

 

labels:新增後設資料到container中,檢視現有容器的labels:

[root@docker lnmp]# docker inspect -f {{.Config.Labels}} lnmp-nginx
map[com.docker.compose.oneoff:False com.docker.compose.service:nginx com.docker.compose.version:1.17.0 license:GPLv2 vendor:CentOS build-date:20170911 com.docker.compose.config-hash:edaa6c6d8f12639e28cb4bcdffda4961ef5a90f076e9017b204dacda56525e5b com.docker.compose.container-number:1 com.docker.compose.project:lnmp name:CentOS Base Image]

 

logging:為服務配置記錄日誌

[root@docker lnmp]# cat lnmp.yml|grep logging -A 4
    logging:
      driver: "json-file"
      options:
        max-size: "2000k"
        max-file: "10"

logging支援很多driver,而每一個driver對應的options都不一樣:

[root@docker lnmp]# docker inspect -f {{.HostConfig.LogConfig}} lnmp-nginx
{json-file map[max-file:10 max-size:2000k]}
[root@docker lnmp]# docker info |grep 'Logging Driver'
Logging Driver: json-file

更多其他的driver檢視:https://docs.docker.com/engine/admin/logging/overview/

 

sysctls:在容器中設定核心引數

sysctls:
  net.core.somaxconn: 1024
  net.ipv4.tcp_syncookies: 0

sysctls:
  - net.core.somaxconn=1024
  - net.ipv4.tcp_syncookies=0

 

相關文章