转自:https://www.cnblogs.com/jsonhc/p/7814138.html
本文介绍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