Docker Compose文件详解 V2

Compose file reference

语法:

web:

build: ./web

ports:

- "5000:5000"

volumes:

- .:/code

links:

- redis

redis:

image:redis

build:可使用相对目录或绝对目录

ports:可使用端口范围

links:可使用别名,例如: -redis:web-redis

build

build: ./dir

# build后可直接接Dockerfile所在目录,该目录必须存在Dockerfile

build:

context: ./dir

dockerfile: Dockerfile-alternate

args:

buildno: 1

image: webapp:tag

# contest指定Dockerfile的目录。dockerfile指定Dockerfile文件名

# args:不清楚

# images:指定dockerfile构建出的镜像名,默认使用docker-compose.yml所在的文件夹名+service名

context

build:

context: ./dir

#指定Dockerfile所在的目录

dockerfile

build:

context: .

dockerfile: Dockerfile-alternate

build: .

dockerfile: Dockerfile-alternate

# 当Dockerfile文件名不是默认名称时,使用dockerfile参数指定Dockerfile的文件名

args

cap_add, cap_drop

Add or drop container capabilities. See man 7 capabilities for a full list.

cap_add:

- ALL

cap_drop:

- NET_ADMIN

- SYS_ADMIN

command

覆盖Dockerfile中的command

command: bundle exec thin -p 3000

# 类似于dockerfile中的命令:

command: [bundle, exec, thin, -p, 3000]

container_name

指定容器的名称

container_name: my-web-container

depends_on

表示服务之前的依赖关系,有两个效果:

  • docker-compose up,启动web服务之前、启动redis、db。
  • docker-compose up web,启动web容器时,检查依赖depends_on的配置内容,先启动db和redis

‘2‘

services:

build: .

depends_on:

- db

 
    - redis

redis:

image: redis

 db:
  

 
  image: postgres

dns

自定义DNS,可以是单个的,也可以是列表.

dns: 8.8.8.8

dns:

 
- 8.8.8.8

- 9.9.9.9

dns_search

Custom DNS search domains. Can be a single value or a list.

dns_search: example.com

dns_search:

 
- dc1.example.com

- dc2.example.com

tmpfs

在容器中挂载一个tmpfs。

# tmpfs可以理解成虚拟机磁盘,是创建在内存上,不是在硬盘上。读取速度快,重启后数据消失

run

tmpfs:

- /run

- /tmp

entrypoint

覆盖默认的entrypoint.

entrypoint: /code/entrypoint.sh

可以是个列表,类似dockerfile中的语法

entrypoint:

- php

- -d

- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so

- -d

- memory_limit=-1

- vendor/bin/phpunit

env_file

指定变量的文件,默认为docker-compose文件夹下的 .env 文件.

同一个变量,通过export设置,会覆盖env_file中的变量.

env_file: .env

env_file:

- ./common.env

- ./apps/web.env

env

environment

设置环境变量,支持数组和字典.

environment:

RACK_ENV: development

‘true‘

SESSION_SECRET:

environment:

- RACK_ENV=development

 - SHOW=true

 - SESSION_SECRET

expose

暴漏端口,并不会暴漏到宿主机上,而是提供给内部容器通信.

expose:

- "3000"

- "8000"

extends、

external_links

待补充

extra_hosts

添加主机映射. Use the same values as the docker client --add-host parameter.

extra_hosts:

- "somehost:162.242.195.82"

- "otherhost:50.31.209.229"

映射结果会体现在 /etc/hosts
:

162.242.195.82 
somehost

50.31.209.229   otherhost

image

指定基础镜像. 可以是镜像名称、镜像ID。

version1中不允许build和image同时出现,version2中允许,image代表指定build之后的镜像名称。

image: redis

image: ubuntu:14.04image: tutum/influxdb

image: example-registry.com:4000/postgresql

image: a4bc65fd

labels

指定容器的标签.

labels:

com.example.description: "Accounting webapp"

com.example.department: "Finance"

com.example.label-with-empty-value: ""

labels:

- "com.example.description=Accounting webapp"

- "com.example.department=Finance"

- "com.example.label-with-empty-value"

links

连接其他容器,可以设置别名,设置link之后,hostname与IP的对应关系会写入到 /etc/hosts中. 和depend_on类似,设置link后,也间接的规定了容器的启动顺序.

web:

links:

- db

- redis

logging

设置容器的日志驱动.version 2 通过logging替换log_driver和log_opt

options:

 
 syslog-address: "tcp://192.168.0.42:123"

net

Version 1 file format only. In version 2, use network_mode.

为容器指定网络类型,version 1专用,version 2使用network_mode.

net: "bridge"

net: "host"

net: "none"

net: "container:[service name or container name/id]"

network_mode

Version 2 file format only. In version 1, use net.

为容器指定网络类型.

network_mode: "bridge"

network_mode: "host"

network_mode: "none"

network_mode: "service:[service name]"

network_mode: "container:[container name/id]"

networks

Version 2 file format only. In version 1, use net.

Networks to join, referencing entries under the top-level networks key.

services:

some-service:

networks:

- some-network

- other-network

aliases

待补充

ipv4_address, ipv6_address

指定容器的IP地址等,用法待补充,未操作

pid

通过设置,容器可通过ps查询宿主机的进程.

pid: "host"

ports

暴漏容器的端口,可设置为8080:8080 或 :8080(宿主机端口随机).

Note:建议使用引号

ports:

- "3000"

- "3000-3005"

- "8000:8000"

- "9090-9091:8080-8081"

- "49100:22"

- "127.0.0.1:8001:8001"

- "127.0.0.1:5000-5010:5000-5010"

stop_signal

待补充

ulimits

设置ulimit值,可替换掉docker或dockerfile默认的值.

ulimits:

nproc: 65535

nofile:

soft: 20000

hard: 40000

volumes, volume_driver

挂载卷,如果不指定宿主机目录,会在volume默认目录创建挂载点。可单独挂载文件、可设置目录权限.  。

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

var/lib/mysql
   # datavolume是通过docker volume create --name datavolume创建

如果不使用宿主机存储,须臾奥指定
volume_driver
.例如rancher的convoy fs。

volume_driver: mydriver

volumes_from

从其他服务或容器挂载所有的volume, 可设置制度访问 (ro)或读写 (rw). 如果不设置,默认为读写模式。

volumes_from:

- service_name

- service_name:ro

- container:container_name

- container:container_name:rw

Note: The Container:... formats are only supported in the version 2 file format. In version 1, you can use container names without marking them as such:

- service_name

- service_name:ro

- container_name

- container_name:rw

cpu_shares, cpu_quota, cpuset, domainname, hostname, ipc,
mac_address, mem_limit, memswap_limit, privileged, read_only, restart,
shm_size, stdin_open, tty, user, working_dir

单独的值,类似于docker run的参数.

cpu_shares: 73

cpu_quota: 50000

cpuset: 0,1

user: postgresql

working_dir: /code

domainname: foo.com

hostname: foo

ipc: host

mac_address: 02:42:ac:11:65:43

mem_limit: 1000000000

memswap_limit: 2000000000

privileged: true

restart: always

read_only: true

shm_size: 64M

stdin_open: true

tty: true

Volume configuration reference

driver

设置volume的驱动,默认是local.

 driver: foobar

driver_opts

设置驱动的参数,不同的驱动所需要的参数不同.可选.

driver_opts:

foo: "bar"

baz: 1

external

如果设置为true,指定volume在compose的外部,docker-compose up 时不去创建该volume。如果不存在报错.

version: ‘2‘

services:

db:

image: postgres

volumes:

- data:/var/lib/postgresql/data

volumes:

data:

external: true

You can also specify the name of the volume separately from the name used to refer to it within the Compose file:

volumes:

data:

external:

name: actual-name-of-volume

A more extended example, defining volumes and networks:

version: ‘2‘

services:

web:

build: .

ports:

- "5000:5000"

volumes:

- .:/code

networks:

- front-tier

- back-tier

redis:

image: redis

volumes:

- redis-data:/var/lib/redis

networks:

- back-tier

volumes:

redis-data:

driver: local

networks:

front-tier:

driver: bridge

back-tier:

driver: bridge

version: ‘2‘

services:

db:

image: postgres

volumes:

- data:/var/lib/postgresql/data

volumes:

data: {}

时间: 2025-01-11 19:27:02

Docker Compose文件详解 V2的相关文章

Docker Compose 配置文件详解

Docker Compose 配置文件详解 左蓝关注 72017.01.19 23:25:09字数 2,827阅读 144,706 先来看一份 docker-compose.yml 文件,不用管这是干嘛的,只是有个格式方便后文解说: version: '2' services: web: image: dockercloud/hello-world ports: - 8080 networks: - front-tier - back-tier redis: image: redis links

Kubernetes之yaml文件详解-v1.15.x

Kubernetes之yaml文件详解 K8S 创建资源的方式 K8S有两种创建资源的方式:kubectl 命令和 yaml 配置文件. kubectl命令行:最为简单,一条命令就OK,但缺点也很明显,你并不知道这条命令背后到底做了哪些事!yaml配置文件:提供了一种让你知其然更知其所以然的方式.优势如下: 完整性:配置文件描述了一个资源的完整状态,可以很清楚地知道一个资源的创建背后究竟做了哪些事:灵活性:配置文件可以创建比命令行更复杂的结构:可维护性:配置文件提供了创建资源对象的模板,能够重复

Docker基础命令详解——镜像及容器操作

Docker基础命令详解--镜像及容器操作 前言 ? 上篇文章介绍了有关Docker的基础与Linux下docker的安装,本文主要讲解安装docker后的基础使用方法以及命令的介绍,主要是docker镜像操作及容器操作命令. ? 当然,docker的相关命令非常多,可以使用docker help命令查看对应目录以及相关提示命令. Docker镜像操作命令 [[email protected] ~]# which docker /usr/bin/docker 1.镜像搜索:docker sear

package-info.java文件详解

package-info.java文件详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.pacakge-info.java介绍 pacakge-info.java是一个Java文件,可以添加到任何的Java源码包中.pacakge-info.java的目标是提供一个包级的文档说明或者是包级的注释. pacakge-info.java文件中,唯一要求包含的内容是包的声明语句,比如: package com.ch.service; 二.包文档 在

史上最全的maven的pom.xml文件详解

史上最全的maven的pom.xml文件详解 http://www.cnblogs.com/hafiz/p/5360195.html <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 h

POM文件详解(1)

POM文件详解 <project xmlns=http://maven.apache.org/POM/4.0.0 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> 1      Parent坐标 <

T-SQL 操作文件 详解

/*******  导出到excel EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S"GNETDATA/GNETDATA" -U"sa" -P""' /***********  导入Excel SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 'Data Source="

delphi 资源文件详解

delphi资源文件详解 一.引子: 现在的Windows应用程序几乎都使用图标.图片.光标.声音等,我们称它们为资源(Resource).最简单的使用资源的办法是把这些资源的源文件打入软件包,以方便程序需要的时候调用.资源是程序的一部分,程序要正常运行就离不了资源文件.但是它是不可执行代码. 为了更好地管理资源,Delphi中提供了一种.RES类型的资源文件.它可以把我们程序中所需要的资源整合到一个资源文件(.RES)下来.在编译应用程序时直接编译进了可执行程序里,成为应用程序的整合体. 这样

hibernate 对象关系映射文件详解

POJO 类和数据库的映射文件*.hbm.xml POJO类和关系数据库之间的映射可以用一个XML文档来定义. 映射文件的扩展名为.hbm.xml 在运行时Hibernate将根据这个映射文件来生成各种SQL语句 通过POJO类的数据库映射文件,Hibernate可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据库表列之间的对应关系 映射文件说明 hibernate-mapping 类层次:class 主键:id 基本类型:property 实体引用类: many-to-one