Build and push a Docker image to a container registry

For many CI and CD workflows, you might want to package and deploy your application as a Docker image after it passes automated tests.

Build a Docker image

Before you can build a Docker image, you need to enable access to the Docker daemon by simply adding the docker: true option to your bitbucket-pipelines.yml file.

Here is an example of how to build a Node.js application as a Docker file. You will need to replace the following placeholders with your own details:

  • <my.container.registry.io>

  • <my_app>

  • <my_tag>

bitbucket-pipelines.yml

1 2 3 4 5 6 7 8 9 10 image: node:10.15.0 pipelines: default: - step: script: - npm install - npm test - docker build -t <my.container.registry.io>/<my_app>:<my_tag> . services: - docker

Push to a container registry

Once your application has been built into a Docker image, you'll want to push it to a container registry for safe-keeping, ready for deployment.

You'll need to log into your container registry before pushing. Here's an example that builds and pushes a Docker image to a container registry.

First make sure you've set up the variables DOCKER_HUB_USER and DOCKER_HUB_PASSWORD.

Then you will need to replace the following placeholders with your own details:

  • <my.container.registry.io>

  • <my_app>

  • <my_tag>

bitbucket-pipelines.yml

1 2 3 4 5 6 7 8 9 10 11 12 image: node:10.15.0 pipelines: default: - step: script: - npm install - npm test  - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASSWORD - docker build -t <my.container.registry.io>/<my_app>:<my_tag> . - docker push <my.container.registry.io>/<my_app>:<my_tag> services: - docker
Last modified on Nov 13, 2020
Cached at 3:35 PM on Jan 23, 2021 |

Additional Help

Ask the Community