0

I've poured over the docs but there's some fundamental piece that I'm still missing.

System:

  • mac os 11.2.3
  • docker v20.10.5

I have a dumb hello world container in python using Flask in http_server.py:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
    return 'hello world'

The Dockerfile:

FROM python:3.8
WORKDIR /
COPY *.py /
RUN pip install Flask
ENV FLASK_APP=http_server.py
CMD flask run -h localhost -p 80

I build the container:

docker build -t au .

I run the container using port forwarding:

docker run -p 127.0.0.1:8080:80 httptest

I check the container:

$ docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                    NAMES
c857400d06ce   httptest  "/bin/sh -c 'flask r…"   4 minutes ago   Up 4 minutes   127.0.0.1:8080->80/tcp   busy_feistel

I double check to see there's actually an open socket on the local machine:

$ netstat -an | grep 8080
tcp4       0      0  127.0.0.1.8080         *.*                    LISTEN

I open the cli in the container to triple check the app is running:

$ docker exec -it c857400d06ce8f2dd88d8466d57c0d274320215e24f2a4610b67d0ee75f8a2ad /bin/sh
# curl 127.0.0.1:80
hello world

Seems like it's all buttoned up! I try to hit the container from my local:

$ curl 127.0.0.1:8080
curl: (52) Empty reply from server

What am I missing? I feel like there is something extremely simple that I'm glossing over, but I've reached the 60-minute mark of searching SO and docker's documentation, so I am throwing in the towel.

Thank you in advance!

tmb322
  • 1
  • 1

2 Answers2

0

Run the flask application on 0.0.0.0 instead of localhost Or run the container in the host network_mode, I think it will fix the issue.

  • This worked but feels like a hack of sorts. `0.0.0.0` will catch all IPv4 addresses inside the docker container. Why does `localhost` not work? – tmb322 Apr 29 '21 at 18:33
  • I think that's because docker acts like a proxy to deliver the request to the flask application and technically the request not come from localhost, as I said before you can set the network_mode to "host" mode, in this way the request will send from localhost – Aryan Arabshahi Apr 29 '21 at 18:55
0

I'm not sure but try write in your Dockerfile the next line EXPOSE 80

TheVitik
  • 11
  • 3