0

Simple request fails on Flask in Docker container, here's Dockerfile:

FROM python:3.8-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

EXPOSE 8000:8000

CMD [ "flask", "run" ]

Here's docker-compose.yml:

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

And here's main.py:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello():
  return 'Hello!'

Idk, I've checked this multiple times, it must be something obvious which I don't see. But sending a GET request to http://127.0.0.1:8000 returns a general error in Insomnia with the words "Error: Server returned nothing (no headers, no data)".

chocojunkie
  • 479
  • 2
  • 8
  • 14

1 Answers1

2

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
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35