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 to your 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 any other configuration, as long as you have that.
Here is the most basic pipeline that clones a repo and builds an image:
Building a Docker image and pushing it to any registry.
You can push your image to any Registry. First you need to connect your external registry in the integrations page. Here are the instructions for:
- Docker Hub
- Google Container Registry
- Amazon EC2 Container Registry
- Other Registries
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:
Here we use a specific tag -
Codefresh has several other variables that can be used for tagging images. Common examples that you can use are
CF_BUILD_ID. See the variables page for more information.
If you run the pipeline the Docker image will be pushed both to the private Docker regisry (by the build step) and the external docker registry (by the push step)
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.