Security with Docker is mostly correct, boring defaults applied consistently. Here is the short list that removes most of the risk.
Safe defaults
- Scan images in CI (Trivy/Grype) and fail the build on high-severity CVEs
- Use BuildKit secret mounts (--mount=type=secret) instead of ENV for build-time secrets
- Drop Linux capabilities and run read-only root filesystems where possible
Wire it into the pipeline
Build and push an image from GitHub Actions using Buildx and layer caching.
name: build-image
on: { push: { branches: [main] } }
jobs:
image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=maxReinforcing habits
- Pin base image tags (or digests) instead of :latest for reproducibility
- Order layers from least- to most-frequently changed to maximize cache hits
- Use multi-stage builds to keep runtime images tiny and attack surface small
Want senior help putting Docker to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.