Deploying a Minimal Flask App in Docker - Server Connection Issues

Deploying a minimal flask app in docker - server connection issues

The problem is you are only binding to the localhost interface, you should be binding to 0.0.0.0 if you want the container to be accessible from outside. If you change:

if __name__ == '__main__':
app.run()

to

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

It should work.

Note that this will bind to all interfaces on the host, which may in some circumstances be a security risk - see https://stackoverflow.com/a/58138250/4332 for more information on binding to a specific interface.

python: Cant expose flask app in docker container

In the logs, you can see Running on http://127.0.0.1:5000/ (Press CTRL+C to quit). 127.0.0.1 means that your app is 'bound' to 127.0.0.1 (localhost) which means that it'll only accept connections from that machine. In a Docker container, localhost is the container itself. So it won't accept connections from outside the container.

To get it to do that, you need to make it bind to 0.0.0.0 instead. Then it'll accept connections from anywhere.

You do that by adding the host parameter to your app.run statement in your program, like this

app.run(host='0.0.0.0')

Python app does not print anything when running detached in docker

Finally I found a solution to see Python output when running daemonized in Docker, thanks to @ahmetalpbalkan over at GitHub. Answering it here myself for further reference :

Using unbuffered output with

CMD ["python","-u","main.py"]

instead of

CMD ["python","main.py"]

solves the problem; you can see the output (both, stderr and stdout) via

docker logs myapp

now!

Refused to connect local ip address by using docker while running ok without docker

127.0.0.1 is the localhost interface and when running inside the container only the container has access to it. To be accessible outside the container it should be the special IP 0.0.0.0 to bind all interfaces.

Solution 1

Change the host to be 0.0.0.0 instead of 127.0.0.1 in the test_flask.py. Something like the following snippet.

WARNING: As 0.0.0.0 binds all interfaces to the outside world it could be a security risk if running it locally instead of in a container. That said, the Solution 2 is the recommended one (With the 127.0.0.1 in the Python source as it is). So, when running it directly locally it binds to the localhost and when running it in the container it binds the outside world of the container itself.

...

if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)

Solution 2

On the other hand, to run it passing the host as an argument the Dockerfile should be changed to the following:

...

ENV FLASK_APP=test_flask
CMD ["flask", "run", "--host=0.0.0.0"]

Test it with the browser connecting to http://localhost:8000/ or http://127.0.0.1:8000/

Unable to Connect to Flask App On Docker From Host

The web server running in your container is listening for connections on port 5000 of the loopback network interface (127.0.0.1). As such this web server will only respond to http requests originating from that container itself.

In order for the web server to accept connections originating from outside of the container you need to have it bind to the 0.0.0.0 IP address.

As you are using Flask, this can be easily achieved in your runserver.py file by using:

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

Then when you start your container and look at the log, you should see something like:

 * Running on http://0.0.0.0:5000/

failing GET request to Flask in Docker

Your app binds to 127.0.0.1 meaning that it'll only accept connections from inside the container. If you make it bind to 0.0.0.0, it'll accept connections from anywhere

services:
vodolei:
image: my_api:demo
ports:
- 8000:8000
volumes:
- ./src:/app
environment:
FLASK_APP: main.py
FLASK_ENV: development
FLASK_RUN_HOST: 0.0.0.0
FLASK_RUN_PORT: 8000

Can't access my simple flask app running in docker

figured it out. from a very nice person on slack.

I needed to type "localhost:/5000" into my address bar. It confused me because I have always access the site I'm working on through the flask log which in this case is:

 * Serving Flask app 'project/app.py' (lazy loading)
* Environment: development
* Debug mode: on
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!

I guess since it's running in the container that address doesn't work anymore. Hopefully this helps anyone else going through growing pains like me while learning docker.

Python Flask not running with docker-compose

You have to run your flask app on "0.0.0.0" host, in order to be able to map ports from the docker container.

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


Related Topics



Leave a reply



Submit