Docker makes it insanely easy to build and ship apps. But with great power comes great... yeah, you guessed it โ risk. In this post, we'll explore essential practices for securing both your Docker containers and hosts. We'll look at how to drop privileges, manage secrets safely, scan for vulnerabilities, and lock things down tight. ๐ก๏ธ
By default, Docker containers run processes as root. This is dangerous.
You can prevent this by creating a user in your Dockerfile:
# Dockerfile (secure user)
FROM node:18-alpine
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser
WORKDIR /app
COPY . .
CMD ["node", "index.js"]
๐ฟ You just minimized risk โ even if someone breaks in, they're not root!
Secrets like API keys or DB credentials should never be hardcoded or stored in ENV
in Dockerfiles.
--env or --env-file at runtime-v /secrets:/run/secretsExample: Using secrets via file:
docker run -v /my/secrets:/run/secrets myapp
Inside your app, read from /run/secrets/db_password securely.
Outdated packages = exploit playgrounds. Tools like Trivy or Docker Scout can help.
brew install aquasecurity/trivy/trivy
trivy image node:18-alpine
Look for CVEs (Common Vulnerabilities and Exposures) and fix them before shipping.
Docker containers get default privileges via Linux kernel. Use seccomp to drop unnecessary ones.
Example of a restricted run:
docker run --security-opt seccomp=seccomp.json myapp
You can generate a custom seccomp.json to limit syscalls.
Same with AppArmor and SELinux โ use OS-level profiles to restrict containers.
Base image = your app's foundation. Pick wisely:
node:18-alpine โ small, hardenedpython:3.12-slim โ minimal attack surfaceubuntu:latest โ bigger surface, slowerSmaller images = fewer vulnerabilities. Win-win.
Use tools like:
Monitoring = knowing when something's fishy before it's too late.
Docker can now run without root access using rootless mode. It's ideal for dev machines or shared environments.
To enable:
dockerd-rootless-setuptool.sh install
Then restart Docker as your user โ no root needed, less damage possible.
USERSecuring containers is about layers. Don't rely on just Docker defaults. Apply these practices incrementally, review your images regularly, and bake security right into your pipeline.
โ Blog by Aelify (ML2AI.com)
๐ Documentation Index