Terminus
Understand target in Docker Compose

Understand target in Docker Compose

The short answer

In Docker Compose, the [.inline-code] target[.inline-code]  property in the [.inline-code] build[.inline-code]  section of a Compose file is used to select a specific stage from a multi-stage [.inline-code] Dockerfile[.inline-code] , enabling the building of specific stages or utilization for different environments with a single [.inline-code] Dockerfile[.inline-code] .

To specify a stage to build, you can use the [.inline-code] target[.inline-code]  property as follows:

version: '3.8'

services:
  <service_name>:
    build:
      context: <context>
      target: <target>

Where:

  • [.inline-code] context[.inline-code] is the relative or absolute path to the [.inline-code] Dockerfile[.inline-code] .
  • [.inline-code] target[.inline-code]  is optional and is the name of the build stage from the [.inline-code] Dockerfile[.inline-code]  you want to build and run.

For example, suppose you have the following [.inline-code] Dockerfile[.inline-code] :

 # 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 [.inline-code] target[.inline-code]  property to specify the [.inline-code] production[.inline-code]  stage as follows:

 # compose.yaml
version: '3.8'

services:
  webapp:
    build:
      context: ./root
      target: production

In this example, the build engine goes through each stage, leading to the specified [.inline-code] target[.inline-code]  stage named [.inline-code] production[.inline-code]  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 [.inline-code] target[.inline-code]  set to [.inline-code] development[.inline-code] , the Docker Engine will build both [.inline-code] base[.inline-code] and [.inline-code] development[.inline-code]  stages due to their interdependence.

You can refer to the official documentation to learn about other supported parameters of the [.inline-code] build[.inline-code]  section.

[#easily-recall-syntax-with-ai] Easily retrieve this syntax using Warp AI feature [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp AI feature:

Entering [.inline-code] How to use the target property in a compose file?[.inline-code]  in the AI question input will prompt a human-readable step by step guide including code snippets.

[#use-target-with-volumes] Using the [.inline-code] target[.inline-code] field with volumes [#use-target-with-volumes]

In Docker Compose, the [.inline-code] volumes[.inline-code]  property in the services is used to mount a local directory to a Docker container (i.e. bind mount).

The [.inline-code] target[.inline-code]  property can be defined within the long syntax of [.inline-code] volumes[.inline-code]  as follows:

 version: '3.8'

services:
  

Where:

  • [.inline-code] service_name[.inline-code]  is the name of the service the volume will be mounted to.
  • [.inline-code] image[.inline-code]  is the name of the Docker image the container will be launched from.
  • [.inline-code] source[.inline-code]  is the name of a volume defined in the top-level [.inline-code] volumes[.inline-code]  key.
  • [.inline-code] target[.inline-code]  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 [.inline-code] source[.inline-code]  is the name of volume [.inline-code] mydata[.inline-code]  defined in the top-level [.inline-code] volumes[.inline-code]  property. The [.inline-code] target[.inline-code]  specifies that the volume will be mounted at the [.inline-code] /data[.inline-code]  directory within the [.inline-code] web[.inline-code]  service containers.

You can also read our article on Docker Compose Volume to learn more about bind mounts in Docker.

[#use-targets-with-secrets] Using the [.inline-code] target[.inline-code]  field with secrets [#use-targets-with-secrets]

In Docker Compose, the [.inline-code] secrets[.inline-code]  property in the services is used to grant access to sensitive data defined in the top-level [.inline-code] secrets[.inline-code]  key.

The [.inline-code] target[.inline-code]  property can be defined within the long syntax of [.inline-code] secrets[.inline-code]  as follows:

 version: '3.8'

services:
  <service_name>:
    image: <image>
      secrets:
      - source: <source>
        target: <target>

secrets:
  <source>:

Where:

  • [.inline-code] source[.inline-code]  is the name of a secret defined in the top-level [.inline-code] secrets[.inline-code]  key.
  • [.inline-code] target[.inline-code]  is the name of or absolute path to the file mounted in [.inline-code] /run/secrets[.inline-code]  in the service container. Defaults to [.inline-code] source[.inline-code]  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 [.inline-code] source[.inline-code]  is the name of the secret [.inline-code] mysecret[.inline-code]  defined at the top-level [.inline-code] secrets[.inline-code]  key. The [.inline-code] target[.inline-code]  specifies that the secret file will be available at the [.inline-code] /run/secrets/redis_secret[.inline-code]  directory within the [.inline-code] web[.inline-code]  service containers. 

You can also refer to the official documentation to learn more about secrets.

[#use-targets-with-configs] Using the [.inline-code] target[.inline-code] field with configs [#use-targets-with-configs]

In Docker Compose, the [.inline-code] configs[.inline-code]  property is used by the services to adapt to the specified configuration files without the need to rebuild a Docker image.

The [.inline-code] target[.inline-code]  property can be defined within the long syntax of [.inline-code] configs[.inline-code]  as follows:

version: '3.8'

services:
  <service_name>:
    image: <image>
    configs:
      - source: <source>
        target: <target>

configs:
  <source>:

Where:

  • [.inline-code] source[.inline-code]  is the name of a configuration defined in the top-level [.inline-code] configs[.inline-code]  key. 
  • [.inline-code] target[.inline-code]  is the path or name of the file to be mounted in the service containers. Defaults to [.inline-code] /<source>[.inline-code]  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 [.inline-code] source[.inline-code]  is the name of the configuration [.inline-code] myconfig[.inline-code]  defined at the top-level [.inline-code] configs[.inline-code]  key. The [.inline-code] target[.inline-code]  specifies that the configuration file will be available at the [.inline-code] /redis_config[.inline-code]  within the [.inline-code] web[.inline-code]  service containers.

You can also refer to the official documentation to learn more about configs.

[#use-target-with-ports] Using the [.inline-code] target[.inline-code] field with ports [#use-target-with-ports]

In Compose, the [.inline-code] ports[.inline-code]  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 [.inline-code] target[.inline-code]  property can be defined within the long syntax of [.inline-code] ports[.inline-code]  as follows:

version: '3.8'

services:
  <service_name>:
    image: <image>
     ports:
      - target: <target_port>
        published: <published_port>

Where:

  • [.inline-code] target_port[.inline-code]  is the container port or port range.
  • [.inline-code] published_port[.inline-code]  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

In this example, the [.inline-code] web[.inline-code]  service exposes the ports [.inline-code] 80[.inline-code]  to the specified host port [.inline-code] 8080[.inline-code] . 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.