Mike Johnson Jr
Mike Johnson Jr

Reputation: 796

(django) (docker) Django webserver wont start

I do

docker-compose up

I get

$ docker-compose up
Starting asynchttpproxy_postgres_1
Starting asynchttpproxy_web_1
Attaching to asynchttpproxy_postgres_1, asynchttpproxy_web_1
postgres_1  | LOG:  database system was interrupted; last known up at 2017-
05-01 18:52:29 UTC
postgres_1  | LOG:  database system was not properly shut down; automatic 
recovery in progress
postgres_1  | LOG:  invalid record length at 0/150F410: wanted 24, got 0
postgres_1  | LOG:  redo is not required
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
web_1       | Performing system checks...
web_1       |
web_1       | System check identified no issues (0 silenced).

My Dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

My docker-compose.yml

postgres:
  image: postgres:latest
  volumes:
    - ./code/
  env_file:
    - .env
  volumes:
    - /usr/src/app/static
  expose:
    - '5432'
web:
  build: .
  command: python3 manage.py runserver 0.0.0.0:8000
  env_file:
    - .env
  volumes: 
    - .:/code
  links:
    - postgres
  expose:
    - '8000'

As you can see, the django server wont start. What am I doing wrong? Thanks in advance.

Upvotes: 0

Views: 138

Answers (1)

Matheus Veleci
Matheus Veleci

Reputation: 344

First, try to run on another terminal

docker ps

to check if you server really did not start.

And check if you postgres database setup is ready when your django application start, if not try to run the an bash script to see if the connection is setup at postgress to initialize the django container.

wait-bd.sh

#!/bin/bash
  while true; do
    COUNT_PG=`psql postgresql://username:password@localhost:5432/name_db -c '\l \q' | grep "name_db" | wc -l`
    if ! [ "$COUNT_PG" -eq "0" ]; then
       break
    fi
       echo "Waiting Database Setup"
       sleep 10
  done

and in docker-compose.yml add the tag command in the django container:

   web:
     build: .
     command: /bin/bash wait-bd.sh && python3 manage.py runserver 0.0.0.0:8000

This script gonna wait you database setup, so will run the django setup container.

Upvotes: 1

Related Questions