18. Docker + CI/CD Pipelines
18.1 CI/CD Concepts
Section titled “18.1 CI/CD Concepts”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.
18.2 Docker in CI
Section titled “18.2 Docker in CI”When you run Docker commands inside an automated pipeline rather than on your laptop, several new challenges arise.
Docker Builds in CI
Section titled “Docker Builds in CI”The CI server must have Docker installed (or use a tool like BuildKit/Kaniko) to execute docker build.
Build Caching
Section titled “Build Caching”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.
Registry Authentication
Section titled “Registry Authentication”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.
Image Tagging
Section titled “Image Tagging”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.
Test Containers
Section titled “Test Containers”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.
18.3 GitHub Actions (Recommended Starting Point)
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.
Core Terminology
Section titled “Core Terminology”- Workflow: An automated process defined in a
.ymlfile 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 installor 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.
Example: Docker Build & Push Workflow
Section titled “Example: Docker Build & Push Workflow”name: CI to Docker Hub
# Trigger the workflow when code is pushed to the main branchon: 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 }}18.4 Jenkins (Enterprise Alternative)
Section titled “18.4 Jenkins (Enterprise Alternative)”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 Pipeline
Section titled “Jenkins Pipeline”Jenkins workflows are defined in a text file called a Jenkinsfile using a scripting language called Groovy.
Docker Agent
Section titled “Docker Agent”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.
The Flow in Jenkins
Section titled “The Flow in Jenkins”- Build: Jenkins clones the repo and compiles code inside a temporary container.
- Test: Jenkins runs automated tests.
- Push: Jenkins executes
docker pushto send the artifact to the registry. - Deploy: Jenkins SSHs into the production server (or triggers a Kubernetes deployment) to pull the new image.