-1

I am attempting to build a bridge between my flask application and my database using two separate containers rather than the mysql connector. I am using flask_mysqldb for my app.

  ---docker-compose
     App
      |
      |--app.py
      |--dockerfile
     db
      |
      |--dockerfile
      |--db.sql 

This is My Docker-compose file

  version: '2'
  services:
    pythonapp:
        build: ./NewtestPythonProject
        command: sh -c "sleep 10s ; python3 ./main.py"
        ports:
        - '5000:5000'
        depends_on:
          - mysql
        
          
    mysql:
      build: ./MyDbDockerFile
      restart: always
      environment:
          MYSQL_DATABASE: 'profile'
          MYSQL_ROOT_PASSWORD: '1234'
      ports:
        - '3306:3306'

This is my Database Dockerfile

FROM mysql:latest
COPY ./dcs.sql /docker-entrypoint-initdb.d/

This is my app dockerfile

FROM python:3.9

WORKDIR /usr/app/src

COPY . /usr/app/src/

RUN pip3 install -r requirements.txt


EXPOSE 5000

This is My Connection String

from flask import *
from flask_mysqldb import MySQL
import yaml,re 
import MySQLdb.cursors
from passlib.hash import pbkdf2_sha256

app = Flask(__name__)
app.secret_key = 'This'
#-----------------------------database connection-----------
db = yaml.load(open("db.yaml"),Loader=yaml.FullLoader)
app.config["MYSQL_HOST"] =  db['mysql_host']
app.config["MYSQL_USER"] =  db['mysql_user']
app.config["MYSQL_PASSWORD"] =  db['mysql_password']
app.config["MYSQL_DB"] =  db['mysql_db']

MySQL = MySQL(app)

Db.yaml

mysql_host: "localhost"
mysql_user: "root"
mysql_password: "1234"
mysql_db: "profile"

  • 1
    What's the question ? From your python webapp can you reach mysql:3306 ? – 0x0fba Nov 21 '22 at 07:57
  • @0x0fba is i am able to reach mysql:3306, but I'm having trouble connecting my app and database container. – user9778557 Nov 21 '22 at 08:19
  • What kind of trouble ? Can your webapp connect to your db ? Use `docker logs` can help troubleshooting. – 0x0fba Nov 21 '22 at 08:27
  • My Webapp Container is unable to connect to my MySQL Docker Container, however everything is working properly in my local environment. I can't get my two containers to communicate with one another. – user9778557 Nov 21 '22 at 08:32
  • You can try to update docker-compose to version 3. And to read the log of your containers. – 0x0fba Nov 21 '22 at 08:45
  • Isn't [`links`](https://docs.docker.com/compose/compose-file/#links) required? – bereal Nov 21 '22 at 09:00
  • I would say that `links` isn't required anymore since docker-compose `v2`. – 0x0fba Nov 21 '22 at 09:10
  • `links:` is obsolete and you should delete it. – David Maze Nov 21 '22 at 11:21
  • @user9778557 You've said a couple of times that the application database connection doesn't work. Can you [edit] the question to include the details of that connection and the actual error you're getting? What you've shown so far seems fine. – David Maze Nov 21 '22 at 11:23
  • I have updated the same as you have requested @DavidMaze – user9778557 Nov 21 '22 at 12:52
  • `mysql_host: "localhost"` tells your Flask container to connect to itself, not the separate database container. See among other things [Django connection to postgres by docker-compose](https://stackoverflow.com/questions/42811727/django-connection-to-postgres-by-docker-compose). – David Maze Nov 21 '22 at 14:25
  • When you have a problem, you have to show the logs. How are others supposed to help you without an error message? – Loïc Nov 21 '22 at 18:46

1 Answers1

1

Change in Db.yaml the line mysql_host: "localhost" to mysql_host: "mysql" because the localhost is the same container.

FedeG
  • 106
  • 6