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
How to Set a Custom Docker Image in Bitbucket PipelinesPin the build environment in Bitbucket Pipelines by setting a Docker image globally or per step, including pr…
How to Use size 2x for More Memory in Bitbucket PipelinesGive a memory-hungry Bitbucket Pipelines step more RAM with the size key (2x, 4x), and split the budget corre…