简体   繁体   English

Django应用程序服务器挂起/无法在Docker Compose中启动

[英]Django app server hangs / won't start in Docker Compose

I am trying to launch a straightforward Django app server in Docker Compose, paired with a Postgres container. 我正在尝试在Docker Compose中启动一个简单的Django应用服务器,并与Postgres容器配对。 It goes through as I would expect, launching the entrypoint script, but it never seems to actually run the Django app server (which should be the last step, and remain running). 它按照我的预期进行,启动了入口点脚本,但似乎从未真正运行Django应用服务器(这应该是最后一步,并保持运行状态)。

I know it runs the entrypoint script, because the migrate step is run. 我知道它运行入口点脚本,因为运行了迁移步骤。 The app server never outputs any of the expected output, and port 8000 never responds. 应用程序服务器从不输出任何预期的输出,并且端口8000从不响应。

I am using Docker for Mac (stable), if it matters. 如果有问题,我正在使用Mac的Docker(稳定)。

Dockerfile for my Django app container: 我的Django应用容器的Dockerfile:

FROM ubuntu:16.04

COPY my_app /my_app

RUN apt-get update \
 && apt-get install -y python3 python3-psycopg2 python3-pip

RUN apt-get install -y nodejs npm

WORKDIR /my_app
RUN pip3 install -r requirements.txt
RUN npm install bower
RUN python3 manage.py bower install
RUN python3 manage.py collectstatic --no-input

EXPOSE 8000

COPY entrypoint.sh /
RUN chmod 755 /entrypoint.sh

CMD python3 manage.py runserver 0.0.0.0:8000
ENTRYPOINT ["/entrypoint.sh"]

Django entrypoint script: Django入口点脚本:

#!/bin/sh

# Allow database container to start up or recover from a crash
sleep 10

cd /my_app

# Run any pending migrations
python3 manage.py migrate

exec $@

docker-compose.yml: docker-compose.yml:

version: '2'
services:
  db:
    image: postgres:9.6
    volumes:
      - ./db/pgdata:/pgdata
    environment:
      - POSTGRES_USER=my_user
      - POSTGRES_PASSWORD=my_password
      - PGDATA=/pgdata
      - POSTGRES_DB=my_database
  appserver:
    image: my-image
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - '8000:8000'
    environment:
      - POSTGRES_USER=my_user
      - POSTGRES_PASSWORD=my_password
      - POSTGRES_DB=my_database
    links:
      - db
    depends_on:
      - db

Use the exec form for CMD in your Dockerfile 在Dockerfile中使用CMD的exec表单

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

The entrypoint.sh scripts exec is currently trying to run: entrypoint.sh脚本exec目前正试图运行:

/bin/sh -c python3 manage.py runserver 0.0.0.0:8000

Which doesn't seem to work, I think it's just running python3 . 这似乎不起作用,我认为它只是运行python3

You should quote the positional parameters variable so the shell maintains each parameter, even if there are spaces. 您应该引用位置参数变量,以便即使有空格,外壳程序也可以维护每个参数。

exec "$@"

But it's best not to have sh in between docker and your app, so always use the exec form for a CMD . 但最好不要有sh在码头工人和您的应用程序之间,所以总是使用exec形式的CMD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM