Docker - 无法访问 Django 服务器 [英] Docker - Can't access Django server
问题描述
我无法通过容器中的端口连接到 django.我正在使用这个地址:0.0.0.0.:8000 并查看:http://joxi.ru/Dr8MeGLhkBWnLm.我正在使用一个命令创建一个图像和一个容器:'docker-compose up -d'.
$ docker ps容器 ID 图像命令创建状态端口名称4fea50856eef docker_web "python manage.py ru..." 13 秒前向上 11 秒 0.0.0.0:8000->8000/tcp docker_web_1
docker-compose.yaml
版本:'3'服务:网络:建造:语境: .dockerfile:/django.testsite/Dockerfile端口:- 8000:8000"
Dockerfile
FROM python:3运行easy_install pip运行 pip install django==1.9.12运行 pip 安装请求添加 ./.工作目录/django.testsiteCMD ["python", "manage.py", "runserver", "8000"]
我该如何解决这个问题?
您的问题:
CMD [python"、manage.py"、runserver"、8000"]
为了使其工作,您需要将其更改为:
CMD [python"、manage.py"、runserver"、0.0.0.0:8000"]
最后,在浏览器中转到 http://127.0.0.1:8000/.>
为什么?好吧,Python 认为您将其暴露在 127.0.0.1
上,而实际上您想要的是 eth0 地址,因为这可能会改变,我们不想对其进行硬编码,我们使用 0.0.0.0
表示全部";接口.
记住 docker 127.0.0.1
不是你的主机 127.0.0.1
,每个 docker 容器都有自己的回环.
额外:如果你不想写出0.0.0.0
,你可以简单地写出0
——做同样的事情.
I can not connect to django through the port in the container. I'm using this address: 0.0.0.0.:8000 and see: http://joxi.ru/Dr8MeGLhkBWnLm. I'm creating an image and a container with one command: 'docker-compose up -d'.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4fea50856eef docker_web "python manage.py ru…" 13 seconds ago Up 11 seconds 0.0.0.0:8000->8000/tcp docker_web_1
docker-compose.yaml
version: '3'
services:
web:
build:
context: .
dockerfile: /django.testsite/Dockerfile
ports:
- "8000:8000"
Dockerfile
FROM python:3
RUN easy_install pip
RUN pip install django==1.9.12
RUN pip install requests
ADD . /.
WORKDIR /django.testsite
CMD ["python", "manage.py", "runserver", "8000"]
How do I solve this issue?
Your issue:
CMD ["python", "manage.py", "runserver", "8000"]
In order for it to work, you need to change it to:
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
and finally, go to http://127.0.0.1:8000/ in your browser.
Why? Well, Python thinks you're exposing it on 127.0.0.1
when in reality you want the eth0 address, since that can change, and we don't want to hard code it, we use 0.0.0.0
which means "ALL" interfaces.
Remember docker 127.0.0.1
IS NOT your host 127.0.0.1
, each docker container has its own loopback.
Extra: If you don't want to write out 0.0.0.0
you can simply write 0
-- does the same thing.
这篇关于Docker - 无法访问 Django 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!