6

I am using Docker while development. I noticed that I can't launch dev server with docker-compose up command, but can with docker-compose run

Here is my Dockerfile:

FROM python:3.6 WORKDIR /opt/lib RUN pip install --upgrade pip COPY requirements.txt ./ RUN pip install -r requirements.txt WORKDIR /opt/web 

Here is docker-compose.yaml

version: '2' services: web: build: ./web/ working_dir: /opt/web ports: - "3000:3000" volumes: - ./web:/opt/web user: 1000:1000 depends_on: - database env_file: env command: python manage.py runserver 0.0.0.0:3000 database: image: mdillon/postgis:9.6 ports: - "5432:5432" volumes: - ./database/data:/var/lib/postgresql/data 

Now, if I run docker-compose up, only database starts up: enter image description here

But with docker-compose run server starts fine: enter image description here

If I change docker-compose.yml > services > web > command to /usr/local/bin/gunicorn project.wsgi:application -w 4 -b :3000 it also works fine, but I need autorestart when files change enter image description here

I use Docker for MacOS Version 18.03.1-ce-mac65 (24312), Django==1.10

I tried to reset it to factory settings and this did not help.

Can you help me with this?

EDIT 1:

Other manage.py commands, like migrate, work fine

4
  • the ENTRYPOINT in python:3.6 is python and not shell (or bash) so you dont have to run python again, i think you could try just ./manage.py runserver 0.0.0.0:3000 Commented May 8, 2018 at 9:20
  • @MazelTov yep, may be. But this does not help, anyway Commented May 8, 2018 at 9:21
  • what is the output of docker-compose up web? Commented May 8, 2018 at 9:23
  • @MazelTov nothing after Attaching to mein-dach-web_web_1. Seems like it just hangs. docker exec <container id> ps outputs: PID TTY TIME CMD 1 ? 00:00:00 python 51 ? 00:00:02 python 99 ? 00:00:00 ps Commented May 8, 2018 at 9:31

3 Answers 3

11

the ./manage.py runserver requires to Allocate a pseudo-TTY. You can pass it to docker-compose.yml this way

services: web: tty: true command: ./manage.py runserver 0:3000 
Sign up to request clarification or add additional context in comments.

Comments

2

As you mentioned later, you should specify the command in compose file, but with reload option.

Like this:

command: /usr/local/bin/gunicorn project.wsgi:application -w 4 -b :3000 --reload

official docs ref

2 Comments

@atomAltera runserver is a development server and should not be used in production
@Zulu, I need it for development
0

In my case, I faced this problem only on the very first docker-compose up run. It happened because DB initialization took too long. So just press Ctrl-C and run docker-compose up again.

depends_on does not really monitor the DB-server status — it monitors container status. So it starts your web app before the DB is really ready for connection.

And yes, tty: true suggested by Mazel Tov is very useful. Without it, you won't see the Django error message (I don't know why) which would help you to understand the issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.