Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Connect to Flask App On Docker From Host [duplicate]

I've setup a docker ubuntu 14.04 image and I'm running it with the following command:

docker run -d -p 5000:5000 ari/python3-flask

The Dockerfile:

FROM ubuntu:14.04
RUN apt-get update && apt-get install -y python3 python3-pip
ADD . /var/my_app
RUN pip3 install -r /var/my_app/requirements.txt
EXPOSE 5000
CMD ["python3", "/var/my_app/runserver.py"]

However, if I attempt to curl the address (localhost:5000) or visit it in a browser I get a connection failed error.

The docker log for the container shows:

Running on http://127.0.0.1:5000/

Restarting with reloader

Does anyone what is or could be wrong with my docker setup and/or configuration? Thanks.

like image 411
Ari Avatar asked Oct 17 '14 11:10

Ari


People also ask

Can you use localhost in Docker?

Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1 ) will refer to the docker host.

Can a container have multiple apps?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.


1 Answers

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/
like image 84
Thomasleveil Avatar answered Oct 17 '22 22:10

Thomasleveil