0
0
DockerHow-ToBeginner · 3 min read

How to Build Docker Image in CI: Simple Steps

To build a Docker image in CI, use the docker build command inside your CI pipeline script, specifying the Dockerfile location and image tag. Then, optionally push the image to a registry with docker push for deployment.
📐

Syntax

The basic command to build a Docker image is docker build. You specify the directory containing the Dockerfile and use -t to tag the image with a name and version.

  • docker build -t <image-name>:<tag> <path>: Builds the image.
  • docker push <image-name>:<tag>: Pushes the image to a Docker registry.
bash
docker build -t myapp:1.0 .
docker push myapp:1.0
💻

Example

This example shows a GitHub Actions workflow that builds a Docker image from the current directory and pushes it to Docker Hub. It uses environment variables for Docker Hub credentials.

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 mydockerhubuser/myapp:latest .

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

Common Pitfalls

Common mistakes when building Docker images in CI include:

  • Not logging into the Docker registry before pushing, causing authentication errors.
  • Using the latest tag without versioning, which can cause deployment confusion.
  • Not caching Docker layers properly, leading to slow builds.
  • Forgetting to set up secrets for registry credentials securely.
yaml
Wrong:
  run: docker push mydockerhubuser/myapp:latest

Right:
  - uses: docker/login-action@v2
    with:
      username: ${{ secrets.DOCKER_USERNAME }}
      password: ${{ secrets.DOCKER_PASSWORD }}

  - run: docker push mydockerhubuser/myapp:latest
📊

Quick Reference

Tips for building Docker images in CI:

  • Always log in to your Docker registry before pushing images.
  • Tag images with meaningful versions, not just latest.
  • Use multi-stage Dockerfiles to keep images small.
  • Cache dependencies to speed up builds.
  • Store credentials securely using CI secrets.

Key Takeaways

Use docker build -t image:tag . to build images in CI pipelines.
Log in to your Docker registry before pushing images to avoid authentication errors.
Tag images with versions to track deployments clearly.
Store registry credentials securely using CI environment secrets.
Optimize Dockerfiles and caching to speed up CI builds.