18. CI/CD Pipelines with Docker
From Git Commit to Production
A CI/CD pipeline automates building, testing, and deploying your Docker images. Every commit to your repository triggers the pipeline, ensuring that your images are always tested and ready for deployment. This module covers GitHub Actions, the most widely used CI/CD platform for Docker projects.
GitHub Actions for Docker
GitHub Actions runs workflows defined in `.github/workflows/*.yml`. A workflow can trigger on push, pull request, schedule, or manual dispatch. For Docker, you typically: check out code, set up Docker Buildx, log in to a registry, build the image (using cache from previous runs for speed), scan for vulnerabilities, and push to the registry. For production, also deploy to Swarm or Kubernetes as a separate job.
# .github/workflows/docker-build.yml
name: Build and Push Docker Image
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha,prefix=,format=short
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Scan image for vulnerabilities
run: docker scout cves ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest --exit-code
Caching in CI: The Critical Performance Optimization
Without caching, every CI run would rebuild your entire image from scratch — wasting minutes and cloud resources. BuildKit supports remote cache backends. The `gha` (GitHub Actions) cache backend stores layer cache in GitHub's cache service. The `inline` cache embeds cache metadata in the image itself (useful for `--cache-from`). Use `cache-from: type=gha` to pull cache from previous runs, and `cache-to: type=gha,mode=max` to push cache for future runs. This can reduce build times from 5 minutes to 30 seconds.
Deploying to Production After Build
After the image is built and pushed, a separate job can deploy it to production. Use `needs: build-and-push` to make the deployment job wait for the build job to complete. Deployment steps typically: SSH to the server, pull the new image, and restart the service. For Swarm, use `docker stack deploy` with the updated image tag. For Kubernetes, use `kubectl set image` to trigger a rolling update. Always deploy using the git commit SHA tag, not `latest`, so you can roll back to any previous version.
deploy:
needs: build-and-push
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to Swarm
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SWARM_HOST }}
username: ${{ secrets.SWARM_USER }}
key: ${{ secrets.SWARM_SSH_KEY }}
script: |
docker stack deploy -c /opt/app/docker-stack.yml --with-registry-auth app
docker service update --image ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} app_web
Knowledge Check
Ready to test your understanding of 18. CI/CD Pipelines with Docker?