flask mysql docker,使用docker-compose从Flask Application连接到MySQL

I have an application using Flask and MySQL. The application does not connect to MySQL container from the Flask Application but it can be accessed using Sequel Pro with the same credentials.

Docker Compose File

version: '2'

services:

web:

build: flask-app

ports:

- "5000:5000"

volumes:

- .:/code

mysql:

build: mysql-server

environment:

MYSQL_DATABASE: test

MYSQL_ROOT_PASSWORD: root

MYSQL_ROOT_HOST: 0.0.0.0

MYSQL_USER: testing

MYSQL_PASSWORD: testing

ports:

- "3306:3306"

Docker file for MySQL

The docker file for MySQL will add schema from test.dump file.

FROM mysql/mysql-server

ADD test.sql /docker-entrypoint-initdb.d

Docker file for Flask

FROM python:latest

COPY . /app

WORKDIR /app

RUN pip install -r requirements.txt

ENTRYPOINT ["python"]

CMD ["app.py"]

Starting point app.py

from flask import Flask, request, jsonify, Response

import json

import mysql.connector

from flask_cors import CORS, cross_origin

app = Flask(__name__)

def getMysqlConnection():

return mysql.connector.connect(user='testing', host='0.0.0.0', port='3306', password='testing', database='test')

@app.route("/")

def hello():

return "Flask inside Docker!!"

@app.route('/api/getMonths', methods=['GET'])

@cross_origin() # allow all origins all methods.

def get_months():

db = getMysqlConnection()

print(db)

try:

sqlstr = "SELECT * from retail_table"

print(sqlstr)

cur = db.cursor()

cur.execute(sqlstr)

output_json = cur.fetchall()

except Exception as e:

print("Error in SQL:\n", e)

finally:

db.close()

return jsonify(results=output_json)

if __name__ == "__main__":

app.run(debug=True,host='0.0.0.0')

When I do a GET request on http://localhost:5000/ using REST Client I get a valid response.

A GET request on http://localhost:5000/api/getMonths gives error message:

mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '0.0.0.0:3306' (111 Connection refused)

When the same credentials were used on Sequel Pro, I was able to access the database.

LdPon.png

Please advice me on how to connect the MySQL container from the Flask Application. This is my first time suing Docker and do forgive me if this is a silly mistake from my part.

解决方案

Change this

return mysql.connector.connect(user='testing', host='0.0.0.0', port='3306', password='testing', database='test')

to

return mysql.connector.connect(user='testing', host='mysql', port='3306', password='testing', database='test')

Your code is running inside the container and not on your host. So you need to provide it a address where it can reach within container network. For docker-compose each service is reachable using its name. So in your it is mysql as that is name you have used for the service

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值