(django)(docker)Django webserver不会启动

时间:2017-05-01 19:08:44

标签: django docker

我做

docker-compose up

我得到了

$ 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).

我的Dockerfile:

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

我的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'

如您所见,django服务器无法启动。我究竟做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:1)

首先,尝试在另一个终端上运行

docker ps

检查服务器是否确实没有启动。

检查你的django应用程序启动时你的postgres数据库设置是否准备就绪,如果没有尝试运行一个bash脚本,看看是否在postgress设置了连接以初始化django容器。

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

并在docker-compose.yml中在django容器中添加tag命令:

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

此脚本将等待您进行数据库设置,因此将运行django安装容器。