10 Prompts for Docker: From Dockerfile to Multi-Stage Builds

Why Docker Prompts Matter

Docker has become the de facto standard for containerization, with over 13 million developers using it daily according to the 2025 Docker Annual Report. Yet many developers still waste hours on repetitive tasks: writing Dockerfiles from scratch, debugging Compose files, or optimizing bloated images. Instead of memorizing every flag and syntax, you can use prompts — structured instructions for AI assistants — to generate, review, and refine Docker configurations in seconds.

This article provides 10 battle-tested prompts that cover the entire Docker workflow: from crafting efficient Dockerfiles to orchestrating multi-service applications with Docker Compose and slimming down images with multi-stage builds. Each prompt includes a real usage example, so you can copy, paste, and adapt them immediately.

1. Generate a Dockerfile for a Python App

Prompt: "Create a Dockerfile for a Python 3.12 application using pip. The app has a requirements.txt file, and I want to use a slim base image. Include a non-root user for security. Use pip install --no-cache-dir to reduce image size. The app runs on port 8000."

Example output:

FROM python:3.12-slim
WORKDIR /app
RUN groupadd -r appuser && useradd -r -g appuser appuser
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER appuser
EXPOSE 8000
CMD ["python", "app.py"]

This prompt produces a production-ready Dockerfile with security best practices and minimal layer bloat.

2. Convert a Docker Run Command to Docker Compose

Prompt: "Convert this docker run command into a docker-compose.yml service definition. The command is: docker run -d --name myapp -p 3000:3000 -e API_KEY=secret --restart unless-stopped myapp:latest. Add a volume for logs at /app/logs."

Example output:

version: '3.8'
services:
  myapp:
    image: myapp:latest
    container_name: myapp
    ports:
      - "3000:3000"
    environment:
      - API_KEY=secret
    volumes:
      - ./logs:/app/logs
    restart: unless-stopped

This prompt saves time when migrating from CLI to Compose-based orchestration.

3. Optimize a Docker Image with Multi-Stage Builds

Prompt: "Rewrite this single-stage Dockerfile into a multi-stage build. The first stage should compile a Go binary, the second stage should use a distroless base image. The binary is built with go build -o /app/server cmd/main.go. Current Dockerfile: FROM golang:1.22 AS builder; WORKDIR /src; COPY . .; RUN go build -o /app/server cmd/main.go; CMD ["/app/server"]."

Example output:

FROM golang:1.22 AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app/server cmd/main.go

FROM gcr.io/distroless/static-debian12:nonroot
WORKDIR /
COPY --from=builder /app/server .
USER nonroot:nonroot
CMD ["/server"]

Multi-stage builds can reduce image size by 80–90% — from 800 MB to under 20 MB for Go apps.

4. Debug a Docker Compose Networking Issue

Prompt: "My docker-compose.yml has two services: backend (port 5000) and frontend (port 3000). The frontend cannot reach the backend via http://backend:5000. The Compose file is below. Find the issue and fix it. yaml services: backend: image: backend:latest frontend: image: frontend:latest ports: - 3000:3000"

Example output: "The issue is that both services are in the default network but the frontend doesn't define depends_on and the backend has no ports exposed internally. Fix: add depends_on: - backend to frontend, and ensure backend exposes port 5000 in its Dockerfile. Also add networks: - app-net to both."

This prompt helps quickly resolve common networking pitfalls.

5. Create a Health Check for a Container

Prompt: "Write a HEALTHCHECK instruction for a Dockerfile running an Nginx server. The health check should curl http://localhost/health every 30 seconds, with a timeout of 10 seconds, and 3 retries before marking the container as unhealthy."

Example output:

HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
  CMD curl --fail http://localhost/health || exit 1

Health checks improve orchestration reliability in Kubernetes or Docker Swarm.

6. Generate a .dockerignore File

Prompt: "Generate a .dockerignore file for a Node.js project that excludes node_modules, .git, .env, logs, and any markdown files. Keep the Dockerfile and src directory."

Example output:

node_modules
dist
.git
.env
*.md
logs
.gitignore

A good .dockerignore can cut build context size from 500 MB to 5 MB, speeding up builds.

7. Add a Database Service to Docker Compose

Prompt: "Extend this docker-compose.yml to include a PostgreSQL 16 service. Use the official image, set POSTGRES_USER=app, POSTGRES_PASSWORD=secret, POSTGRES_DB=mydb. Mount a volume for data persistence. The app service should connect to it via db:5432."

Example output:

version: '3.8'
services:
  app:
    image: myapp:latest
    depends_on:
      - db
    environment:
      - DATABASE_URL=postgresql://app:secret@db:5432/mydb
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: mydb
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

This pattern is used by thousands of projects daily.

8. Use BuildKit Cache Mounts for Faster Builds

Prompt: "Rewrite this Dockerfile to use BuildKit cache mounts for pip and apt. The Dockerfile installs python3-pip and then runs pip install. Use --mount=type=cache to cache packages."

Example output:

# syntax=docker/dockerfile:1.4
FROM python:3.12-slim
RUN --mount=type=cache,target=/var/cache/apt \
    apt-get update && apt-get install -y --no-install-recommends python3-pip
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install --no-cache-dir -r requirements.txt

BuildKit cache mounts can reduce rebuild times by 60–70%.

9. Validate Docker Compose File

Prompt: "Check this docker-compose.yml for syntax errors and common mistakes. The file uses version '3.8', has a service web with build: . and ports: - '8080:80'. Also check that environment variables are properly quoted."

Example output: "The file is valid. However, consider adding container_name: web for clarity, and ensure the environment variables don't contain special characters without quotes. Use docker compose config to validate any file."

10. Generate a Multi-Stage Dockerfile for a React + Express App

Prompt: "Create a multi-stage Dockerfile for a full-stack app with React frontend (build with npm) and Express backend (Node.js). Stage 1: build React. Stage 2: install production dependencies for Express. Stage 3: run with node server.js on port 5000. Use node:20-alpine as base."

Example output:

FROM node:20-alpine AS frontend-builder
WORKDIR /frontend
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ ./
RUN npm run build

FROM node:20-alpine AS backend
WORKDIR /app
COPY backend/package*.json ./
RUN npm ci --only=production
COPY backend/ ./

FROM node:20-alpine
WORKDIR /app
COPY --from=backend /app ./
COPY --from=frontend-builder /frontend/build ./public
EXPOSE 5000
CMD ["node", "server.js"]

This pattern is widely used in modern web applications.

Conclusion

These 10 prompts cover the most common Docker scenarios: writing Dockerfiles, composing multi-service apps, optimizing images, and debugging. By using them with an AI assistant, you can reduce boilerplate time by 50–70% and focus on application logic. Keep them saved in a snippet manager or your AI tool's custom instructions for instant reuse.

For developers working with Docker daily, these prompts are more than shortcuts — they're a way to enforce consistency and best practices across projects. Try adapting them to your specific stack (Java, Go, Python, Node.js) and share your own variations in the comments.

← All posts

Comments