A container is a process boundary with useful isolation, not a complete security guarantee. A hardened service should have only the privileges and resources its job requires, while remaining observable and recoverable.
Reduce runtime privilege
Run as a non-root user and use a read-only root filesystem when the application permits it. Mount writable paths explicitly and narrowly. Drop Linux capabilities the process does not need, and avoid privileged mode or host namespace sharing. Apply seccomp and other runtime profiles supported by the deployment environment. Verify that the final image contains no build credentials or development tools that are unnecessary at runtime.
Set CPU and memory limits based on observed workload and service-level expectations. Resource limits prevent one process from consuming the whole host, but overly tight limits can cause repeated restarts. Choose a shutdown grace period that lets the service stop accepting work and finish in-flight requests.
Keep images and health checks maintainable
Use a small, maintained base image and rebuild it to receive security updates. Pin versions or digests according to the release process, and keep an update path so pinning does not become permanent staleness. Health checks should detect a service that cannot serve requests without making every optional dependency a liveness requirement.
Inspect effective runtime settings, scan the built image, and test under the same restrictions used in deployment. Confirm writable paths, signal handling, startup, and recovery after a dependency restart. Security settings need operational tests so hardening does not become a source of fragile restarts.
Verify the container's effective configuration
Docker controls memory and CPU usage through the host's resource controls. For example, --memory sets a hard memory limit and --cpus constrains the CPU time available to the container. A read-only root filesystem still permits writes to explicitly writable mounts. Check which paths the service uses for cache, uploads, and temporary files, then give it narrowly scoped storage with the lifetime those paths need. Docker resource constraints
Run the image as a non-root account. Drop capabilities the process does not require, and inspect the runtime user, mounts, and security configuration after Compose or the deployment system applies its overrides. Test normal startup and the path that writes a temporary file. Keep administration sockets and unrelated host paths outside the service. These are concrete checks for a web service with trusted code and a known writable surface. Docker Engine security
Worked example
Use a pre-pulled image in a local test. These flags restrict this simple workload; they are not a complete sandbox for hostile programs.
shdocker run --rm --read-only --cap-drop ALL --security-opt no-new-privileges \ --network none --pids-limit 64 --memory 128m --cpus 0.5 \ --user 65534:65534 node:24-alpine node -e 'console.log("ready")'




Community
Discussion
Loading comments…