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
image: node:6
pipelines:
  default:
    - step:
        script:
          - npm install
          - npm test
          - docker build -t <my.container.registry.io>/<my_app>:<my_tag> .

options:
  docker: true

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.

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

  • <my.container.registry.io>
  • <my_app>
  • <my_tag>
bitbucket-pipelines.yml
image: node:6
pipelines:
  default:
    - step:
        script:
          - npm install
          - npm test
          - docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
          - docker build -t <my.container.registry.io>/<my_app>:<my_tag> .
          - docker push <my.container.registry.io>/<my_app>:<my_tag>

options:
  docker: true
Last modified on Jun 14, 2017

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.