Skip to content
Latchkey

How to Run Docker-in-Docker in Bitbucket Pipelines

Adding the built-in docker service to a step gives it a Docker daemon so you can build and push images.

Attach the docker service to a step to get a daemon, then run docker build/docker push. Authenticate to your registry with secured variables.

Build and push an image

The docker service enables the daemon; credentials come from secured variables.

bitbucket-pipelines.yml
pipelines:
  default:
    - step:
        name: Build image
        services:
          - docker
        script:
          - echo "${REGISTRY_PASS}" | docker login -u "${REGISTRY_USER}" --password-stdin my-registry.example.com
          - docker build -t my-registry.example.com/app:${BITBUCKET_COMMIT} .
          - docker push my-registry.example.com/app:${BITBUCKET_COMMIT}

Gotchas

  • The Docker daemon shares the step memory budget; large builds often need size: 2x.
  • Use the built-in ${BITBUCKET_COMMIT} variable to tag images with the commit hash.
  • Pass the registry password via stdin (--password-stdin) so it is not captured in process listings.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →