13

I'm writing a Dockerfile which needs to run multiple commands as part of the CMD instruction and I thought the right way to do this would be to run a shell script with the main daemon executed via exec. Unfortunately, as part of that process some of my output (stdout? stderr? I don't know, and I don't know how to find out) gets lost.

Here's the shell script:

#!/bin/sh

python manage.py migrate
exec python manage.py runserver 0.0.0.0:8000

The idea being that the migrate command is just run once and its output shown, and then the runserver command should take over and the container runs until that process exits.

The actual problem is that the output of migrate is displayed correctly, but the immediate output of runserver is not shown. Strangely, later request logging of runserver is shown just fine.

To clarify, here's the output I expected:

[...]
No migrations to apply.
[...]
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[21/Jan/2015 16:27:06] "GET / HTTP/1.1" 200 15829

Here's what I'm getting with fig up:

[...]
No migrations to apply.
[...]
[21/Jan/2015 16:27:06] "GET / HTTP/1.1" 200 15829

I'm not even sure who's fault this is. Does the runserver command change its output depending on how it is run? Is it a problem with exec? Is it docker/fig?

As one additional data point, I noticed that I do get all the output when running the container with fig run web, but not when I do fig up, but I don't understand how that's different or relevant.

Note: sorry for the tag spam, I'll reduce the tags once I know what actually causes this effect.

Henrik Heimbuerger
  • 9,924
  • 6
  • 56
  • 69
  • You could redirect output manually, and use append redirects `>>` later. – Paul Jan 21 '15 at 21:03
  • @Paul What do you mean? I'm not using any `>>` append redirects. – Henrik Heimbuerger Jan 21 '15 at 21:51
  • Why running migrate before each runserver? You can make`django-admin.py` an entrypoint and run migration like `fig run migrate`. – Krzysztof Szularz Jan 22 '15 at 16:13
  • @KrzysztofSzularz This is a dev environment setup aimed at non-technical people. The source code will be hooked in via a host-shared volume. I can't expect them to type `fig run migrate` everytime the migrations change (assume they don't know what migrations are or when a new migration has been committed, nor do they care). The setup just has to always work on `fig up`. – Henrik Heimbuerger Jan 22 '15 at 17:44

1 Answers1

23

I found this old issue today using docker composer. Python logging module checks the output is a terminal so you need to add tty: true to the service. Example:

version: '2'
services:
  django:
    tty: true
    command: python -u manage.py runserver 0.0.0.0:8080
    ports:
    - "8080:8080"
ernestoalejo
  • 853
  • 1
  • 12
  • 23