Loading...
Loading...
Docker and container image best practices including multi-stage builds, security hardening, layer optimization, and Alpine/slim variants. Use when writing or reviewing Dockerfiles, container configurations, or docker-compose files.
npx skill4agent add rory-data/copilot docker-best-practicesv1.2.3node:18-alpinelatest# GOOD: Optimise for caching
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./ # Cache-friendly: deps change less
RUN npm ci --only=production
COPY . . # App code changes most# Build stage
FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage
FROM node:18-alpine AS production
WORKDIR /app
COPY /app/dist ./dist
COPY /app/package*.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/main.js"]# Create and use non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
USER nextjs# Combine commands to reduce layers and clean up
RUN apk add --no-cache \
python3 \
py3-pip \
&& pip install --no-cache-dir flask \
&& apk del build-dependencies# Pin specific versions for reproducibility
FROM python:3.11.5-slim
RUN pip install flask==2.3.3FROM python:3.11-slim AS build
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
FROM python:3.11-slim AS production
WORKDIR /app
COPY /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY . .
USER nobody
EXPOSE 8000
CMD ["python", "app.py"]FROM node:18-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
FROM node:18-alpine AS production
WORKDIR /app
COPY /app/node_modules ./node_modules
COPY . .
USER node
EXPOSE 3000
CMD ["node", "server.js"]FROM golang:1.21-alpine AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o main .
FROM scratch AS production
COPY /app/main /main
EXPOSE 8080
CMD ["/main"]node_modules
.git
.gitignore
README.md
.env
.nyc_output
coverage
.vscodeHEALTHCHECK \
CMD curl -f http://localhost:3000/health || exit 1services:
app:
image: myapp:latest
deploy:
resources:
limits:
memory: 512M
cpus: "0.5"FROM ubuntu:latest # Use specific versions
RUN apt-get update # Combine with install
COPY . . # Do this after deps
RUN apt-get install -y curl # Separate command
ADD https://example.com/file.tar.gz # Use COPY + RUNFROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
curl \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:18-alpine AS production
WORKDIR /app
COPY /app/dist ./dist
RUN addgroup -g 1001 nodejs && adduser -S -u 1001 nextjs
USER nextjs
EXPOSE 3000
CMD ["node", "dist/server.js"]