Skip to content
Advertisement

Minimal Flask app unreachable from Docker container

I have a very simple flask service that I’m trying to run inside a docker container.
I used python to implement a minimal application that is working fine on my Windows, but isn’t reachable when inside a Docker container.
I tried the common fix of setting –host=0.0.0.0 (tried multiple ways), but it did not solve my problem.

Project structure:
├── application/
│ ├── Dockerfile
│ ├── requirements.txt
│ ├── service.py

Dockerfile:

JavaScript

service.py:

JavaScript

Finally, here’s the log when I start the Docker image in interactive mode:

JavaScript

So there appears to be no problems, however when I use a browser (Google Chrome) to visit the address shown in the console, it just loads forever then displays that the site is unreachable (ERR_CONNECTION_TIMED_OUT).
Also, I can run the flask with no problem from my native OS by typing the following commands from a bash terminal:

JavaScript

Can anybody help me understand why it’s not working inside a Docker container? It’s working from outside, and I thought adding host='0.0.0.0' was supposed to make the container accessible from outside.

Edit: docker command tested:

JavaScript

RESOLVED
The trick, as mentioned by u/OneCricketeer, is to use -p 5000:5000 argument in docker run, and now you can use reach the container BUT you have to ignore the address given in the docker terminal. Use this instead : http://localhost:5000 (the ip it would give you if you ran flask from outside the docker container.)

Advertisement

Answer

The issue that you are likely seeing is that your windows host cannot get to the docker container. The networking is not exposed to your host so you’ll need to include the -p 5000:5000 option on the docker command line to tell docker to forward host port 5000 to the docker container port 5000.

In short:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement