Step 1: Tag your Docker image
First, you need to tag your local Docker image for the private registry format. The address of the private registry is typically <your-private-registry>/<image-name>:<tag>.
Example: If your private registry address is registry.example.com, your image name is my-app, and you want to tag the version as v1.0, you can use the following command to tag it:
bashdocker tag my-app:latest registry.example.com/my-app:v1.0
This command creates a new tag that points to the original image but uses the new registry address and version number.
Step 2: Log in to the private registry
Before pushing the image, you need to log in to your private registry using the docker login command:
bashdocker login registry.example.com
You need to provide your username and password for authentication. In a CI/CD environment, these credentials can be provided via environment variables or secret management tools.
Step 3: Push the image to the private registry
Once logged in successfully, you can use the docker push command to push the image to the registry:
bashdocker push registry.example.com/my-app:v1.0
This command uploads your image to the specified private registry. The upload process will show the push progress.
Step 4: Verify the image has been successfully pushed
After completing the push, you can verify the image has been successfully uploaded by browsing the UI of the private registry or using command-line tools to query the list of images in the repository.
For example, use the following command to view the list of images in the private registry:
bashcurl -u "username:password" https://registry.example.com/v2/_catalog
Alternatively, if your private registry supports the Docker Registry HTTP API V2, you can use the relevant API endpoints to query.
Example
Suppose I was responsible for pushing multiple microservice Docker images to the company's private registry in a project. I used Jenkins to automate the build and push process. Each service's Dockerfile is located in its source code repository, and the Jenkinsfile includes steps for building and pushing the images:
- Build the image:
docker build -t my-app:${BUILD_NUMBER} . - Tag the image:
docker tag my-app:${BUILD_NUMBER} registry.example.com/my-app:${BUILD_NUMBER} - Push the image:
docker push registry.example.com/my-app:${BUILD_NUMBER}
The entire process is automated and integrated into the CI/CD pipeline, ensuring that images are updated and pushed promptly after each code update.
This example illustrates how to push Docker images to a private registry in a real project and highlights the importance of automating this process.