Docker security best practices
Updated 11/29/2026
Docker adds isolation, but it does not remove the need for security. A few rules greatly reduce the risks.
1. Use official, pinned images
Prefer official images and pin the version (not latest), so you know exactly what you run:
image: postgres:16.2
2. Do not run as root in the container
Many images allow a non-root user. In the Dockerfile:
USER appuser
3. Do not put secrets in the image
Passwords and keys go through environment variables or .env files, never written directly into the image or into git.
4. Limit resources
Stop a container from consuming the whole server:
docker run --memory=512m --cpus=1 the_image
5. Do not expose the Docker port
Never expose the Docker socket or API port to the internet, it is equivalent to root access on the server.
6. Update images
docker compose pull && docker compose up -d
Combine with a firewall on the server for defense in depth.
Put this guide into practice on your own VPS
EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.
See pricingRelated tutorials
How networking works in Docker
Understand how containers communicate with each other and how to isolate them on separate Docker networks.
Updated 11/29/2026Docker & containersHow to keep data in Docker with volumes
Containers are ephemeral: without volumes, you lose data when you delete the container. Here is how to keep it correctly.
Updated 11/29/2026Docker & containersHow to free up disk space used by Docker
Images, stopped containers and orphaned volumes can fill your disk. Here is how to clean them up safely.
Updated 11/29/2026