0
0
DockerHow-ToBeginner · 4 min read

How to Use Docker in CI/CD Pipelines for Automation

Use Docker in CI/CD by creating a pipeline that builds Docker images, runs tests inside containers, and pushes images to a registry. Automate these steps with CI/CD tools like GitHub Actions or Jenkins using docker build, docker run, and docker push commands.
📐

Syntax

In a CI/CD pipeline, you typically use these Docker commands:

  • docker build -t <image-name> .: Builds a Docker image from a Dockerfile in the current directory.
  • docker run --rm <image-name>: Runs a container from the image and removes it after execution.
  • docker push <registry>/<image-name>: Pushes the built image to a Docker registry like Docker Hub.

These commands automate building, testing, and deploying your app inside containers.

bash
docker build -t myapp:latest .
docker run --rm myapp:latest

docker push myregistry/myapp:latest
💻

Example

This example shows a simple GitHub Actions workflow that builds a Docker image, runs tests inside the container, and pushes the image to Docker Hub.

yaml
name: Docker CI/CD

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

    - name: Run tests inside container
      run: |
        docker run --rm myusername/myapp:latest sh -c "./run-tests.sh"

    - name: Push Docker image
      run: docker push myusername/myapp:latest
Output
Build successful Tests passed Image pushed to Docker Hub
⚠️

Common Pitfalls

Common mistakes when using Docker in CI/CD include:

  • Not caching Docker layers, causing slow builds.
  • Forgetting to log in to the Docker registry before pushing images.
  • Running containers without cleaning up, which wastes resources.
  • Not handling secrets securely (e.g., Docker credentials).

Always use multi-stage builds and secrets management features of your CI/CD tool.

bash
## Wrong: Pushing without login

docker push myusername/myapp:latest

## Right: Login before push

docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD

docker push myusername/myapp:latest
📊

Quick Reference

CommandPurpose
docker build -t .Build Docker image from Dockerfile
docker run --rm Run container and remove after exit
docker push /Push image to Docker registry
docker login -u -p Authenticate to Docker registry
Use CI/CD secretsSecurely store credentials and tokens

Key Takeaways

Automate Docker image build, test, and push steps in your CI/CD pipeline.
Always log in to your Docker registry before pushing images.
Use container cleanup to avoid resource waste during CI runs.
Securely manage credentials using your CI/CD tool's secrets feature.
Optimize builds with caching and multi-stage Dockerfiles.