Understand target in Docker Compose
The short answer
In Docker Compose, the target property in the build section of a Compose file is used to select a specific stage from a multi-stage Dockerfile , enabling the building of specific stages or utilization for different environments with a single Dockerfile .
To specify a stage to build, you can use the target property as follows:
version: '3.8'
services:
<service_name>:
build:
context: <context>
target: <target>Where:
- context is the relative or absolute path to the Dockerfile .
- target is optional and is the name of the build stage from the Dockerfile you want to build and run.
For example, suppose you have the following Dockerfile :
# Base stage
FROM node:14-alpine AS base
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
# Development stage
FROM base AS development
COPY . .
CMD ["npm", "run", "dev"]
# Production stage
FROM node:14-alpine AS production
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --production
COPY ./dist ./dist
CMD ["node", "dist/index.js"]To run the application in production mode, you can use the target property to specify the production stage as follows:
# compose.yaml
version: '3.8'
services:
webapp:
build:
context: ./root
target: productionIn this example, the build engine goes through each stage, leading to the specified target stage named production and builds it to run the application.
Note that, the dependency between the stages affects the build process. For example, if you run the application with the target set to development , the Docker Engine will build both base and development stages due to their interdependence.
You can refer to the official documentation to learn about other supported parameters of the build section.
Easily retrieve this syntax using Warp AI feature
If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp AI feature:

Entering How to use the target property in a compose file? in the AI question input will prompt a human-readable step by step guide including code snippets.
Using the target field with volumes
In Docker Compose, the volumes property in the services is used to mount a local directory to a Docker container (i.e. bind mount).
The target property can be defined within the long syntax of volumes as follows:
version: '3.8'
services:Where:
- service\_name is the name of the service the volume will be mounted to.
- image is the name of the Docker image the container will be launched from.
- source is the name of a volume defined in the top-level volumes key.
- target is the path in the container where the volume is mounted.
For example:
version: '3.8'
services:
web:
image: nginx:alpine
volumes:
- type: volume
source: mydata
target: /data
volumes:
mydata:In this example, the source is the name of volume mydata defined in the top-level volumes property. The target specifies that the volume will be mounted at the /data directory within the web service containers.
You can also read our article on Docker Compose Volume to learn more about bind mounts in Docker.
Using the target field with secrets
In Docker Compose, the secrets property in the services is used to grant access to sensitive data defined in the top-level secrets key.
The target property can be defined within the long syntax of secrets as follows:
version: '3.8'
services:
<service_name>:
image: <image>
secrets:
- source: <source>
target: <target>
secrets:
<source>:Where:
- source is the name of a secret defined in the top-level secrets key.
- target is the name of or absolute path to the file mounted in /run/secrets in the service container. Defaults to source if not specified.
For example:
version: '3.8'
services:
web:
image: nginx:alpine
secrets:
- source: mysecret
target: redis_secret
secrets:
mysecret:In this example, the source is the name of the secret mysecret defined at the top-level secrets key. The target specifies that the secret file will be available at the /run/secrets/redis\_secret directory within the web service containers.
You can also refer to the official documentation to learn more about secrets.
Using the target field with configs
In Docker Compose, the configs property is used by the services to adapt to the specified configuration files without the need to rebuild a Docker image.
The target property can be defined within the long syntax of configs as follows:
version: '3.8'
services:
<service_name>:
image: <image>
configs:
- source: <source>
target: <target>
configs:
<source>:Where:
- source is the name of a configuration defined in the top-level configs key.
- target is the path or name of the file to be mounted in the service containers. Defaults to /<source> if not specified.
For example:
version: '3.8'
services:
web:
image: nginx:alpine
configs:
- source: myconfig
target: redis_config
configs:
myconfig:In this example, the source is the name of the configuration myconfig defined at the top-level configs key. The target specifies that the configuration file will be available at the /redis\_config within the web service containers.
You can also refer to the official documentation to learn more about configs.
Using the target field with ports
In Compose, the ports property within the services is used as a communication endpoint between the host and a container through which a containerized application can send and receive data.
The target property can be defined within the long syntax of ports as follows:
version: '3.8'
services:
<service_name>:
image: <image>
ports:
- target: <target_port>
published: <published_port>Where:
- target\_port is the container port or port range.
- published\_port is the host port the service containers are exposed to.
For example:
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- target: 80
published: 8080In this example, the web service exposes the ports 80 to the specified host port 8080 . These ports can be used for communicating with other containers available on the same network.
You can also read our article on Understanding Port Mappings in Docker Compose.
Related articles
Learning Docker (The Easy Way) Using LazyDocker & Warp
A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Remove a Docker Image
Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.
Override the Container Entrypoint With docker run
Learn how to override and customize the entrypoint of a Docker container using the docker run command.
The Dockerfile ARG Instruction
Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.
Start a Docker Container
Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.
Stop All Docker Containers
How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.
Set Docker Container Hostname
Learn how to set, change and match a docker container hostname.
How To Use An .env File In Docker Compose
Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Restart Docker Containers
Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.
Run Bash Shell In Docker
Start an interactive shell in Docker container