10

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?

2
  • The -d and -p options need to appear before the image name test_tag. In the position they're in now they override the image's CMD instead of configuring Docker options.
    – David Maze
    Feb 22, 2021 at 13:41
  • Thanks! I just found this out seconds before your comment deeply burried in another stackoverflow answer... Man. Some big note for this in the docker getting started would be helpful Feb 22, 2021 at 13:42

2 Answers 2

10

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.

2
  • 1
    how can i set this on dockerfile? Aug 12, 2022 at 14:57
  • 1
    Afaik, binding ports is not an option available in Dockerfiles (creating the container image), but it is always a runtime option when execution an image with docker or docker-compose. Aug 15, 2022 at 17:01
0
docker run -dp 8080:8080 -it test_tag worked for me
1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 19, 2022 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.