为什么Docker容器中的Django不起作用?

时间:2018-05-01 13:34:41

标签: django docker

Dockerfile:

FROM python:3.6.5-jessie
MAINTAINER example-user

RUN apt-get update \
    && apt-get install -y vim
RUN mkdir require
COPY ./requirements.txt require/requirements.txt
RUN pip install -r require/requirements.txt
EXPOSE 8000

requirements.txt

Django==2.0.2

如上所述,我制作了一个名为sample-image

的图片

接下来,我从sample-image中运行了一个容器,

docker run -ti --name sample_django -p 8001:8000 sample-image /bin/bash

在容器bash上,我命令了,

root@4a947abf5ce5:/# mkdir startdir && cd startdir
root@4a947abf5ce5:/startdir# django-admin startproject sample .
root@4a947abf5ce5:/startdir# python manage.py runserver

它返回了它:

Performing system checks...

System check identified no issues (0 silenced).

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

May 01, 2018 - 12:44:45
Django version 2.0.2, using settings 'sample.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

因为我在运行容器上设置了-p 8001:8000,我在浏览器中找到了这个页面(127.0.0.1:8080),但它确实返回了它:

enter image description here

问题: 如何正确设置容器上的端口?或者我错过了什么?

1 个答案:

答案 0 :(得分:4)

在泊坞窗容器内,使用python manage.py runserver 0.0.0.0:8000,以便runserver不会绑定到127.0.0.1