DEV Community

Cover image for How to Secure Docker: The Default Settings Will Burn You
Mango Developer
Mango Developer

Posted on Originally published at mangodeveloper.com

How to Secure Docker: The Default Settings Will Burn You

$32

$33

Prerequisites

  • A Linux host running Docker (Engine 20.10+ or 24+). The commands in this article target Linux; Docker Desktop on Mac/Windows runs containers in a Linux VM, which changes the user-namespace story slightly.
  • Root or sudo on the host for the user-namespace-remapping setup. The Dockerfile changes can be done by any developer.
  • Basic Docker knowledge: how to read a Dockerfile, what docker run flags do, what docker-compose.yml looks like.
  • An app to harden. The examples use a Python FastAPI app because it's a common shape - Flask/Express/Go/Rust translate directly.
  • Trivy installed for image scanning (brew install trivy or apt install trivy).
  • Optional but recommended: Hadolint for Dockerfile linting (brew install hadolint).

$34

$35

$36

$37

$38

$39

$3a

$3b

$3c

$3d

Step 9: Pin Image Digests

Tags are mutable. python:3.12-slim today is not the same image as python:3.12-slim last week. A supply-chain attack on the upstream registry could replace a tagged image with a malicious one. Pin by digest:

# Not pinned (vulnerable to tag mutation)
FROM python:3.12-slim

# Pinned (immutable)
FROM python:3.12-slim@sha256:1bb9b1d4d6f3a4d8b3a7c8e2a5c0e3f3a6c0e3f3a6c0e3f3a6c0e3f3a6c0e3f3a
Enter fullscreen mode Exit fullscreen mode

Get the digest:

docker pull python:3.12-slim
docker inspect --format='{{index .RepoDigests 0}}' python:3.12-slim
# python@sha256:1bb9b1d4d6f3a4d8b3a7c8e2a5c0e3f3a6c0e3f3a6c0e3f3a6c0e3f3a6c0e3f3a
Enter fullscreen mode Exit fullscreen mode

Use Renovate or Dependabot to automatically open PRs when pinned images have updates. You get the security of immutability plus the convenience of automated updates.

$3e

$3f

$40

$41

$42

$43

$44

$45


Originally published on Mango Developer.

Top comments (0)