在Docker容器中看不到Django [英] Don't see Django in Docker container

查看:205
本文介绍了在Docker容器中看不到Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个简单的Docker容器中运行Django。

I want to run Django in a simple Docker container.

首先,我使用Docker-file构建了容器。没什么特别的(只有FROM,RUN和COPY命令)

First I built my container with Docker-file. There wasn't anything special in it (only FROM, RUN and COPY commands)

然后我用命令

docker run -tid -p 8000:8000 --name <container_name> <image>

输入我的容器:

docker exec -it <container_name> bash

Ran Django服务器:

Ran Django server:

python manage.py runserver

Got:

Starting development server at http://127.0.0.1:8000/

中启动开发服务器

但是当我转到127.0.0.1:8000时,我什么也看不到:

But when I go to 127.0.0.1:8000 I see nothing:

The 127.0.0.1 page isn’t working

没有Nginx或其他工作服务器。

There are no Nginx or other working servers.

我在做什么错了?

更新1(Dockerfile)

FROM ubuntu:16.04
MAINTAINER Max Malyshev <user>
COPY . /root
WORKDIR /root
RUN apt-get update
RUN apt-get install python-pip -y
RUN apt-get install postgresql -y
RUN apt-get install rabbitmq-server -y
RUN apt-get install libpq-dev python-dev -y
RUN apt-get install npm -y
RUN apt-get install mongodb -y
RUN pip install -r requirements.txt


推荐答案

问题在于您将开发服务器暴露在Docker容器内而不是主机OS上的 127.0.0.1 中。
如果您访问容器的另一个控制台并对 127.0.0.1:8000 发出http请求,它将起作用。

The problem is that you're exposing the development server to 127.0.0.1 inside your Docker container, not on the host OS. If you access another console to your container and do a http request to 127.0.0.1:8000 it will work.

关键是确保Docker容器将开发服务器公开给所有IPv4地址,您可以使用 0.0.0.0 代替 127.0.0.1

The key is to make sure the Docker container exposes the development server to all IPv4 addresses, you can do this by using 0.0.0.0 instead of 127.0.0.1.

尝试运行以下命令来启动Django开发服务器:

Try running the following command to start your Django development server instead:

python manage.py runserver 0.0.0.0:8000

此外,为了获得更多启发,您可以查看此工作的Dockerfile,以使用内置开发服务器 https://github.com/Niklas9/django-unixdatetimefield/blob/master/Dockerfile

Also, for further inspiration, you can check out this working Dockerfile for hosting a Django application with the built-in development server https://github.com/Niklas9/django-unixdatetimefield/blob/master/Dockerfile.

这篇关于在Docker容器中看不到Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆