A container exits immediately with code 0 — diagnose the startup problem.
A service container starts and stops right away. docker ps -a shows it Exited (0) seconds after launch, and docker logs prints one build banner and nothing else. Here is the Dockerfile and the observed state.
FROM python:3.12-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "migrate"]
$ docker ps -a
CONTAINER ID IMAGE COMMAND STATUS
a1b2c3d4e5f6 svc "python manage.py mig…" Exited (0) 3 seconds ago
$ docker logs a1b2c3d4e5f6
Operations to perform: ... OK
Explain why it exits and what to change so the container keeps running.
Exit code 0 means the main process finished successfully — it did not crash. The CMD runs a one-shot migration that completes and returns, and a container lives only as long as its PID 1. Make PID 1 the long-running server and run the migration as a separate init step.
- ✗Reading exit 0 as a crash rather than the main process finishing cleanly
- ✗Making a one-shot command the container's PID 1 instead of a long-running server
- ✗Trying to keep it alive with sleep instead of running the actual service as CMD
- →How would exit code 127 instead of 0 change your diagnosis?
- →Where should the migration run in a Kubernetes deployment — init container or Job?
A container lives exactly as long as its PID 1 process. Here PID 1 is python manage.py migrate: a one-shot command that completes successfully and returns 0. So the container stops normally — this is not a crash.
Make the main command a long-running server and move the migration to a separate step (an init container, a Job, or a pre-start hook), not the service's CMD.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
# PID 1 is the long-running server, not the one-shot migration.
CMD ["gunicorn", "app.wsgi", "--bind", "0.0.0.0:8000"]