Production-ready multi-stage Dockerfile and docker-compose
Create a secure, slim multi-stage Dockerfile and a docker-compose stack with database, cache and worker: non-root user, healthchecks, volumes and secrets kept out of the image. Designed for ChatGPT, Claude, Gemini and Microsoft Copilot. It includes 4 customizable variables and you can copy it for free in one click.
Intermediate · Code · Pending editorial review
Prompt
Act as a DevOps engineer experienced in containerizing applications for production.
I want to containerize this application:
- Stack and how it starts: {{stack}}
- Services it needs: {{services}}
- Target environment: {{environment}}
- Server CPU architecture: {{platform}}
Deliverables:
1. A multi-stage `Dockerfile`: dependencies, build and a minimal runtime stage. Pinned base image version, good layer caching, non-root user, `WORKDIR`, `HEALTHCHECK`, proper stop-signal handling, and no build tools in the final image.
2. A `.dockerignore` suited to the stack.
3. A `docker-compose.yml` with every service: networks, named volumes for data, `depends_on` with `condition: service_healthy`, a healthcheck per service, sensible resource limits and a restart policy.
4. Environment variables from a `.env` file (plus a `.env.example` with no real values) and how to pass secrets without baking them into the image.
5. If the environment includes development: a `compose.override.yml` with hot reload and debug ports, separate from the production config.
6. Commands to build, start, follow logs, run migrations and do a multi-platform build if needed.
7. A security and size checklist: what to check with a vulnerability scanner and how to shrink the image.
Constraints:
- Add a short comment to every non-obvious instruction.
- Do not invent dependency versions or variable names: mark unknowns as [FILL IN].
- If my stack needs a decision (e.g., app server or static file collection), ask me first.
Format: one code block per file and a final commands section.How to use it
- State how you start the app locally today (exact command) and which services it uses.
- Choose production only or production plus a dev environment.
- Build the image, check its size and run a vulnerability scanner before pushing it.
Usage example
Stack: Django 5 with Gunicorn · Services: PostgreSQL 16, Redis and a Celery worker · Environment: both · Platform: linux/amd64.
Sample output
1. Dockerfile
# Also pin the patch version or digest: python:3.12.[FILL IN]-slim-bookworm
FROM python:3.12-slim-bookworm AS builder
WORKDIR /app
# Build tools live only in this stage
RUN apt-get update && apt-get install -y --no-install-recommends build-essential libpq-dev \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
# Copying dependencies first keeps the layer cache effective
RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt
FROM python:3.12-slim-bookworm AS runtime
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
WORKDIR /app
# Only the PostgreSQL client library and an unprivileged user
RUN apt-get update && apt-get install -y --no-install-recommends libpq5 \
&& rm -rf /var/lib/apt/lists/* \
&& useradd --create-home --uid 10001 app
COPY --from=builder /wheels /wheels
RUN pip install --no-cache-dir /wheels/* && rm -rf /wheels
COPY --chown=app:app . .
USER app
EXPOSE 8000
# Needs a health endpoint: [FILL IN] the path
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/healthz/')"
# Exec form: Gunicorn is PID 1 and receives SIGTERM directly
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"]
The Celery worker reuses this same image in docker-compose.yml, overriding only command.
Question before going further
Do you serve static files with WhiteNoise or from a separate nginx? That decides whether collectstatic runs at build time or at startup…
Key facts
- Category
- Programming & development · DevOps
- Output type
- Code
- Level
- Intermediate
- Designed for
- ChatGPT, Claude, Gemini and Microsoft Copilot
- Verified in
- Pending editorial verification
- Variables
- Stack and startup, Required services, Target environment and CPU architecture
- Language
- English
- Price
- Free, no sign-up
- Updated
- Nov 7, 2026
Frequently asked questions
What does a multi-stage Dockerfile buy me?
It separates building from running: the final image only carries what the app needs at runtime, so it is smaller and exposes less attack surface.
Why not run the container as root?
If someone exploits a bug in the app, an unprivileged user limits what they can do inside the container and lowers the risk of escaping to the host.
Does depends_on guarantee the database is ready?
Only with `condition: service_healthy` and a real healthcheck on the database. Without it, Compose just waits for the container to start.
What is this prompt for?
Create a secure, slim multi-stage Dockerfile and a docker-compose stack with database, cache and worker: non-root user, healthchecks, volumes and secrets kept out of the image.
Which AI tools does it work with?
It is designed for ChatGPT, Claude, Gemini and Microsoft Copilot. We have not verified it in these tools yet, so results may vary between them.
How do I customize the prompt?
Click “Customize” and fill in the variables (Stack and startup, Required services, Target environment and CPU architecture). The text updates instantly and you can then copy it in one click. Values are stored only in your browser.
Do I need experience to use it?
It is intermediate: it helps to know the topic so you can provide good context and review the result critically.
Is this prompt free?
Yes. Every HomeCasa Prompts prompt is free, with no sign-up or subscription. You can also use them in professional projects.
Can I trust the AI output?
Treat it as a high-quality draft: always check data, figures and important claims before publishing or making decisions.