Docker compose up gives error

hi, i am pretty new in docker and docker compose. I write a web app, python flask. and I wanna dockerize it. but it gives error.

  1. if I write this

if name == ‘main’:
app.run(host=‘127.0.0.1’)

% docker compose up

then it runs, but the website get not open, and it says that the website overloaded, and try again later.

  1. if I write
    if name == ‘main’:
    app.run(host=‘0.0.0.0’)

% docker compose up

it runs and give as an output:

pexonhwdockerfilekopie-web-1 | * Running on http://127.0.0.1:5000
pexonhwdockerfilekopie-web-1 | * Running on http://172.18.0.2:5000 (Press CTRL+C to quit)

and if I choose Running on http://127.0.0.1:5000 , I see that my web app runs truely,
but if I choose
Running on http://172.18.0.2:5000
then it takes longer then ever, and get not open.

my docker-compose file is:
version: ‘3’
services:
web:
build: .
ports:
- “5000:5000”
volumes:
- .:/app:/Desktop/pexonhwdockerfilekopie/app.py

so what is the problem hier? please help me…

Hi

Where are you trying to access http://172.18.0.2:5000 from?
because if its the same host as where docker is running, it should work.

But if you’re accessing it from another host, then it wont work because 172.18.0.0 is a local network on the docker host.

pexonhwdockerfilekopie-web-1 | * Running on http://127.0.0.1:5000
pexonhwdockerfilekopie-web-1 | * Running on http://172.18.0.2:5000 (Press CTRL+C to quit)

Is this log from the same time? because i would expect that only the last line would be present with the 0.0.0.0 “interface”

The IP address 172.18.0.2 is internal to Docker. You will never reach it. That’s why you are port forwarding to localhost on your computer with:

ports:
  - “5000:5000”

Those lines in the docker-compose.yaml file will forward traffic from http://172.18.0.2:5000 in the container to http://127.0.0.1:5000 on the host computer.

When you used:

app.run(host=‘0.0.0.0’)

That told Flask to bind to all adapters. The two adapters in your container were 127.0.0.1 (i.e., localhost) and 172.18.0.2. The beauty of specifying 0.0.0.0 is that you didn’t need to know the actual IP address that gets assigned to the container. If you run the container again, it may get a different IP address but again, you don’t care. You never use that address. You always address it as http://127.0.0.1:5000.

You’re able to reach a containers service by connecting to its internal docker ip, just like 127.0.0.1, as long as its from the host itself.

From another host/external? it wont work, of course

You are correct. I forgot that if you are running Docker on Linux you actually can reach that IP address. If, however, you are running Docker on Mac or Windows, the IP address is the IP of the hidden VM that Mac and Windows use in the background to provide a Linux host which you can’t reach. Thanks for pointing that out.