Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker app cannot be accessed through opened ports

I have a flask app inside of a container. I run this container with

docker run -p 5000:5000 pyprojects_web

It replies

 * Serving Flask app "debateit.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

If I run

docker container ls

I get

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
2221298e6e2c        pyprojects_web      "flask run"         12 minutes ago      Up 12 minutes       0.0.0.0:5000->5000/tcp   elated_joliot

If I access http://127.0.0.1:5000 I get:

This site can’t be reached
The web page at http://127.0.0.1:5000/ might be temporarily down or it may have moved permanently to a new web address.
ERR_SOCKET_NOT_CONNECTED

http://localhost:5000 gives a similar response.

The normal advice is to listen to all connections inside your container with 0.0.0.0 - but I am already doing that. Here is my app:

from app import app


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

If I curl from inside of my container it works perfectly:

docker exec -it 2221298e6e2c curl http://localhost:5000

with a long HTML response, and my server logs get:

127.0.0.1 - - [04/Jun/2018 01:00:16] "GET / HTTP/1.1" 200 -

Given these results, and given that I have a "0.0.0.0" host, what is left to try?

Thanks.

like image 647
WarSame Avatar asked Jun 04 '18 01:06

WarSame


People also ask

How do I access exposed port Docker?

Need of exposing ports. In order to make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, we can use the -P or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

Does Docker use port 8080?

Docker also finds ports you expose with --expose 8080 (assuming you want to expose port 8080). Docker maps all of these ports to a host port within a given epehmeral port range . You can find the configuration for these ports (usually 32768 to 61000) in /proc/sys/net/ipv4/ip_local_port_range .

What port should I use for Docker?

By default, the httpd server listens on port 80. It's not mandatory to perform port mapping for all Docker containers.

How do I check if port is running on Docker?

Check your Docker daemon. After restarting docker service, you can see the port in the output of systemctl status docker. service like /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock .


1 Answers

So based on the comments it's clear you're not actually running on 0.0.0.0 and that's probably (noticing your command for the container) because you're running a newer version of flask where I think you have to pass some args to flask run.

Try flask run --host=0.0.0.0 as the command in your container and I think that will probably work out how you're expecting :)

More info at the Flask Docs.

like image 167
johnharris85 Avatar answered Oct 11 '22 16:10

johnharris85