Skip to content

18. Docker + CI/CD Pipelines

A robust CI/CD pipeline acts as an automated assembly line for your software.

Code (Developer pushes to GitHub)
↓
Build (Compile the source code)
↓
Test (Run unit and integration tests)
↓
Scan (Run security vulnerability scanners)
↓
Build Image (`docker build` inside the pipeline)
↓
Push Image (`docker push` to the Private Registry)
↓
Deploy (Notify Kubernetes/Swarm to pull the new Image)

By automating this entire flow, teams can safely deploy software dozens of times a day. If any step fails (e.g., a unit test fails or a critical vulnerability is found), the pipeline abruptly halts, preventing broken code from ever reaching the production server.

When you run Docker commands inside an automated pipeline rather than on your laptop, several new challenges arise.

The CI server must have Docker installed (or use a tool like BuildKit/Kaniko) to execute docker build.

By default, every time a CI pipeline runs, it starts completely fresh. This means Docker cannot rely on its local layer cache, making builds painfully slow. Solution: Modern pipelines use remote caching (pulling the previous image from the registry to use as a cache reference) or GitHub Actions cache mechanisms to drastically speed up docker build steps.

The CI server needs permission to push images to your private registry (like Docker Hub or AWS ECR). You must securely store registry credentials in the CI tool’s “Secrets” manager, not in the codebase.

As discussed in the previous section, the pipeline automatically generates dynamic tags for the image, typically injecting the ${GITHUB_SHA} (the Git commit hash) as the image tag so every build is uniquely identifiable.

During the “Test” phase, the pipeline can temporarily spin up throwaway Docker containers (like a temporary Postgres database) using Docker Compose, run integration tests against it, and instantly destroy it once the tests finish.

Section titled “18.3 GitHub Actions (Recommended Starting Point)”

GitHub Actions is built directly into GitHub and is currently the most popular CI/CD tool for modern workflows.

  • Workflow: An automated process defined in a .yml file inside the .github/workflows/ directory.
  • Job: A set of steps that execute on the same runner. Workflows can have multiple jobs (e.g., test-job, build-job) that run in parallel.
  • Step: A single task inside a job (e.g., running npm install or executing a shell script).
  • Runner: The actual virtual machine provided by GitHub that executes your workflow. (Ubuntu runners have Docker pre-installed!).
  • Secrets: Highly sensitive variables (like DOCKERHUB_PASSWORD) configured in the repository settings and injected securely into the runner environment.
name: CI to Docker Hub
# Trigger the workflow when code is pushed to the main branch
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
context: .
push: true
# Tag the image with the word 'latest' AND the specific Git commit hash
tags: myuser/myapp:latest, myuser/myapp:${{ github.sha }}

Jenkins is the grandfather of CI/CD. It is highly complex, incredibly powerful, and ubiquitous in older enterprise environments. While you should learn GitHub Actions first, understanding Jenkins at a high level is necessary for enterprise DevOps.

Jenkins workflows are defined in a text file called a Jenkinsfile using a scripting language called Groovy.

Unlike GitHub Actions which provides runners for you, you must host Jenkins yourself. Jenkins pipelines can use Docker as an “agent”. Instead of installing Python or Node.js directly on the Jenkins server, the pipeline temporarily spins up a Docker container, runs the build/test steps inside that container, and then destroys it. This keeps the Jenkins server perfectly clean.

  1. Build: Jenkins clones the repo and compiles code inside a temporary container.
  2. Test: Jenkins runs automated tests.
  3. Push: Jenkins executes docker push to send the artifact to the registry.
  4. Deploy: Jenkins SSHs into the production server (or triggers a Kubernetes deployment) to pull the new image.