0

I am in the process to dockerize my flask app. I got my Dockerfile working, now I just need to hook up my MySQL database. In flask I used to do this like the following in my __init.py___:

# SQL configs
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:password@localhost/db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

This works on my webserver, is there a way to achieve the same with docker?

I get the following error when running docker-compose up

app_1_fb82702505ba | [2018-11-08 04:02:27 +0000] [10] [ERROR] Exception in worker process
app_1_fb82702505ba | Traceback (most recent call last):
app_1_fb82702505ba |   File "/home/supv/venv/lib/python3.5/site-packages/pymysql/connections.py", line 582, in connect
app_1_fb82702505ba |     **kwargs)
app_1_fb82702505ba |   File "/usr/local/lib/python3.5/socket.py", line 712, in create_connection
app_1_fb82702505ba |     raise err
app_1_fb82702505ba |   File "/usr/local/lib/python3.5/socket.py", line 703, in create_connection
app_1_fb82702505ba |     sock.connect(sa)
app_1_fb82702505ba | OSError: [Errno 99] Address not available

Here is my docker-compose.yml:

version: "2"
services:
  app:
    build: ./
    links:
      - db
    ports:
      - "5000:5000"

  db:
    image: mysql:5.7
    ports:
      - "32000:3306"
    environment:
      MYSQL_ROOT_PASSWORD: pass
    volumes:
      - ./db:/docker-entrypoint-initdb.d/:ro

Also, I can manually connect to the mysql via command line:

# mysql --host=127.0.0.1 --port=32000 -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]>
lion_bash
  • 1,309
  • 3
  • 15
  • 27
  • 1
    Try changing `mysql+pymysql://username:password@localhost/db` to `mysql+pymysql://username:password@db/db` – Miguel Garcia Nov 08 '18 at 04:19
  • @MiguelGarcia I tried changing it to `db` and now getting ` "Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)` – lion_bash Nov 08 '18 at 18:45
  • Probably you need to wait for the database server, it takes some time to start. see https://stackoverflow.com/questions/42014344/mariadb-docker-container-cant-connect-to-mysql-server-on-host-111-connection-r – Miguel Garcia Nov 08 '18 at 20:17
  • Okay yeah so the db was the issue, my mysql is not fully up when my gunicorn tries to start my flask app. – lion_bash Nov 09 '18 at 03:21

0 Answers0