Cannot connect to (flask) container from Ubuntu host

Hi all,

So I am aware this has been asked before - and I have literally read through 50 odd posts and replies on this topic, here and on SO. I have tried every conceivable combination of configs, and nothing has worked.

I created a simple flask API just for troubleshooting (the issue occurs also with nginx as server). When I try to run it locally on my Ubuntu machine, the container runs but I cannot access any endpoint associated with the container. (it runs fine on my windows machine, but my daily is a Ubuntu 22.04 laptop)

api.py

from flask import Flask


app = Flask(__name__)

@app.route("/", methods=["GET"])
def hello():
    return "<h1>HELLO CRUEL WORLD</h1>"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Running Flask locally works. Deploying to our k8s cluster works too. It is just on the local ubuntu machine (Ubuntu 22.04) that I cannot access the container. For 127.0.0.1:5000/, 0.0.0.0:5000/ and localhost:5000/ it throws a “This site can’t be reached” error.

DOCKERFILE

FROM python:3.9-slim
WORKDIR /app
COPY . /app

RUN apt update && apt upgrade -y
RUN pip install -r requirements.txt
RUN chmod -R 777 /app

EXPOSE 5000

CMD ["flask", "--app", "api.py","run", "--host","0.0.0.0", "--debug"]

docker commands

sudo docker build -t api-test .
sudo docker run -p 5000:5000 api-test

docker output

* Serving Flask app 'api'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://172.17.0.2:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 114-686-213

If I try to reach it via curl on my host machine, I get:

curl http://127.0.0.1:5000/ -v
*   Trying 127.0.0.1:5000...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

If I run curl inside the container, it works, so the server is running just fine.

I have also checked ufw/iptables, and there are no special rules for port 5000 (also occurs when I choose different ports)

I have looked through a lot of of posts detailing this very same issue. Most solutions are connected to the “–host”,“0.0.0.0” line or portmapping, both of which is given here.

I have also completely purged and reinstalled docker-ce per the Instructions. Did not work.

Anyother ideas what could be happening?


UPDATE
So I tried using podman instead. And after resolving the subuid/subgid issue, it works. Which is good for me, but the issue with docker persists, so I am leaving the post up. But at least I can now test locally.

Maybe try

docker run -d -p 5000:80 traefik/whoami

and

curl http://127.0.0.1:5000
1 Like