jidicula
jidicula

Reputation: 3929

Azure Pipelines: Build a specific stage in a multistage Dockerfile without building non-dependent stages

I have a multistage Dockerfile containing the following stages:

FROM python:3.8-slim-bullseye as python-base
# ...

FROM python-base as builder-base
# ...

FROM python-base as runtime-deps
# ...

FROM runtime-deps as dev-deps
# ...

FROM dev-deps as development
# ...

FROM runtime-deps as cloud
# ...

You can see how these stages depend on each other more visually here: stage dependencies

The cloud stage doesn't depend on dev-deps or development, yet those stages still get built when I use the Docker task with the arg --target cloud:

- task: Docker@2
  displayName: Build Docker image
  inputs:
  command: build
  repository: $(IMAGE_NAME)
  tags: $(TAG_NAME)
  arguments: '--target cloud'

Those unneeded stages also still get built when I run the docker command directly in a Bash task:

bash: docker build --target cloud --tag $REGISTRY_NAME/$IMAGE_NAME:$TAG_NAME .

How can I configure a Docker build in a pipeline so only the specified target stage is built?

Upvotes: 1

Views: 1765

Answers (1)

jidicula
jidicula

Reputation: 3929

As skipping stages only works with Buildkit, and Buildkit doesn't seem to be available in the Docker task or in the docker command on Azure Pipelines, you'll have to enable it by using buildx, which uses Buildkit:

- bash: docker buildx build --target cloud --tag $REGISTRY_NAME/$IMAGE_NAME:$TAG_NAME .

You could also use the DOCKER_BUILDKIT=1 environment variable, as described in the Docker docs:

- bash: DOCKER_BUILDKIT=1 docker build --target cloud --tag $REGISTRY_NAME/$IMAGE_NAME:$TAG_NAME .

Upvotes: 1

Related Questions