Docker-machine Port Forwarding on Windows not work

2019-07-17 05:32发布

问题:

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.

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"]

So that should be exposing (at least in the container) port 8000.

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.

I went into the VirtualBox to see if forwarding those ports would work. Here is the configuration:

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>

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.

Also when I run docker ps I get this:

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

Any help would be greatly appreciated.

回答1:

The issue was that the server was running on 127.0.0.1 when it should have been running on 0.0.0.0.

I changed the CMD line in the Dockerfile from

CMD ["python", "manage.py", "runserver", "8000"]

to

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

and it now works.