0

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
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • In addition to the builtin Django development server not generally being suitable for production systems, it doesn't behave correctly in Docker containers. Running stuff as root is also not best practice. I hope you are only doing this to play around and not putting it on the Internet. http://blog.dscpl.com.au/2015/12/issues-with-running-as-pid-1-in-docker.html http://blog.dscpl.com.au/2015/12/don-run-as-root-inside-of-docker.html – Graham Dumpleton Aug 06 '16 at 06:48
  • @GrahamDumpleton This is just step 1 and it's all on my laptop. It will eventually be a uwsgi/nginx setup after I work out all of those details. Thanks for the notes. – Dan Lowe Aug 06 '16 at 13:44

1 Answers1

3

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.

Community
  • 1
  • 1
Matt
  • 68,711
  • 7
  • 155
  • 158