Problem reaching flask app in docker-compose

Viewed 1444

yet another docker/flask question. I'm having problems connecting to my flask app from another container in docker-compose.

My dockerfile sets up a flask app like:

COPY ./ /app/

WORKDIR /app

RUN find -name "*.pyc" -delete && \
    pip3 install -r requirements.txt

EXPOSE 5000

CMD ["python3", "app.py"]

The app runs on 0.0.0.0:5000, the docker-compose.yml looks like this:

version: '3'

services:

  app:
    build: .
    ports:
      - "5000:5000"

  test:
    image: alpine:3.8

Now after docker-compose up I can curl from the host like:

curl -i http://0.0.0.0:5000
...
200 - ok

but if I jump into the test container I get a 404:

docker-compose run test sh
# apk --update add curl
# curl -i http://app:5000
...
404 - not found

Actually these 2 containers should see each other, and indeed when I look at the logs of app I can see the requests from test comming in. But they are answered with 404.

What am I doing wrong here?

Btw, I have a minimal example here: https://github.com/mRcSchwering/flask_docker-compose

1 Answers

This is because your config

app.config['SERVER_NAME'] = '0.0.0.0:5000'

Your Flask thinks its name is 0.0.0.0:5000, therefore not responding to others.

To verify, call curl -H Host:0.0.0.0:5000 -i http://app:5000 from test.

You can remove this config and run the server with

app.run(host='0.0.0.0')
Related