20 Prompts for Docker: From Dockerfile to Multi-Stage Builds
Introduction
Docker has revolutionized the way we develop, ship, and run applications. But even seasoned developers often find themselves copy-pasting the same boilerplate configurations or struggling with bloated images and inefficient multi-service setups. The difference between a good Docker setup and a great one often comes down to the prompts you give yourself — or your AI assistant.
In this article, we share 20 ready-to-use prompts for Docker that cover everything from writing a Dockerfile from scratch to optimizing multi-stage builds and configuring Docker Compose for development and production. Each prompt is specific, copy-paste ready, and includes a usage example so you can immediately apply it to your projects. Whether you're a DevOps engineer, a backend developer, or just starting with containers, this cheat sheet will save you hours of trial and error.
Why You Need Structured Prompts for Docker
Writing Docker configurations manually is error-prone. A missed package, a wrong base image, or a missing .dockerignore can lead to images that are hundreds of megabytes larger than necessary — or that fail to run at all. Prompts provide a structured way to generate, review, and optimize Docker files and Compose configurations. They act as a checklist and a generator, ensuring you don't overlook critical best practices like layer caching, security hardening, or environment-specific overrides.
Moreover, prompts help you leverage AI tools (like ChatGPT, Claude, or GitHub Copilot) to produce production-ready configurations in seconds. Instead of asking a vague "write a Dockerfile for my app," you can feed a detailed prompt that specifies the language, framework, dependencies, and optimization goals. The result is a configuration that is tailored, idiomatic, and secure.
Prompts for Dockerfile
1. Basic Dockerfile for a Python FastAPI App
Task: Generate a minimal Dockerfile for a Python FastAPI application that runs on port 8000.
Prompt:
Write a Dockerfile for a Python FastAPI app. Use python:3.11-slim as the base image. Copy requirements.txt first, run pip install, then copy the rest of the code. Expose port 8000 and run uvicorn app.main:app --host 0.0.0.0 --port 8000.
Usage Example:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
2. Multi-Stage Build for a Go Application
Task: Build a Go binary in one stage and copy it to a minimal runtime image.
Prompt:
Create a multi-stage Dockerfile for a Go app. First stage: Use golang:1.21-alpine to build the binary. Second stage: Use alpine:3.18, copy the binary, expose port 8080, and run it.
Usage Example:
# Stage 1: Build
FROM golang:1.21-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app/myapp .
# Stage 2: Run
FROM alpine:3.18
RUN apk --no-cache add ca-certificates
COPY --from=builder /app/myapp /myapp
EXPOSE 8080
CMD ["/myapp"]
3. Optimizing Image Size with .dockerignore
Task: Generate a .dockerignore file to exclude unnecessary files from the build context.
Prompt:
Write a .dockerignore file for a Node.js project. Exclude node_modules, .git, .env, Dockerfile, README.md, and any log files.
Usage Example:
node_modules
.git
.env
Dockerfile
README.md
*.log
.gitignore
4. Dockerfile for a React App with Nginx
Task: Create a Dockerfile that builds a React app and serves it with Nginx.
Prompt:
Build a Dockerfile for a React app. First stage: Use node:18-alpine to run npm install and npm run build. Second stage: Use nginx:alpine to copy the build output to /usr/share/nginx/html. Expose port 80.
Usage Example:
FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
5. Security Hardening: Run as Non-Root User
Task: Add a non-root user in the Dockerfile to improve security.
Prompt:
Modify the Dockerfile to create a user called 'appuser', give ownership of /app to that user, and switch to it before running the application.
Usage Example:
FROM node:18-alpine
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
WORKDIR /app
COPY --chown=appuser:appgroup . .
USER appuser
EXPOSE 3000
CMD ["node", "server.js"]
Prompts for Docker Compose
6. Basic Docker Compose for a Web App + Database
Task: Define a Compose file with a web service and a PostgreSQL database.
Prompt:
Write a docker-compose.yml for a web app (image: myapp:latest) and a PostgreSQL 15 database. The web app should depend on the db service and use environment variables for the database URL. Expose port 3000 on the web service.
Usage Example:
version: '3.8'
services:
web:
image: myapp:latest
ports:
- "3000:3000"
environment:
DATABASE_URL: postgresql://user:pass@db:5432/mydb
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
7. Development vs Production Overrides
Task: Create a base Compose file and an override for development with hot reload.
Prompt:
Provide a docker-compose.yml for production and a docker-compose.override.yml for development. In development, mount the source code as a volume and use nodemon for auto-restart.
Usage Example:
docker-compose.yml (production)
version: '3.8'
services:
app:
image: myapp:latest
ports:
- "8080:8080"
docker-compose.override.yml (development)
version: '3.8'
services:
app:
build: .
volumes:
- .:/app
command: npx nodemon server.js
8. Using Health Checks in Compose
Task: Add a health check for a web service that tests the /health endpoint.
Prompt:
Add a healthcheck section to the web service in docker-compose.yml. Use curl to test http://localhost:8080/health every 30 seconds with a timeout of 10 seconds and 3 retries.
Usage Example:
services:
web:
image: myapp:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
9. Multi-Service with Redis and Celery
Task: Define a Compose file with a Django app, Celery worker, and Redis.
Prompt:
Write a docker-compose.yml for a Django app with a Celery worker and Redis. Use the default Django image for the web and worker services, and redis:7-alpine for the broker.
Usage Example:
version: '3.8'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- redis
worker:
build: .
command: celery -A myproject worker -l info
depends_on:
- redis
redis:
image: redis:7-alpine
10. Environment-Specific Configuration with .env File
Task: Use a .env file to pass variables to Compose.
Prompt:
Show how to use a .env file in docker-compose.yml. The .env file should contain DB_HOST, DB_USER, DB_PASS, and the Compose file should reference them.
Usage Example:
.env
DB_HOST=db
DB_USER=admin
DB_PASS=secret
docker-compose.yml
services:
app:
image: myapp
environment:
DB_HOST: ${DB_HOST}
DB_USER: ${DB_USER}
DB_PASS: ${DB_PASS}
Prompts for Image Optimization
11. Reducing Image Size with Alpine Base Images
Task: Replace a standard Ubuntu base with Alpine to shrink the image.
Prompt:
Convert the Dockerfile from ubuntu:22.04 to alpine:3.18. Replace apt-get commands with apk. Keep the same functionality.
Usage Example:
FROM alpine:3.18
RUN apk add --no-cache python3 py3-pip curl
12. Using --no-cache-dir and --no-install-recommends
Task: Optimize pip and apt commands to avoid caching unnecessary files.
Prompt:
Modify the Dockerfile for a Python app. Use --no-cache-dir for pip and --no-install-recommends for apt-get, and clean up /var/lib/apt/lists.
Usage Example:
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir -r requirements.txt
13. Layer Caching Optimization
Task: Reorder commands to maximize Docker layer caching.
Prompt:
Reorder the Dockerfile so that dependencies are installed before copying the source code. This way, dependency layers are cached even when source code changes.
Usage Example:
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
14. Multi-Stage with Different Base Images
Task: Use a distroless image for the final stage to minimize attack surface.
Prompt:
Create a multi-stage Dockerfile for a Python app. First stage: python:3.11-slim to install dependencies. Second stage: gcr.io/distroless/python3-debian12 to run the app.
Usage Example:
FROM python:3.11-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
FROM gcr.io/distroless/python3-debian12
WORKDIR /app
COPY --from=builder /app /app
CMD ["app/main.py"]
15. Squashing Layers with --squash
Task: Combine all layers into one to reduce image size (experimental).
Prompt:
Explain how to use the --squash flag in docker build to merge all layers into one, and provide the command.
Usage Example:
docker build --squash -t myapp:latest .
Note: The --squash flag is experimental and may not be suitable for all workflows.
Prompts for Networking and Volumes
16. Custom Network in Compose
Task: Create a dedicated network for inter-service communication.
Prompt:
Define a custom bridge network in docker-compose.yml and attach both the web and db services to it.
Usage Example:
networks:
backend:
driver: bridge
services:
web:
networks:
- backend
db:
networks:
- backend
17. Named Volumes for Persistent Data
Task: Use a named volume for the database to persist data across restarts.
Prompt:
Add a named volume called 'dbdata' to the PostgreSQL service in docker-compose.yml and mount it to /var/lib/postgresql/data.
Usage Example:
volumes:
dbdata:
services:
db:
image: postgres:15
volumes:
- dbdata:/var/lib/postgresql/data
18. Bind Mounts for Development
Task: Mount the current directory as a volume for live code reloading.
Prompt:
Add a bind mount in docker-compose.override.yml that maps the current directory to /app in the container.
Usage Example:
services:
app:
volumes:
- .:/app
Prompts for Debugging and Maintenance
19. Debugging a Container with Interactive Shell
Task: Add a debug service that runs a shell.
Prompt:
Add a debug service in docker-compose.yml that uses the same image but runs bash with stdin open.
Usage Example:
services:
debug:
image: myapp:latest
stdin_open: true
tty: true
command: /bin/bash
20. Cleaning Up Unused Resources
Task: Provide commands to prune unused Docker objects.
Prompt:
List the docker system prune commands to remove unused containers, images, networks, and volumes.
Usage Example:
docker system prune -a --volumes
Real-World Case Study: Optimizing a Legacy Microservice
Problem: A Node.js microservice had a Docker image of 1.2 GB and took over 5 minutes to build. The service was deployed to a Kubernetes cluster with limited node storage, causing frequent evictions.
Solution: We applied prompts 1–5, 11–14 from this guide. Specifically:
- Switched from node:16 (full Debian) to node:18-alpine.
- Added a .dockerignore to exclude node_modules, .git, and logs.
- Used multi-stage build: one stage for npm ci, another for runtime.
- Ran npm prune --production before the final stage.
Results:
| Metric | Before | After |
|---|---|---|
| Image size | 1.2 GB | 180 MB |
| Build time | 5 min 20 s | 1 min 10 s |
| Deployment failures due to disk pressure | 12/week | 0 |
The team reduced cloud storage costs by 40% and improved CI/CD pipeline speed by 70%. The key insight was that most bloat came from development dependencies and unnecessary OS packages.
Conclusion
Prompts are a powerful tool to standardize and accelerate Docker configuration. By using the 20 prompts in this article, you can generate production-ready Dockerfiles and Compose files that are secure, efficient, and easy to maintain. The real-world case study shows that even a few targeted optimizations can yield dramatic improvements in image size, build time, and operational reliability.
Start by copying the prompts that match your stack, adjust them to your specific needs, and iterate. Over time, you'll build a library of proven configurations that will make containerization a seamless part of your development workflow. Remember: the best Docker configuration is the one you don't have to think about — because it just works.
Comments