0

I have a simple Flask app consisting of a web part and a database part. Code can be found here. Running this Flask app locally works perfectly well.

I'd like to have the app run on Docker. Therefore I created the following docker-compose file.

version: '3.6'

services:
  web:
    build: .
    depends_on:
      - db
    networks:
      - default
    ports:
      - 50000:5000
    volumes:
      - ./app:/usr/src/app/app
      - ./migrations:/usr/src/app/migrations
    restart: always

  db:
    environment:
      MYSQL_ROOT_PASSWORD: ***
      MYSQL_DATABASE: flask_employees
      MYSQL_USER: root
      MYSQL_PASSWORD: ***
    image: mariadb:latest
    networks:
      - default
    ports:
      - 33060:3306
    restart: always
    volumes:
       - db_data:/var/lib/mysql

volumes:
    db_data: {}

Inside the Flask app, I'm setting the following SQLALCHEMY_DATABASE_URI = 'mysql://root:***@localhost:33060/flask_employees'.

When I do docker-compose up, both containers are created and are running however when I go to http://localhost:50000 or http://127.0.0.1:50000 I get:

This site can’t be reached, 127.0.0.1 refused to connect.

wiwa1978
  • 2,317
  • 3
  • 31
  • 67
  • How do you start the Flask application; what does the `app.run()` line look like? (See also [Deploying a minimal flask app in docker - server connection issues](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues).) From the point of view of the Flask app, `localhost` is the Flask app, not the database, so reviewing [Networking in Compose](https://docs.docker.com/compose/networking/) to see the host names available for inter-container communication may also help you. – David Maze Jan 24 '21 at 23:25
  • Thanks. I was using ` app.run(host='127.0.0.1', port='5000')` and that caused indeed the error. – wiwa1978 Jan 25 '21 at 08:01
  • Does this answer your question? [Deploying a minimal flask app in docker - server connection issues](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues) – David Maze Jan 25 '21 at 12:20

1 Answers1

2

You have to run your flask app on "0.0.0.0" host, in order to be able to map ports from the docker container.

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='5000')
o_petriv
  • 68
  • 6
  • The first option does not work. I get `services.web.ports contains an invalid type, it should be a number, or an object`. But the second works and happy with it. If you edit the answer I can accept it. – wiwa1978 Jan 25 '21 at 08:00
  • Invalid option was removed – o_petriv Jan 26 '21 at 08:10