Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run flask app in debug mode using Docker

I am trying to run a simple flask app in debug mode using docker-compose. I have created my Dockerfile as follows:

FROM jazzdd/alpine-flask

EXPOSE 80

My docker-compose file looks like this:

version: '2'

networks:
  test_network:
    driver: bridge

services:
  db:
    networks:
      - test_network
    image: postgres:9.5.3
    env_file:
      - docker.env
    expose:
      - 5432

  app:
    networks:
      - test_network
    build: .
    env_file:
      - docker.env
    expose:
      - 80
    ports:
      - 80:80
    volumes:
      - ./app/:/app
    command: -d

My docker.env just has password to postgres database. I created a simple python file as follows:

  from flask import Flask
  app = Flask(__name__)

  @app.route('/')
  def hello_world():
     return "Hello, World"

 if __name__ == '__main__':
     app.run(debug=True,host='0.0.0.0')

Now to run the app, I am using docker-compose up -d --build command. I would assume that after the app starts on the server, when I make any change to app.py file, it will be reflected on the webpage without me having to restart the containers. I'm not seeing the expected behavior. I tried setting my local env variable FLASK_DEBUG=1 but not sure if that would help. Am I missing something?

I also referenced this page but didn't see anything useful.

like image 238
clever_bassi Avatar asked Oct 18 '22 07:10

clever_bassi


1 Answers

A sample (simplifed) runthru demostrating file edits with no need for container restarts outlined below for your reference.

app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello, World"

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=80)

You will need to specify the port for the flask development server in order to match the exposed container port of 80.

screenshot can be viewed here

Summary of Steps in screenshot (MAC OS X):

  1. starting with Empty directory
  2. Create app.py
  3. docker run
  4. curl localhost (this will display Hello, World)
  5. edit app.py
  6. curl localhost (this should display the new edits)
like image 157
ABC Avatar answered Oct 21 '22 04:10

ABC