Dan Lowe Dan Lowe - 3 months ago 24
Python Question

Django app server hangs / won't start in Docker Compose

I am trying to launch a straightforward Django app server in Docker Compose, paired with a Postgres container. It goes through as I would expect, launching the entrypoint script, but it never seems to actually run the Django app server (which should be the last step, and remain running).

I know it runs the entrypoint script, because the migrate step is run. The app server never outputs any of the expected output, and port 8000 never responds.

I am using Docker for Mac (stable), if it matters.

Dockerfile for my Django app container:

FROM ubuntu:16.04

COPY my_app /my_app

RUN apt-get update \
&& apt-get install -y python3 python3-psycopg2 python3-pip

RUN apt-get install -y nodejs npm

WORKDIR /my_app
RUN pip3 install -r requirements.txt
RUN npm install bower
RUN python3 manage.py bower install
RUN python3 manage.py collectstatic --no-input

EXPOSE 8000

COPY entrypoint.sh /
RUN chmod 755 /entrypoint.sh

CMD python3 manage.py runserver 0.0.0.0:8000
ENTRYPOINT ["/entrypoint.sh"]


Django entrypoint script:

#!/bin/sh

# Allow database container to start up or recover from a crash
sleep 10

cd /my_app

# Run any pending migrations
python3 manage.py migrate

exec $@


docker-compose.yml:

version: '2'
services:
db:
image: postgres:9.6
volumes:
- ./db/pgdata:/pgdata
environment:
- POSTGRES_USER=my_user
- POSTGRES_PASSWORD=my_password
- PGDATA=/pgdata
- POSTGRES_DB=my_database
appserver:
image: my-image
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8000:8000'
environment:
- POSTGRES_USER=my_user
- POSTGRES_PASSWORD=my_password
- POSTGRES_DB=my_database
links:
- db
depends_on:
- db

Answer

Use the exec form for CMD in your Dockerfile

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

The entrypoint.sh scripts exec is currently trying to run:

/bin/sh -c python3 manage.py runserver 0.0.0.0:8000

Which doesn't seem to work, I think it's just running python3.

You should quote the positional parameters variable so the shell maintains each parameter, even if there are spaces.

exec "$@"

But it's best not to have sh in between docker and your app, so always use the exec form for a CMD.

Comments