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):