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?
-d
and-p
options need to appear before the image nametest_tag
. In the position they're in now they override the image'sCMD
instead of configuring Docker options.