Getting an app running in Docker on your laptop takes an afternoon. Getting it to run well in production, month after month, takes a few more decisions. The difference shows up in build times, cloud bills, security scan reports and the number of 2am pages your team receives.
This guide covers the practices we consider the baseline for any container that serves real users. None of them are exotic. Most take minutes to adopt. Together they make images smaller, builds faster and incidents rarer.
Start from a small, trusted base image
Every image inherits everything in its base: packages, libraries and their vulnerabilities. A full operating system image can carry hundreds of packages your app never touches, and each one is something a scanner will flag and an attacker could use.
Pick the smallest base that runs your app reliably:
- Slim variants such as
node:24-slimorpython:3.13-slimdrop documentation, compilers and extras while keeping a familiar Debian userland. They are a safe default. - Alpine images are tiny, but they use musl instead of glibc. Some native modules and Python wheels behave differently or need rebuilding. Test before you commit.
- Distroless or minimal hardened images contain only your runtime and its direct dependencies, with no shell or package manager. They shrink the attack surface further, at the cost of harder debugging inside the container.
Use official or verified publisher images, and pin them. A tag like node:24-slim moves when the maintainers publish updates. For fully repeatable builds, pin to a digest (node:24-slim@sha256:...) and let a tool such as Dependabot or Renovate open pull requests when a new digest is available. You get stability and updates, on your schedule.
Use multi-stage builds
Your build needs compilers, dev dependencies and source files. Your running app needs none of them. Multi-stage builds let you compile in one stage and copy only the output into a clean final stage.
Here is a short example for a Node.js service:
# syntax=docker/dockerfile:1
FROM node:24-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:24-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
The final image has production dependencies and compiled output only. The same pattern works for Go, Java, .NET and Rust, where the gap between build and runtime images is often even larger. A Go service can end up as a single static binary in an image of a few megabytes.
Order layers for caching
Docker builds images in layers and reuses any layer whose inputs have not changed. Once one layer changes, every layer after it is rebuilt. That makes instruction order a performance decision.
Put the things that change least at the top:
- Base image and system packages.
- Dependency manifests (
package.json,requirements.txt,pom.xml). - Dependency install.
- Application source.
- Build step.
In the example above, copying the lock file and running npm ci before COPY . . means a code change does not reinstall every dependency. On a busy team that can turn ten-minute builds into one-minute builds.
Two related habits help:
- Add a
.dockerignorefile. Exclude.git,node_modules, local.envfiles, test output and build artefacts. This shrinks the build context, speeds up uploads to the builder and stops secrets from leaking into images by accident. - Use BuildKit cache mounts for package manager caches, for example
RUN --mount=type=cache,target=/root/.npm npm ci. The cache persists between builds without ending up in the image.
In CI, enable remote layer caching (most CI providers and docker buildx support registry or provider-native cache backends) so that each pipeline run does not start cold.
Keep secrets out of images
Anything you COPY or pass with ARG or ENV during a build can end up in the image or its history. Anyone who can pull the image can read it. This is one of the most common container security mistakes.
Follow three rules:
- Never bake secrets into images. No API keys, database passwords or private certificates in the Dockerfile, the build context or environment variables set at build time.
- Use build secrets when a build needs credentials, for example to pull from a private package registry. BuildKit's
RUN --mount=type=secret,id=npmrcmakes the secret available to that one command and leaves no trace in the layers. - Inject runtime secrets at runtime from a secrets manager such as AWS Secrets Manager, Azure Key Vault or Google Secret Manager, or through your orchestrator's secret mechanism. Rotate them without rebuilding the image.
If a secret has ever been committed to an image that was pushed to a registry, treat it as exposed and rotate it.
Run as a non-root user with least privilege
By default, processes in a container run as root. Container isolation is good, but it is not a guarantee. If an attacker gets code execution in a root container and finds a kernel or runtime flaw, the damage can spread to the host.
Lock things down:
- Add a
USERinstruction so the app runs as an unprivileged user. Many official images ship one (the Node images includenode). - Run with a read-only root filesystem where you can, and mount a writable volume or
tmpfsonly for paths that genuinely need writes. - Drop Linux capabilities you do not need. Most web services need none beyond the defaults, and many need fewer.
- Never run production containers with
--privilegedunless there is a specific, reviewed reason. - Set CPU and memory limits so one misbehaving container cannot starve its neighbours.
Kubernetes, Amazon ECS, Azure Container Apps and Google Cloud Run all let you express these settings in configuration, so they are applied the same way every time.
Handle signals, health and logs properly
Orchestrators start, stop and replace containers constantly. Your app needs to cooperate.
Shut down cleanly. When a container is stopped, the runtime sends SIGTERM to process ID 1 and waits a grace period before sending SIGKILL. Use the exec form of CMD (["node", "server.js"]), not the shell form (node server.js), so your process receives the signal directly instead of a shell swallowing it. Handle SIGTERM in code: stop accepting new requests, finish in-flight ones, close database connections, then exit. If your process cannot reap child processes, run with --init or a tiny init such as tini.
Expose health checks. Provide a cheap endpoint that reports whether the app is alive and whether it is ready to take traffic. Kubernetes uses separate liveness and readiness probes; other platforms have their own equivalents. A readiness check that fails while the app warms up prevents traffic from reaching a container that is not ready.
Log to stdout and stderr. Do not write log files inside the container. The platform collects standard output and ships it to your logging tool. Use structured JSON logs so you can search and filter them later.
One main concern per container. Run the web server in one container and the background worker in another, even if they share an image. They scale, fail and restart independently, which makes both easier to operate.
Scan, sign and tag deliberately
An image that was clean last month may not be clean today, because new vulnerabilities are published every week.
- Scan in CI and in the registry. Tools such as Docker Scout, Trivy and Grype, and the built-in scanners in Amazon ECR, Azure Container Registry and Google Artifact Registry, flag known CVEs. Fail the build on critical issues with a fix available, and track the rest.
- Generate an SBOM and provenance.
docker buildxcan attach a software bill of materials and build provenance attestations to an image. They tell you exactly what is inside and how it was built, which matters when the next widely publicised vulnerability lands and someone asks "are we affected?" - Sign images with a tool such as Sigstore Cosign, and have your platform verify signatures before running them.
- Use immutable, meaningful tags. Tag each build with the Git commit SHA or a release version. Avoid deploying
latest: it tells you nothing about what is running and makes rollbacks guesswork. - Rebuild regularly, even without code changes, to pick up patched base images.
A production readiness checklist
Use this table as a quick review before an image goes live.
| Area | Check |
|---|---|
| Base image | Slim, minimal or distroless; official source; pinned by tag or digest |
| Build | Multi-stage; dependencies installed before source copy; .dockerignore in place |
| Secrets | None in the Dockerfile, build args or layers; runtime secrets from a manager |
| Privileges | Non-root USER; read-only filesystem where possible; no --privileged |
| Resources | CPU and memory limits set |
| Lifecycle | Exec form CMD; handles SIGTERM; liveness and readiness checks |
| Logging | Structured logs to stdout and stderr |
| Supply chain | Scanned in CI; SBOM and provenance attached; signed; tagged by SHA or version |
| Maintenance | Automated base image updates; scheduled rebuilds |
If you can tick every row, your containers are in good shape. If several rows are blank, start with secrets and non-root users, since those carry the most risk.
How EveTek can help
Our cloud and DevOps team can review your Dockerfiles and pipelines against this checklist, then fix what needs fixing on AWS, Azure or Google Cloud. If you want containers kept patched after launch, our maintenance and support retainer covers it. Get in touch to talk it through.



