Build and Push an Image

How to build Docker images and push them to registries with Codefresh

Building a Docker image and then pushing it to a registry is one of the most basic scenarios for creating a Pipeline. In this example we will use a demo Node.js application that will be packaged in a Docker image.

The source code of the repository is located at https://github.com/codefreshdemo/cf-example-build-and-push. Feel free to fork it if you want to follow along.

If you don’t already have a Codefresh account, you can easily create a free one from the sign-up page.

Building a Docker image and pushing it to the Codefresh registry

All Codefresh accounts come with a private integrated Docker registry. The nice thing about this registry is that it is fully automated. All successful pipelines in Codefresh automatically push to that registry without any other configuration.

So in the most simple case, you only need a single build step and Codefresh will automatically push the image for you!

Here is the full pipeline:

codefresh.yml

version: '1.0'
steps:
  build_image:
    title: Building Voting Image
    type: build
    image_name: my-voting-image
    dockerfile: Dockerfile

If you create this pipeline in Codefresh and run it you will see the automatic pushing of the image in the Codefresh registry:

The push refers to repository [r.cfcr.io/kostis-codefresh/my-voting-image]                                                                 
Layer '4624212f67bc' successfully pushed
Layer '573f5d62f821' successfully pushed    
Layer '573f5d62f821' successfully pushed
...

You can then visit the Codefresh Registry and view your image:

Pushing to the built-in registry

Pushing to the built-in registry

That’s it. Using the Codefresh Registry is very easy, and no extra configuration is needed.

Building a Docker image and pushing it to an external registry.

You can also push your image to any external Registry. First you need to connect your external registry in the integrations page. Here are the instructions for:

Once that is done, you only need to add a push step in your pipeline and use the registry name of your integration.

Here is the full example:

codefresh.yml

version: '1.0'
steps:
  build_image:
    title: Building Voting Image
    type: build
    image_name: my-voting-image
    dockerfile: Dockerfile
  push_to_registry:
    title: Pushing to Docker Registry 
    type: push
    #A candidate is the image that we want to push to registry
    candidate: '${{build_image}}'
    # You can push the image with whatever tag you want. In our example we use CF_BRANCH that holds the git branch name
    tag: '${{CF_BRANCH}}'
    registry: <your-registry-configuration-name>    

Codefresh has several other variables that can be used for tagging images. Other common examples that you can use are CF_SHORT_REVISION or CF_BUILD_ID. See the variables page for more information.

More options for push

Codefresh has several more options when it comes to pushing:

  • You can specify multiple tags to be pushed
  • You can use directly ECR registries
  • You can embed credentials in the push steps

See the push step documentation for more details.