Docker-无法访问Django服务器
我无法通过容器中的端口连接到django。我使用的地址是:0.0.0.0。:8000,请参见: http://joxi.ru/Dr8MeGLhkBWnLm 。我正在使用一个命令创建映像和容器:'docker-compose up -d'。
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
docker-compose.yaml
version: '3'
services:
web:
build:
context: .
dockerfile: /django.testsite/Dockerfile
ports:
- "8000:8000"
Dockerfile
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?
您的问题:
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"]
,最后,转到 http://127.0。 0.1:8000 / 在您的浏览器中。
and finally, go to http://127.0.0.1:8000/ in your browser.
为什么?嗯,Python认为您实际上将其公开在 127.0.0.1
上,而实际上您想要eth0地址,因为这可能会改变,我们不想对其进行硬编码,我们使用 0.0.0.0
表示所有接口。
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.
记住docker 127.0 .0.1
不是您的主机 127.0.0.1
,每个Docker容器都有自己的环回。
Remember docker 127.0.0.1
IS NOT your host 127.0.0.1
, each docker container has its own loopback.
额外:如果您不想写出 0.0.0.0
,您只需写出 0
-做同样的事情。
Extra: If you don't want to write out 0.0.0.0
you can simply write 0
-- does the same thing.