docker-compose up not forwarding port

时间:2018-10-02 09:09:06

标签: python docker docker-compose

I have the following files. When I run docker-compose up -d and exec into the docker container, I am able to run http://127.0.0.1:5000 and get back results. From the host I get back:

user@ubuntu:~/projects/grip/glm-plotter$ curl -v http://localhost:5000
* Rebuilt URL to: http://localhost:5000/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 5000 (#0)
> GET / HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.58.0
> Accept: */*
> 
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

If I run the docker-compose command without a -d, it starts up but from a separate terminal I also get back empty results.

docker ps (When running without -d)

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
237b0a7f8710        glm-plotter_web     "python3 glm-plotter…"   16 minutes ago      Up 24 seconds       0.0.0.0:5000->5000/tcp   glm-plotter_web_1

I'm not sure if the process should be listed under "docker-proxy":

user@ubuntu:~$ sudo netstat -tulpn | grep 5000 
tcp6       0      0 :::5000                 :::*                    `LISTEN      13785/docker-proxy`  

Dockerfile

FROM library/python:3.6-stretch
RUN apt-get update && apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential

COPY requirements.txt /
RUN pip3 install --trusted-host pypi.org -r /requirements.txt
RUN pip3 install --upgrade numpy
ADD ./glm-plotter /code
WORKDIR /code
RUN ls .
CMD ["python3", "glm-plotter.py"]

docker-compose.yml

version: "3"
services:
  web:
    volumes:
      - ~/.aws:/root/.aws
    build: .
    ports:
      - "5000:5000"

1 个答案:

答案 0 :(得分:5)

您的glm-plotter.py仅在容器中的localhost上监听。您的应用必须在容器中的所有接口上监听-TCP主机0.0.0.0,而不仅仅是locahost/127.0.0.1-您需要修复python代码。

这是一个常见问题-烧瓶示例:flask is working inside container but not when I port forward it