Why the django run command “python3 manage.py runserver” does not execute in docker-compose?

What Is The Problem?

I have a Dockerfile, docker-compose.yml and a run.sh script that runs my django server with so many configurations that work just fine and everything is tested but… the server does not run at the end on python3 manage.py runserver 127.0.0.1:80 command inside run.sh bash script.

I searched everywhere but didn’t find any solution at all. If someone can guide me what the problem is, I would be so thankful because I literally lost two days of my life in the process of running a simple django server with docker-compose.

Included Files

This is my docker-compose.yml file:

version: "3.9"

services:
  backend:
    build:
      context: .
      dockerfile: backend.Dockerfile
    restart: always
    image: backend:latest
    container_name: backend
    networks: 
      - net
    dns: backend
    volumes:
      - /project/backup:/project/backup --name backup
volumes:
  backup:
    name:
      "backup"
networks:
  net:
    name: net
    driver: bridge

And this is my Dockerfile which you just need to read the last CMD line at the end cause the other else commands on top of it work just fine:

# This is the only latest version of alpine that actually works with uwsgi
FROM python:3.8-alpine3.10

# for mysql and postgres
RUN set -ex 
    && apk add --update --no-cache --virtual build-deps 
        make 
        linux-headers 
        pcre-dev 
        postgresql-dev 
    && apk add --no-cache 
        mariadb-dev 
        python3 
        libc-dev 
        postgresql-libs

# pillow dependencies
RUN apk add --no-cache --virtual build-pillow 
        python3-dev 
        musl-dev 
    && apk add 
        jpeg-dev 
        zlib-dev 
        libjpeg 
        gcc

# my code
RUN apk add --update --no-cache 
        openssh-keygen 
        openssh-client 
        git 
    && eval $(ssh-agent) 
    && ssh-keygen -t rsa -N "" -f "/root/.ssh/id_rsa" 
    && echo && echo 
    && cat /root/.ssh/id_rsa.pub && echo && echo 
    && echo "you have 60 seconds to activate that ssh-key" 
    && sleep 60 
    && ssh-add /root/.ssh/id_rsa 
    && echo "StrictHostKeyChecking accept-new" >> /etc/ssh/ssh_config 
    && git clone "My-Super-Private-Git-Repository :)" /project/

WORKDIR /project/

# installing dependencies
RUN python3 -m pip install --upgrade pip 
    && pip3 install wheel 
    && pip3 install -r requirements.txt 
    && apk del 
        build-pillow 
        build-deps

COPY config.ini config.ini

COPY .env .env

COPY run.sh run.sh

RUN chmod +x run.sh

EXPOSE 1080 80:80 9418

# Here my problem starts
CMD ["./run.sh"]

And this is the run.sh script that runs migrations and then starts the server:

#!/bin/bash

cd /project

echo "(requirements)"
pip3 install -r requirements.txt

echo "(makemigrations)"
python3 manage.py makemigrations users anjoman notification

echo "(migrate)"
python3 manage.py migrate

echo "(runserver)"
# Script stops right when reading this command
python3 manage.py runserver 127.0.0.1:80

Logs

And this is the log that I’m taking:

backend | (requirements)
backend | Requirement already satisfied: setuptools>=54.1.0 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 5)) (57.4.0)
backend | Requirement already satisfied: uwsgi>=2.0.19.1 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 6)) (2.0.19.1)
backend | Requirement already satisfied: django>=3.1.7 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 7)) (3.2.7)
backend | Requirement already satisfied: djangorestframework>=3.12.2 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 8)) (3.12.4)
backend | Requirement already satisfied: beautifulsoup4>=4.9.3 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 9)) (4.9.3)
backend | Requirement already satisfied: requests>=2.25.1 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 10)) (2.26.0)
backend | Requirement already satisfied: mysqlclient>=2.0.3 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 11)) (2.0.3)
backend | Requirement already satisfied: drf-yasg2>=1.19.4 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 12)) (1.19.4)
backend | Requirement already satisfied: django-filter>=2.4.0 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 13)) (2.4.0)
backend | Requirement already satisfied: Pillow>=8.1.0 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 14)) (8.3.1)
backend | Requirement already satisfied: django-phonenumber-field[phonenumbers]>=5.0.0 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 15)) (5.2.0)
backend | Requirement already satisfied: python-decouple>=3.4 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 16)) (3.4)
backend | Requirement already satisfied: django-cors-headers>=3.7.0 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 17)) (3.8.0)
backend | Requirement already satisfied: faker>=8.1.3 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 18)) (8.12.1)
backend | Requirement already satisfied: pyrogram==0.18.0 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 19)) (0.18.0)
backend | Requirement already satisfied: tgcrypto>=1.2.2 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 20)) (1.2.2)
backend | Requirement already satisfied: django-dbbackup>=3.3.0 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 21)) (3.3.0)
backend | Requirement already satisfied: django-sslserver>=0.22 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 22)) (0.22)
backend | Requirement already satisfied: psycopg2>=2.9.1 in /usr/local/lib/python3.8/site-packages (from -r requirements.txt (line 23)) (2.9.1)
backend | Requirement already satisfied: pyaes==1.6.1 in /usr/local/lib/python3.8/site-packages (from pyrogram==0.18.0->-r requirements.txt (line 19)) (1.6.1)
backend | Requirement already satisfied: pysocks==1.7.1 in /usr/local/lib/python3.8/site-packages (from pyrogram==0.18.0->-r requirements.txt (line 19)) (1.7.1)
backend | Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.8/site-packages (from django>=3.1.7->-r requirements.txt (line 7)) (0.4.1)
backend | Requirement already satisfied: asgiref<4,>=3.3.2 in /usr/local/lib/python3.8/site-packages (from django>=3.1.7->-r requirements.txt (line 7)) (3.4.1)
backend | Requirement already satisfied: pytz in /usr/local/lib/python3.8/site-packages (from django>=3.1.7->-r requirements.txt (line 7)) (2021.1)
backend | Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.8/site-packages (from beautifulsoup4>=4.9.3->-r requirements.txt (line 9)) (2.2.1)
backend | Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.8/site-packages (from requests>=2.25.1->-r requirements.txt (line 10)) (2.0.4)
backend | Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.8/site-packages (from requests>=2.25.1->-r requirements.txt (line 10)) (1.26.6)
backend | Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/site-packages (from requests>=2.25.1->-r requirements.txt (line 10)) (2021.5.30)
backend | Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.8/site-packages (from requests>=2.25.1->-r requirements.txt (line 10)) (3.2)
backend | Requirement already satisfied: coreapi>=2.3.3 in /usr/local/lib/python3.8/site-packages (from drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (2.3.3)
backend | Requirement already satisfied: uritemplate>=3.0.1 in /usr/local/lib/python3.8/site-packages (from drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (3.0.1)
backend | Requirement already satisfied: inflection>=0.3.1 in /usr/local/lib/python3.8/site-packages (from drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (0.5.1)
backend | Requirement already satisfied: six>=1.15.0 in /usr/local/lib/python3.8/site-packages (from drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (1.16.0)
backend | Requirement already satisfied: packaging>=18.0 in /usr/local/lib/python3.8/site-packages (from drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (21.0)
backend | Requirement already satisfied: ruamel.yaml>=0.16.12 in /usr/local/lib/python3.8/site-packages (from drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (0.17.16)
backend | Requirement already satisfied: coreschema>=0.0.4 in /usr/local/lib/python3.8/site-packages (from drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (0.0.4)
backend | Requirement already satisfied: phonenumbers>=7.0.2 in /usr/local/lib/python3.8/site-packages (from django-phonenumber-field[phonenumbers]>=5.0.0->-r requirements.txt (line 15)) (8.12.31)
backend | Requirement already satisfied: python-dateutil>=2.4 in /usr/local/lib/python3.8/site-packages (from faker>=8.1.3->-r requirements.txt (line 18)) (2.8.2)
backend | Requirement already satisfied: text-unidecode==1.3 in /usr/local/lib/python3.8/site-packages (from faker>=8.1.3->-r requirements.txt (line 18)) (1.3)
backend | Requirement already satisfied: itypes in /usr/local/lib/python3.8/site-packages (from coreapi>=2.3.3->drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (1.2.0)
backend | Requirement already satisfied: jinja2 in /usr/local/lib/python3.8/site-packages (from coreschema>=0.0.4->drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (3.0.1)
backend | Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.8/site-packages (from packaging>=18.0->drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (2.4.7)
backend | Requirement already satisfied: ruamel.yaml.clib>=0.1.2 in /usr/local/lib/python3.8/site-packages (from ruamel.yaml>=0.16.12->drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (0.2.6)
backend | Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.8/site-packages (from jinja2->coreschema>=0.0.4->drf-yasg2>=1.19.4->-r requirements.txt (line 12)) (2.0.1)
backend | WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
backend | (makemigrations)
backend | Migrations for 'anjoman':
backend |   anjoman/migrations/0001_initial.py
backend |     - Create model Anjoman
backend |     - Create model Event
backend |     - Create model Job
backend |     - Create model Major
backend |     - Create model Skill
backend |     - Create model University
backend |     - Create model Telegram
backend |     - Create model Project
backend |     - Create model Member
backend |   anjoman/migrations/0002_initial.py
backend |     - Add field user to member
backend |     - Add field anjoman to job
backend |     - Add field anjoman to event
backend |     - Add field university to anjoman
backend | Migrations for 'notification':
backend |   notification/migrations/0001_initial.py
backend |     - Create model Answer
backend |     - Create model Info
backend |     - Create model Notification
backend |     - Create model Request
backend |   notification/migrations/0002_initial.py
backend |     - Add field fr to request
backend |     - Add field notification to request
backend |     - Add field to to request
backend |     - Add field info to notification
backend |     - Add field user to notification
backend |     - Add field request to answer
backend | Migrations for 'users':
backend |   users/migrations/0001_initial.py
backend |     - Create model User
backend | (migrate)
backend | Operations to perform:
backend |   Apply all migrations: admin, anjoman, auth, authtoken, contenttypes, notification, sessions, users
backend | Running migrations:
backend |   No migrations to apply.
backend | (runserver)

Summary

As you see in the last line in Logs when trying to execute python3 manage.py runserver 127.0.0.1:80 the command inside run.sh does not run at all and does not give me any sign of what just happened inside.
All I need is to know what just happened and how can I fix it.
Also thanks for informing me about things that I need to know.

Answer

Maybe python3 manage.py runserver 127.0.0.1:80 already run, just the log did not be flushed.

One option could be add PYTHONUNBUFFERED=1 to docker-compose.yaml to let python not buffer output:

services:
backend:
environment:
- PYTHONUNBUFFERED=1
build:
context: .
dockerfile: backend.Dockerfile

Or add tty: true to docker-compose.yaml to change output to line buffered:

services:
backend:
tty: true

Then, check the log to see if any output could help you debug.