Windows上的Docker计算机端口转发不起作用 [英] Docker-machine Port Forwarding on Windows not working
问题描述
我正在尝试访问在Windows计算机上的Docker中运行的django应用。我正在使用docker-machine。我已经花了几个小时了。
I'm attempting to access my django app running within Docker on my windows machine. I'm using docker-machine. I've been taking a crack at this for hours now.
这是我的Django应用程序的Dockerfile:
Here's my Dockerfile for my django app:
FROM python:3.4-slim
RUN apt-get update && apt-get install -y \
gcc \
gettext \
vim \
curl \
postgresql-client libpq-dev \
--no-install-recommends && rm -rf /var/lib/apt/lists/*
EXPOSE 8000
WORKDIR /home/
# add app files from git repo
ADD . server/
WORKDIR /home/server
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "runserver", "8000"]
因此应该(至少在容器中)暴露端口8000。
So that should be exposing (at least in the container) port 8000.
当我使用命令 docker-machine ip default
时,我得到的IP为192.168.99.101。我在端口8000上访问了该IP,但没有响应。
When I use the command docker-machine ip default
I am given the IP 192.168.99.101. I go to that IP on port 8000 but get no response.
我进入VirtualBox,看看转发这些端口是否可行。配置如下:
I went into the VirtualBox to see if forwarding those ports would work. Here is the configuration:
我也尝试过使用 127.0.0.1
作为主机IP。我还尝试禁用Windows防火墙。
I also tried using 127.0.0.1
as the Host IP. I also tried disabling the windows firewall.
这是我启动容器的命令:
Here's my command for starting the container:
docker run --rm -it -p 8000:8000 <imagename>
我不知道为什么无法在该端口上进行连接。当我运行 docker-machine ls
时,它给我的网址是 tcp://192.168.99.101:2376
以及何时我去找了,它给了我一些文件,所以我知道docker-machine在那个端口上是活动的。
I am at a loss on why I am unable to connect on that port. When I run docker-machine ls
the url it gives me is tcp://192.168.99.101:2376
and when I go to that it gives me some kind of file back, so I know the docker-machine is active on that port.
当我运行 docker ps
我明白了:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c00cc28a2bd <image name> "python manage.py run" 7 minutes ago Up 7 minutes 0.0.0.0:8000->8000/tcp drunk_knuth
任何帮助将不胜感激。
推荐答案
问题是服务器在 127.0.0.1
应该在 0.0.0.0
上运行。
The issue was that the server was running on 127.0.0.1
when it should have been running on 0.0.0.0
.
我将Dockerfile中的 CMD
行从
I changed the CMD
line in the Dockerfile from
CMD [ python, manage.py, runserver, 8000]
至
CMD [ python, manage.py, runserver, 0.0.0.0:8000]
现在可以使用了。
这篇关于Windows上的Docker计算机端口转发不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!