Skip to content
Docker

Writing a Dockerfile

Build an image from a Dockerfile with multi-instruction layers.

By EZ4Code Team
dockerfilebuildimage

Code

# Dockerfile
FROM node:20-alpine AS base
WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY . .
RUN npm run build

EXPOSE 3000
CMD ["node", "dist/server.js"]

# Build and tag
# docker build -t myapp:1.0 .
# docker history myapp:1.0   # inspect layers

Explanation

Each instruction in a Dockerfile creates a layer; ordering COPY/RUN to leverage the build cache speeds up rebuilds. FROM sets the base image, WORKDIR sets the working directory, and CMD defines the default command. EXPOSE documents the port; it does not publish it.

More Docker Snippets