Service configuration reference🔗
The Compose file is a YAML file defining services, networks and volumes. The default path for a Compose file is ./docker-compose.yml
.
Tip: You can use either a
.yml
or .yaml
extension for this file. They both work.
A service definition contains configuration that is applied to each container started for that service, much like passing command-line parameters to docker run
. Likewise, network and volume definitions are analogous to docker network create
and docker volume create
.
As with docker run
, options specified in the Dockerfile, such as CMD
, EXPOSE
, VOLUME
, ENV
, are respected by default - you don’t need to specify them again in docker-compose.yml
.
You can use environment variables in configuration values with a Bash-like ${VARIABLE}
syntax - see variable substitution for full details.
This section contains a list of all configuration options supported by a service definition in version 3.
image
在 services 标签下的第二级标签是 web,这个名字是用户自己自定义,它就是服务名称。image 则是指定服务的镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。
例如下面这些格式都是可以的:
image: busybox是默认拉取busybox:latest ,如果要指定版本image: busybox:5.1
build
服务除了可以基于指定的镜像,还可以基于一份 Dockerfile,在使用 up 启动之时执行构建任务,这个构建标签就是 build,它可以指定 Dockerfile 所在文件夹的路径。Compose 将会利用它自动构建这个镜像,然后使用这个镜像启动服务容器。
设定上下文根目录,然后以该目录为准指定 Dockerfile。
注意 build 都是一个目录,如果你要指定 Dockerfile 文件需要在 build 标签的子级标签中使用 dockerfile 标签指定,如上面的例子。
如果你同时指定了 image 和 build 两个标签,那么 Compose 会构建镜像并且把镜像命名为 image 后面的那个名字。
既然可以在 docker-compose.yml 中定义构建任务,那么一定少不了 arg 这个标签,就像 Dockerfile 中的 ARG 指令,它可以在构建过程中指定环境变量,但是在构建成功后取消,在 docker-compose.yml 文件中也支持这样的写法:
下面这种写法也是支持的,一般来说下面的写法更适合阅读。
与 ENV 不同的是,ARG 是允许空值的。例如:
这样构建过程可以向它们赋值。
注意:YAML 的布尔值(true, false, yes, no, on, off)必须要使用引号引起来(单引号、双引号均可),否则会当成字符串解析。
container_name
前面说过 Compose 的容器名称格式是:<项目名称><服务名称><序号>
虽然可以自定义项目名称、服务名称,但是如果你想完全控制容器的命名,可以使用这个标签指定:
这样容器的名字就指定为 app 了。
hostname
定义容器的主机名
depends_on
Added in version 2.0 file format.
在使用 Compose 时,最大的好处就是少打启动命令,但是一般项目容器启动的顺序是有要求的,如果直接从上到下启动容器,必然会因为容器依赖问题而启动失败。例如在没启动数据库容器的时候启动了应用容器,这时候应用容器会因为找不到数据库而退出,为了避免这种情况我们需要加入一个标签,就是 depends_on,这个标签解决了容器的依赖、启动先后的问题。
例如下面容器会先启动 redis 和 db 两个服务,最后才启动 web 服务:
Express dependency between services. Service dependencies cause the following behaviors:
-
docker-compose up
starts services in dependency order. In the following example,db
andredis
are started beforeweb
. -
docker-compose up SERVICE
automatically includesSERVICE
’s dependencies. In the example below,docker-compose up web
also creates and startsdb
andredis
. -
docker-compose stop
stops services in dependency order. In the following example,web
is stopped beforedb
andredis
.
Simple example:
注意的是,默认情况下使用 docker-compose up web 这样的方式启动 web 服务时,也会启动 redis 和 db 两个服务,因为在配置文件中定义了依赖关系。
Note:
depends_on
does not wait for db
and redis
to be “ready” before starting web
- only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.
links
还记得上面的depends_on吧,那个标签解决的是启动顺序问题,这个标签解决的是容器连接问题,与Docker client的--link一样效果,会连接到其它服务中的容器。格式如下:
使用的别名将会自动在服务容器中的/etc/hosts里创建。例如:
相应的环境变量也将被创建。
dns
和 --dns 参数一样用途,格式如下:
也可以是一个列表:
此外 dns_search 的配置也类似:
labels
向容器添加元数据,和Dockerfile的LABEL指令一个意思,格式如下: