Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

failing GET request to Flask in Docker

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)".

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading