Build from Dockerfile
Build an image from the Dockerfile in the current directory.
docker build -t myapp:dev .By default Docker reads `./Dockerfile` and sends the current directory as build context.
High-value Dockerfile instructions, patterns, and build flags for reproducible and efficient image builds.
Core build invocation and essential Dockerfile instructions.
Build an image from the Dockerfile in the current directory.
docker build -t myapp:dev .By default Docker reads `./Dockerfile` and sends the current directory as build context.
docker build -f Dockerfile.prod -t myapp:prod .Common for environment-specific builds.
Set the base image for a Dockerfile stage.
FROM node:20-alpineEvery stage starts from a `FROM` instruction.
Set the working directory for subsequent instructions.
WORKDIR /appAvoid repetitive `cd` logic and make paths explicit.
Copy files from the build context into the image.
COPY package*.json ./Use `.dockerignore` to keep build contexts lean.
RUN npm ciCommon for dependency installation and image preparation.
Set the default command when the container starts.
CMD ["node", "server.js"]Can be overridden at runtime.
ENTRYPOINT ["/usr/local/bin/app"]Often paired with `CMD` for default arguments.
Document the port the application listens on.
EXPOSE 3000This does not publish ports by itself.
Set environment variables in the image.
ENV NODE_ENV=productionValues become defaults at runtime unless overridden.
ARG NODE_VERSION=20Build args are available only during build unless copied into `ENV`.
Attach metadata labels to the image.
LABEL org.opencontainers.image.source="https://github.com/acme/myapp"Useful for provenance, ownership, and automation.
Repeatable Dockerfile patterns for efficient, secure images.
Build artifacts in one stage and copy only runtime output into the final stage.
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY /app/dist /usr/share/nginx/htmlMulti-stage builds keep runtime images smaller and cleaner.
RUN addgroup -S app && adduser -S app -G app
USER appA common hardening practice for production images.
Define a health check command for the container.
HEALTHCHECK CMD wget -qO- http://localhost:3000/health || exit 1Useful with orchestrators and compose health status.
COPY . /appAvoids extra `chown` layers in many images.
Copy dependency manifests before application code to maximize layer cache reuse.
COPY package*.json ./
RUN npm ci
COPY . .A classic Dockerfile optimization for JS apps.
Exclude unnecessary files from build context.
node_modules
dist
.git
.env
coverageReducing context size speeds up builds and avoids leaking sensitive files.
Common build options used with Dockerfiles.
docker build --target build -t myapp:build .Useful for debugging or exporting builder-only images.
docker build --build-arg NODE_VERSION=20 -t myapp:dev .Great for version pinning and parameterized builds.
docker build --no-cache -t myapp:clean .Useful when cache corruption or stale dependencies are suspected.
Attempt to pull a newer base image before build.
docker build --pull -t myapp:dev .Good for CI pipelines that want refreshed base layers.
Build for a target platform when supported.
docker build --platform linux/amd64 -t myapp:amd64 .Often used together with Buildx or multi-platform workflows.
docker build --progress=plain -t myapp:dev .Helpful when debugging failing builds in CI.