SyntaxStudy
Sign Up
Docker CI/CD Pipeline with Docker
Docker Beginner 1 min read

CI/CD Pipeline with Docker

A production Docker CI/CD pipeline typically follows these stages: checkout code, run unit tests in the test stage of the multi-stage Dockerfile, build the production image, scan for vulnerabilities, push to the registry with version tags, and deploy by updating the running container or triggering an orchestrator rollout. GitHub Actions has first-class Docker support via the docker/build-push-action and docker/login-action actions. BuildKit caching can be enabled to dramatically speed up repeated builds by storing layer cache in the registry so CI runners benefit from cache even without a persistent local cache. Blue-green deployments with Docker can be done manually: run the new version alongside the old, switch the load balancer (Nginx upstream) to the new container once health checks pass, then remove the old container. Tools like Watchtower automate container updates by polling the registry for new image versions, though this approach trades control for convenience and is not recommended for critical production services.
Example
# .github/workflows/docker.yml
name: Build and Push

on:
  push:
    branches: [main]
    tags:     ['v*']

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

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

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: myuser/myapp
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha,prefix=sha-

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          cache-from: type=registry,ref=myuser/myapp:buildcache
          cache-to:   type=registry,ref=myuser/myapp:buildcache,mode=max