An early container setup I worked on ran the app as root, mounted the Docker socket, exposed a debug port, and copied an API key into the image. Each choice made local development easier. Together, they gave a compromised app more access than it needed.
These are the checks I now make when reviewing a Docker setup. They reduce risk; they do not turn a container into a complete security boundary.
Run the app as a non-root user
If a process is compromised, its privileges matter. A container running as root gives an attacker more control inside that container. The host impact depends on the runtime configuration, mounted files, capabilities, and daemon access, so “container root” does not automatically mean “host root.”
For the official Node.js image, you can switch to its non-root node user after setting file permissions:
Use the account and ownership model that fits your base image. Docker’s build best practices explain the USER instruction and non-root users.
Keep the build context small
COPY . . copies files from the build context, which can include local credentials, Git history, test data, and installed dependencies. Add a .dockerignore file and review it when the project changes:
This also makes builds easier to reproduce and can reduce unnecessary uploads to the builder. I once found a container image that included .git history; removing a secret from the latest source file would not remove it from that history.
Keep credentials out of image layers
Avoid putting credentials in ENV or ARG instructions. Environment values become part of the image configuration, and build arguments can appear in image history or build provenance. For a credential needed only during a build, use a BuildKit secret mount. For application credentials, use your deployment platform’s runtime secret mechanism and restrict which services can read them.
Treat the Docker socket as a powerful permission
Mounting /var/run/docker.sock gives a container access to the Docker daemon. On a typical rootful host, that can enable actions with host-level impact. Avoid this mount unless the service truly needs to manage containers; a CI job that only builds an image usually has safer builder options. Docker’s rootless mode changes the daemon’s privilege model, but it still needs deliberate configuration.
Publish only the ports you need
EXPOSE documents a container port; it does not publish that port on the host. A -p mapping does. If a local service should only be reachable from the host, bind it to loopback:
Keep databases and debug interfaces on private networks unless external access is part of the design. In particular, do not leave Node’s inspector bound to a public interface in a production deployment.
Make image updates reviewable
Choose a maintained base image, update it regularly, and keep the version or digest visible in code review. A digest makes the exact image content reproducible; it also means updates need an explicit digest change. A version tag is easier to maintain but may move over time. Pick the trade-off deliberately, then rebuild and scan on a regular schedule.
Scanner output needs triage. A finding is a prompt to check the affected package, whether the vulnerable code is present in the shipped image, and whether an upgrade or mitigation is available. A blanket “fail every high or critical finding” rule can help in some pipelines, but it needs an exception and ownership process so teams do not learn to ignore the gate.
My short review list
- Run the service as a non-root user where possible.
- Keep credentials, Git history, and local dependencies out of the build context.
- Use BuildKit secret mounts for build-time credentials and runtime secret injection for app credentials.
- Avoid Docker socket mounts and broad Linux capabilities.
- Publish only the ports the service needs.
- Track base image updates and review scan findings.
Most of these checks are small. The useful habit is to review the runtime permissions and deployment configuration alongside the Dockerfile, because the image alone does not show every permission the container will receive.