0
0
DockerHow-ToBeginner · 4 min read

How to Push Docker Image in CI: Step-by-Step Guide

To push a Docker image in a CI pipeline, first build the image using docker build, then log in to your Docker registry with docker login, and finally push the image using docker push. Automate these steps in your CI configuration file to enable continuous deployment.
📐

Syntax

Here is the basic syntax to push a Docker image in CI:

  • docker build -t <image-name>:<tag> . - Builds the Docker image with a name and tag.
  • docker login -u <username> -p <password> <registry-url> - Logs into the Docker registry.
  • docker push <image-name>:<tag> - Pushes the image to the registry.
bash
docker build -t myapp:latest .
docker login -u myuser -p mypassword docker.io
docker push myapp:latest
💻

Example

This example shows a simple GitHub Actions workflow that builds and pushes a Docker image to Docker Hub when code is pushed to the main branch.

yaml
name: Build and Push Docker Image

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build Docker image
        run: docker build -t myuser/myapp:latest .

      - name: Push Docker image
        run: docker push myuser/myapp:latest
Output
Run actions/checkout@v3 Login Succeeded Sending build context to Docker daemon 8.192kB Step 1/5 : FROM alpine ---> a24bb4013296 ... Successfully built <image-id> Successfully tagged myuser/myapp:latest The push refers to repository [docker.io/myuser/myapp] ... latest: digest: sha256:<digest> size: 1234
⚠️

Common Pitfalls

Common mistakes when pushing Docker images in CI include:

  • Not logging in to the Docker registry before pushing, causing authentication errors.
  • Using incorrect image names or tags, leading to push failures or overwriting wrong images.
  • Exposing credentials in the CI config instead of using secure secrets.
  • Forgetting to set up permissions or tokens for the CI system to access the registry.
bash
## Wrong: No login before push

docker build -t myapp:latest .
docker push myapp:latest

## Right: Login before push

docker build -t myapp:latest .
docker login -u myuser -p mypassword docker.io
docker push myuser/myapp:latest
📊

Quick Reference

Tips for pushing Docker images in CI:

  • Always use secure secrets for credentials.
  • Tag images clearly with version or commit SHA.
  • Use official CI actions or plugins for login and push steps.
  • Test your pipeline locally before pushing to CI.

Key Takeaways

Always log in to your Docker registry before pushing images in CI.
Use clear image tags to avoid confusion and overwriting images.
Store credentials securely using CI secrets or environment variables.
Automate build and push steps in your CI configuration for smooth deployment.
Test your Docker commands locally before integrating into CI pipelines.