Basic Dockerfile Structure

FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "server.js"]

.dockerignore File

node_modules
dist
.env
.git
*.log

Build and Run the Image

docker build -t myapp .
docker run -d -p 3000:3000 myapp

Key Points

  • Separating COPY package*.json ./ and RUN npm ci into two steps enables layer caching
  • Without .dockerignore, node_modules gets copied and the image becomes huge

If you want to auto-deploy the image built from a Dockerfile via CI/CD, combining it with GitHub Actions: How to Set Up Basic Auto-Deploy will greatly improve efficiency.

Looking for reliable cloud infrastructure? Check out these developer-friendly services.