Writing Dockerfiles
1/8/2025
Writing Dockerfiles
A Dockerfile is a text document that contains all the commands to assemble an image.
Basic Dockerfile
dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Common Instructions
dockerfile
# Base image
FROM python:3.11
# Set working directory
WORKDIR /app
# Copy files
COPY requirements.txt .
COPY . /app
# Run commands
RUN pip install -r requirements.txt
# Environment variables
ENV NODE_ENV=production
# Expose ports
EXPOSE 8000
# Default command
CMD ["python", "app.py"]Building an Image
bash
docker build -t my-app .
docker build -t my-app:v1.0 .Multi-stage Builds
dockerfile
# Build stage
FROM node:18 AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build
# Production stage
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
Monkey Knows Wiki