dockerbitbucketbitbucket-pipelinesdocker-multi-stage-build

Docker build target is ignored in bitbucket pipeline


I'm facing a problem to build a docker image in a bitbucket pipeline. I'm using the bitbucket cloud hosted solution, not a self-hosted runner.

I have a Dockerfile with multiple build stages. When running the following command in the bitbucket pipeline, the --target option is ignored.

step:

definitions:
  steps:
    - step: &build-staging
        name: Build staging
        services:
        - docker
        script:
          - docker build -t image_name --target staging .

I didn't find any mention of a limitation regarding multi-stage build in the documentation about running docker commandes in bitbucket pipelines. Any idea?


Solution

  • This is not a Bitbucket issue, but the Docker itself. BuildKit fixes this bug in multi-stage builds in the legacy docker build engine.

    So, just enable the BuildKit by inserting the line

    - export DOCKER_BUILDKIT=1
    

    before the docker build ... command and the image will be built with the specified target.