Build and push an Image

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 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 have a Codefresh account already, you can easily create a free one from the sign-up page.

Building and push Docker image to default registry

Building a Docker image with Codefresh is easy, and only requires a simple step. In addition, all successful pipelines in Codefresh automatically push to your default Docker registry, without additional configuration, if you have one.

Here is the most basic pipeline that clones a repo and builds an image:

codefresh.yml

version: '1.0'
stages:
- checkout
- build
steps:
  clone:
    title: Cloning main repository...
    type: git-clone
    stage: checkout
    repo: 'codefreshdemo/cf-example-build-and-push'
    revision: 'master'
    git: github
  build_my_app:
    title: Building Node.Js Docker Image
    type: build
    stage: build
    image_name: my-node-js-app
    working_directory: {{clone}}
    tag: 'master'
    dockerfile: Dockerfile

Building and pushing Docker image to any registry.

You can push your image to any registry.

Here is the full example:

codefresh.yml

version: '1.0'
stages:
- checkout
- build
- push
steps:
  clone:
    title: Cloning main repository...
    type: git-clone
    stage: checkout
    repo: 'codefreshdemo/cf-example-build-and-push'
    revision: 'master'
    git: github
  build_my_app:
    title: Building Node.Js Docker Image
    type: build
    stage: build
    image_name: my-node-js-app
    working_directory: {{clone}}
    tag: 'master'
    dockerfile: Dockerfile
  push_to_my_registry:
    stage: 'push'
    type: push
    title: Pushing to a registry
    candidate: ${{build_my_app}}
    tag: 'v1.0.0'
    registry: dockerhub
    image_name: kkapelon/my-node-js-app
    

Here we use a specific tag - v1.0.0 but Codefresh has several variables that you can use to tag images. Common examples are CF_BRANCH_TAG_NORMALIZED, CF_SHORT_REVISION or CF_BUILD_ID. Read more on variables.

Pushing image to external registry

Pushing image to external registry

If you run the pipeline, the Docker image is pushed both to the private Docker regisry (by the build step) and the external docker registry (by the push step).

More options for pushing images

Codefresh has several options when it comes to pushing images:

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

Read more in push steps in pipelines.

CI/CD pipeline examples
Build an Image with the Dockerfile in root directory
Build an Image by specifying the Dockerfile location
Build an Image from a different Git repository
Build an Image With Build arguments