README.md 6.28 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2
# docker-compose

duanledexianxianxian's avatar
duanledexianxianxian committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225


## 服务配置参考

  Compose文件是一个定义服务,网络和卷的YAML文件.Compose文件的默认路径是 `./docker-compose.yml`.  

> 提示:您可以对此文件使用`.yml`或`.yaml`扩展名。他们都工作.

服务定义包含将应用于为该服务启动的每个容器的配置,就像传递命令行参数一样 `docker run`。同样,网络和卷的定义类似于 `docker network create``docker volume create`

正如`docker run`在Dockerfile指定选项(例如,`CMD``EXPOSE``VOLUME``ENV`)是默认的配置-你不需要再次指定它们docker-compose.yml。

您可以使用类似Bash的`${VARIABLE}`语法在配置值中使用环境变量 - 有关详细信息,请参阅

变量替换for full details.

本节包含版本3中服务定义支持的所有配置选项的列表。

### build

Configuration options that are applied at build time.

`build` can be specified either as a string containing a path to the build context:

```
version: "3.7"
services:
  webapp:
    build: ./dir
```

Or, as an object with the path specified under [context](https://docs.docker.com/compose/compose-file/#context) and optionally [Dockerfile](https://docs.docker.com/compose/compose-file/#dockerfile) and [args](https://docs.docker.com/compose/compose-file/#args):

```
version: "3.7"
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
      args:
        buildno: 1
```

If you specify `image` as well as `build`, then Compose names the built image with the `webapp` and optional `tag` specified in `image`:

```
build: ./dir
image: webapp:tag
```

This results in an image named `webapp` and tagged `tag`, built from `./dir`.

> **Note**: This option is ignored when [deploying a stack in swarm mode](https://docs.docker.com/engine/reference/commandline/stack_deploy/) with a (version 3) Compose file. The `docker stack` command accepts only pre-built images.

#### CONTEXT

Either a path to a directory containing a Dockerfile, or a url to a git repository.

When the value supplied is a relative path, it is interpreted as relative to the location of the Compose file. This directory is also the build context that is sent to the Docker daemon.

Compose builds and tags it with a generated name, and uses that image thereafter.

```
build:
  context: ./dir
```

#### DOCKERFILE

Alternate Dockerfile.

Compose uses an alternate file to build with. A build path must also be specified.

```
build:
  context: .
  dockerfile: Dockerfile-alternate
```

#### ARGS

Add build arguments, which are environment variables accessible only during the build process.

First, specify the arguments in your Dockerfile:

```Dockerfile
ARG buildno
ARG gitcommithash

RUN echo "Build number: $buildno"
RUN echo "Based on commit: $gitcommithash"
```

Then specify the arguments under the `build` key. You can pass a mapping or a list:

```
build:
  context: .
  args:
    buildno: 1
    gitcommithash: cdc3b19
build:
  context: .
  args:
    - buildno=1
    - gitcommithash=cdc3b19
```

> **Note**: In your Dockerfile, if you specify `ARG` before the `FROM` instruction, `ARG` is not available in the build instructions under `FROM`. If you need an argument to be available in both places, also specify it under the `FROM` instruction. See [Understand how ARGS and FROM interact](https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact) for usage details.

You can omit the value when specifying a build argument, in which case its value at build time is the value in the environment where Compose is running.

```
args:
  - buildno
  - gitcommithash
```

> **Note**: YAML boolean values (`true`, `false`, `yes`, `no`, `on`, `off`) must be enclosed in quotes, so that the parser interprets them as strings.

#### CACHE_FROM

> **Note**: This option is new in v3.2

A list of images that the engine uses for cache resolution.

```
build:
  context: .
  cache_from:
    - alpine:latest
    - corp/web_app:3.14
```

#### LABELS

> **Note**: This option is new in v3.3

Add metadata to the resulting image using [Docker labels](https://docs.docker.com/engine/userguide/labels-custom-metadata/). You can use either an array or a dictionary.

We recommend that you use reverse-DNS notation to prevent your labels from conflicting with those used by other software.

```
build:
  context: .
  labels:
    com.example.description: "Accounting webapp"
    com.example.department: "Finance"
    com.example.label-with-empty-value: ""
build:
  context: .
  labels:
    - "com.example.description=Accounting webapp"
    - "com.example.department=Finance"
    - "com.example.label-with-empty-value"
```

#### SHM_SIZE

> Added in [version 3.5](https://docs.docker.com/compose/compose-file/compose-versioning/#version-35) file format

Set the size of the `/dev/shm` partition for this build’s containers. Specify as an integer value representing the number of bytes or as a string expressing a [byte value](https://docs.docker.com/compose/compose-file/#specifying-byte-values).

```
build:
  context: .
  shm_size: '2gb'
build:
  context: .
  shm_size: 10000000
```

#### TARGET

> Added in [version 3.4](https://docs.docker.com/compose/compose-file/compose-versioning/#version-34) file format

Build the specified stage as defined inside the `Dockerfile`. See the [multi-stage build docs](https://docs.docker.com/engine/userguide/eng-image/multistage-build/) for details.

```
build:
  context: .
  target: prod
```

### 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
```

> **Note**: These options are ignored when [deploying a stack in swarm mode](https://docs.docker.com/engine/reference/commandline/stack_deploy/) with a (version 3) Compose file.

### cgroup_parent

Specify an optional parent cgroup for the container.

```
cgroup_parent: m-executor-abcd
```

> **Note**: This option is ignored when [deploying a stack in swarm mode](https://docs.docker.com/engine/reference/commandline/stack_deploy/) with a (version 3) Compose file.

### command

Override the default command.

```
command: bundle exec thin -p 3000
```

The command can also be a list, in a manner similar to [dockerfile](https://docs.docker.com/engine/reference/builder/#cmd):

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