• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Understand target in Docker Compose

Thumbnail for Mansi ManhasMansi Manhas

Mansi Manhas

Published: 2/16/2024

About Terminus

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>

Run in Warp

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"]

Run in Warp

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

Run in Warp

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

Thumbnail for

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:
  
Run in Warp

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:

Run in Warp

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

Run in Warp

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:

Run in Warp

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

Run in Warp

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:

Run in Warp

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>

Run in Warp

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

Run in Warp

In 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.

Written by

Thumbnail for Mansi ManhasMansi Manhas

Mansi Manhas

Filed Under

Related Articles

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

Docker

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Docker

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Docker
Thumbnail for Gabriel ManricksGabriel Manricks

Launch MySQL Using Docker Compose

Learn how to launch a MySQL container in Docker Compose.

DockerSQL

Execute in a Docker Container

Learn how to execute one or multiple commands in a Docker container using the docker exec command.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Expose Docker Container Ports

Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.

Docker

Restart Containers In Docker Compose

Learn how to restart and rebuild one or more containers in Docker Compose.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Output Logs in Docker Compose

Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Rename A Docker Image

Learn how to rename Docker images locally and remotely using the docker tag command.

Docker

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null