Docker CI CD Pipeline
Teams ship software many times a day. A CI/CD pipeline automates the path from a code change to a running application. CI stands for continuous integration, and CD stands for continuous delivery. Docker fits this path perfectly because an image behaves the same on every machine.
The Factory Assembly Line
Picture a car factory. Parts enter on one side, machines check and assemble them, and a finished car rolls out the other side. A pipeline works the same way for code. A developer pushes a change, and automated stations test it, package it into an image, and deliver it.
Developer pushes code
|
v
+---------+ +---------+ +---------+ +---------+ +---------+
| Build |-->| Test |-->| Scan |-->| Push |-->| Deploy |
+---------+ +---------+ +---------+ +---------+ +---------+
make image run tests check for send image run new
known flaws to registry container
Why Docker Helps
- The test environment matches production because both use the same image.
- Builds run on any CI server that has Docker.
- Rolling back means starting the previous image tag.
Tagging Images Well
Each pipeline run should produce a unique tag that identifies its source code.
my-app:3f2a9c1 the Git commit ID (exact and traceable) my-app:1.4.0 a release version my-app:latest a moving label for the newest stable build
Deploy the commit tag or version tag. The moving latest tag makes rollbacks and audits difficult.
A GitHub Actions Example
GitHub Actions reads workflow files from the .github/workflows folder of a repository.
name: build-and-push
on:
push:
branches: ["main"]
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Get the code
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: myuser/my-app:${{ github.sha }}
Reading the Workflow
on the event that starts the pipeline (a push to main) jobs groups of steps that run on one machine steps individual actions, run from top to bottom secrets encrypted values stored in repository settings github.sha the commit ID, used as the image tag
Store registry credentials in the repository secrets area. Never write them into the workflow file.
Adding a Test Stage
- name: Build test image
run: docker build --target test -t my-app:test .
- name: Run tests
run: docker run --rm my-app:test
A multi stage Dockerfile can include a stage named test. The pipeline builds that stage, runs it, and stops on failure so broken code never reaches the registry.
Speeding Up Builds With Cache
with:
cache-from: type=gha
cache-to: type=gha,mode=max
Cache settings reuse unchanged layers between runs, which can cut build times from minutes to seconds.
Deployment Choices
Method Fits ------------------------ --------------------------------- SSH and docker compose one or a few servers Docker Swarm update small clusters Kubernetes rollout large clusters Managed container service cloud platforms
A simple server deployment runs docker compose pull followed by docker compose up -d over SSH.
Common Mistakes
- Pushing only the latest tag and losing the ability to roll back.
- Skipping tests to save time.
- Printing secrets in build logs.
- Deploying images that nobody scanned.
Real-World Scenario
A freelance developer maintains a booking site for a small clinic. Every update used to mean connecting to the server, pulling code, building an image by hand, and restarting containers. One typo during a late-night update took the site offline for an hour. The developer sets up a pipeline. A push to the main branch now builds the image, runs the tests, scans for known flaws, pushes the image, and updates the server. The process takes six minutes and needs no manual typing.
Branch Strategy
Branch / Event Pipeline action ----------------- ------------------------------------------ Pull request build and test only Merge to main build, test, scan, push, deploy to staging Version tag push release image, deploy to production
Different events trigger different amounts of work. Pull requests stay fast because they skip the push and deploy stages. Release tags carry the extra approval steps that production deserves.
Measuring Pipeline Health
Track three numbers over time: how long a pipeline run takes, how often runs fail, and how long a failed run waits for a fix. A pipeline that takes thirty minutes discourages developers from pushing small changes. Aim for under ten minutes by using layer caching, parallel test jobs, and small images.
Protecting the Pipeline
Treat the pipeline as part of your production system. Limit who can edit workflow files, rotate registry tokens regularly, and give each token the minimum rights it needs. A token that can only push to one repository does far less damage when it leaks than an account-wide password.
