📧 chinmayeepavi@gmail.com Support

Best Practices

Use official base images, and pin them to a specific tag rather than latest. A build that depends on latest is a build that is not reproducible.

Prefer small base images. An Alpine or slim variant often cuts image size by an order of magnitude, which speeds every pull in every environment.

Use multi-stage builds so that compilers and build tooling do not ship to production:

FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:1.27-alpine
COPY --from=build /app/build /usr/share/nginx/html

Order instructions from least to most frequently changed. Copy dependency manifests and install dependencies before copying source code, so that editing a source file does not invalidate the dependency layer.

Always add a .dockerignore. Without it the whole working directory is sent to the daemon, including node_modules, .git, and any .env — which may then be baked into a published image.

Run as a non-root user. Add a USER instruction. Containers default to root, and root in the container is root on the host kernel.

Keep one concern per container. A container should run a single process that stays in the foreground. Use Compose or an orchestrator to assemble several.

Never bake secrets into images. Pass them as environment variables at run time, or mount them. Anything in a layer is visible via docker history.

Set resource limits in production. An unconstrained container can exhaust host memory.

Use volumes for anything that must survive the container. Treat containers themselves as disposable.

Combine related RUN commands to reduce layer count, and clean up in the same layer that created the mess:

RUN apt-get update \
 && apt-get install -y --no-install-recommends curl \
 && rm -rf /var/lib/apt/lists/*

Add a HEALTHCHECK so the runtime can tell the difference between a container that is running and an application that is working.