Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot reach docker container - port not bound

Setup first:

Windows 10 without WSL2 - old Hyper-V Backend

Docker for Windows - Linux containers.

I have a small python script:

from flask import Flask
server = Flask(__name__)

@server.route("/ping")
def hello():
    return "Hello World!"

if __name__ == "__main__":
   server.run(host='0.0.0.0') 

When I run it locally (without docker) I can reach localhost:5000/ping just fine.

Dockerfile:

FROM python:3.8-buster

RUN useradd -ms /bin/bash user
USER user

WORKDIR /home/user

COPY requirements.txt .
RUN pip install -r requirements.txt && rm requirements.txt
COPY app.py .

EXPOSE 5000

ENTRYPOINT [ "python", "app.py"]

Builds fine. Start with: docker container run -t test_tag -dp 5000:5000 Log after start is also normal:

* Serving Flask app "app" (lazy loading)

* Environment: production

WARNING: This is a development server. Do not use it in a production deployment.

Use a production WSGI server instead.

* Debug mode: off

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

But localhost:5000/ping does not work - connection failed.

But running the docker example docker run -dp 80:80 docker/getting-started works perfectly - I can access the webserver of the container under localhost:80

Running docker inspect on both containers shows one difference: My container has

"Ports": {
                "5000/tcp": null
            },

in the network settings, for the working example it's (a more correct looking)

 "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ]
            },

So it seems the required port is not bound correctly. What do I need to do to bind the port?

like image 242
user3696412 Avatar asked Feb 12 '26 13:02

user3696412


1 Answers

As David Maze pointed out, order does matter for the docker cmd command.

The -dp option needs to come before the image name.

So using

docker container run -dp 5000:5000 -t test_tag 

works like a charm.

like image 180
user3696412 Avatar answered Feb 14 '26 05:02

user3696412