15 Prompts for Docker: From Dockerfile to Production-Ready Compose

15 Prompts for Docker: From Dockerfile to Production-Ready Compose

Docker is the de facto standard for containerization, but crafting efficient Dockerfiles and compose files often involves repetitive trial and error. AI assistants like ChatGPT, Claude, or GitHub Copilot can cut this process by 50% or more. Below are 15 battle-tested prompts you can copy and adapt to your own projects. Each prompt is grounded in official Docker documentation and real-world usage.

1. Generate a Basic Dockerfile for a Python App

Prompt: "Create a Dockerfile for a Python Flask application using Alpine Linux. The app runs on port 5000. Use a virtual environment and install dependencies from requirements.txt. Ensure the container runs as a non-root user."

Example output:

FROM python:3.12-alpine AS builder
WORKDIR /app
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER appuser
EXPOSE 5000
CMD ["python", "app.py"]

Explanation: This prompt produces a multi-stage-like structure (builder stage) and follows security best practices (non-root user). Always prefer alpine for smaller images.

2. Generate a docker-compose.yml for a Full-Stack App

Prompt: "Create a docker-compose.yml for a Node.js backend, React frontend, PostgreSQL database, and Redis cache. The backend should use environment variables for DB connection. Add health checks for the database."

Example output (abridged):

version: '3.8'
services:
  backend:
    build: ./backend
    environment:
      - DB_HOST=postgres
      - REDIS_HOST=redis
    depends_on:
      postgres:
        condition: service_healthy
  postgres:
    image: postgres:15
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
  redis:
    image: redis:7

Tip: Use condition: service_healthy to avoid race conditions – a pattern documented in the Docker Compose specification.

3. Optimize an Existing Dockerfile for Size

Prompt: "Here is my Dockerfile: [paste]. Reduce the final image size by combining RUN layers, cleaning up package manager caches, and switching to a distroless base image. Show the optimized version."

Why it works: This prompt forces the AI to apply known optimization techniques like layer caching and using --no-install-recommends. The official Docker best practices guide recommends minimizing layers.

4. Debug a Container That Exits Immediately

Prompt: "My Docker container exits right after starting. I run 'docker run myapp' and it returns without any logs. How can I debug this? Write a docker-compose override that keeps the container alive with 'tail -f /dev/null' and enables interactive shell access."

Solution example:

services:
  myapp:
    command: ["tail", "-f", "/dev/null"]
    stdin_open: true
    tty: true

Then attach with docker exec -it.

5. Create a Multi-Stage Build for a Go Application

Prompt: "Write a multi-stage Dockerfile for a Go web server. First stage compiles the binary with CGO_ENABLED=0. Second stage copies the binary into a scratch image. Ensure the binary is statically linked."

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 .

FROM scratch
COPY --from=builder /app/server /
EXPOSE 8080
CMD ["/server"]

Reference: The official Go Docker guide recommends this pattern for minimal images (~10 MB).

6. Set Up a Development Docker Compose with Hot Reload

Prompt: "Create a docker-compose.override.yml for local development that mounts source code as volumes, sets environment variables for debug mode, and uses a different entrypoint that enables file watchers."

Example:

version: '3.8'
services:
  backend:
    volumes:
      - ./backend:/app
    environment:
      - DEBUG=true
    command: npx nodemon index.js

This pattern is widely used in the Docker community (see Docker's samples on GitHub).

7. Generate a .dockerignore File

Prompt: "Generate a .dockerignore for a Node.js project. Exclude node_modules, .git, .env, logs, and any generated build artifacts. Use .dockerignore spec."

node_modules
.git
.env
npm-debug.log*
build/
dist/

Why: A proper .dockerignore can reduce build context by 90% and speed up image transfers.

8. Dockerfile with Install for System Dependencies

Prompt: "Write a Dockerfile for a Ruby on Rails app that requires libpq-dev for the pg gem. Use a multi-stage build to keep the final image clean. Install build dependencies only in the builder stage."

Explanation: This prompt teaches the pattern of separating build-time and runtime dependencies – a core concept from the Dockerfile best practices guide.

9. Create a Production-Ready NGINX + React Setup

Prompt: "Generate a multi-stage Dockerfile for a React app. In the first stage, build the static files with Node. The second stage serves them with an NGINX Alpine image. Use a custom nginx.conf with gzip and caching headers."

Output includes a default.conf with:

gzip on;
location / {
    root   /usr/share/nginx/html;
    try_files $uri /index.html;
}

This is the standard approach used by Create React App documentation.

10. Troubleshoot Docker Networking

Prompt: "I have two containers: 'app' and 'db'. App cannot connect to db on hostname 'postgres'. Show me how to debug using docker exec and network tools. Then provide a working docker-compose.yml with explicit network config."

Sample debug commands:

docker exec -it app sh
# inside container
ping postgres
dig postgres

Solution: Ensure both services are on the same network by defining networks: or using top-level networks: key.

11. Generate a Dockerfile for a Java Spring Boot App

Prompt: "Create a Dockerfile for a Spring Boot JAR application. Use a multi-stage build: first stage with Maven to compile, second stage with Eclipse Temurin runtime. Use a layer index for faster rebuilds."

Example:

FROM maven:3.9 AS build
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src src
RUN mvn package -DskipTests

FROM eclipse-temurin:21-jre
COPY --from=build target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]

Reference: Spring Boot official documentation recommends JRE layering.

12. Use Docker Secrets in Compose

Prompt: "Write a docker-compose.yml that uses Docker secrets to pass a database password to a Python app. Define the secret in the compose file and mount it as a file inside the container."

secrets:
  db_password:
    file: ./secrets/db_password.txt

services:
  app:
    secrets:
      - db_password
    environment:
      - DB_PASSWORD_FILE=/run/secrets/db_password

Note: This is the recommended way to handle sensitive data as per Docker security documentation.

13. Automate Cleanup of Unused Docker Resources

Prompt: "Write a shell script that removes dangling images, stopped containers, and unused volumes older than 24 hours. Use docker system prune but with custom filters. Add a dry-run mode."

#!/bin/bash
docker container prune --force --filter "until=24h"
docker image prune --force --filter "until=24h"

Best practice: Attach this to a cron job to keep disk usage under control.

14. Convert Docker Run Command to Docker Compose

Prompt: "Convert the following docker run command to a docker-compose.yml: 'docker run -d --name myapp -p 3000:3000 -e NODE_ENV=production myapp:latest'. Include restart policy and logging driver."

Output:

version: '3.8'
services:
  myapp:
    image: myapp:latest
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    restart: unless-stopped
    logging:
      driver: json-file

Why: This prompt is extremely useful when migrating from manual docker run to orchestration.

15. Validate Dockerfile Best Practices with Hadolint

Prompt: "Write a GitHub Actions workflow that runs hadolint on every push to check the Dockerfile for common mistakes (e.g., missing version pinning, using latest tag). Have it fail the build on errors."

name: Lint Dockerfile
on: [push]
jobs:
  hadolint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hadolint/hadolint-action@v3.1.0
        with:
          dockerfile: Dockerfile
          failure-threshold: error

Source: Hadolint is listed in Docker's official IDE integrations.

Conclusion

These 15 prompts cover the most common Docker scenarios – from writing clean Dockerfiles to debugging networking and automating cleanup. By feeding these templates into your preferred AI assistant, you can focus more on your application logic and less on plumbing. Bookmark this page and tailor each prompt to your stack. For further reading, consult the Dockerfile reference and Compose specification.

← All posts

Comments