简体   繁体   English

如何使用 mysql 数据库 dockerize 我的 Flask web 应用程序? 无法连接 mysql 并且找不到渲染模板

[英]How to dockerize my flask web app with mysql database?Cannot connect mysql and cannot found render templete

I am new to docker and trying to deploy this web app but cannot find solution.我是 docker 新手,正在尝试部署此 Web 应用程序,但找不到解决方案。 I'm running windows 10 and Docker Desktop.我正在运行 Windows 10 和 Docker 桌面。 My docker-version is 19.03.5 and docker-compose version 1.25.4, build unknown.我的 docker-version 是 19.03.5 和 docker-compose 版本 1.25.4,构建未知。

My directory structure is:我的目录结构是:

  -docker-compose.yml
  -app
     -static
          js/
            script.js
          css/
              style.css
     -templates
            -index.html
     -app.py
     -dockerfile
     -requirements.txt
  -db
    -init.sql

docker-compose.yml docker-compose.yml

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

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

dockerfile文件

FROM python:3.6
EXPOSE 5000
RUN mkdir -p /app
RUN ls /app
COPY static /app/static
COPY templates /app/templates
COPY app.py /app
COPY requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD python app.py

requirements.txt要求.txt

flask
mysql-connector

app.py应用程序

from flask import Flask, render_template, request
import mysql.connector
import os

app = Flask(__name__, template_folder='/templates', static_folder='/static')
@app.route('/')
def index():
    connection = mysql.connector.connect(
        user = 'root',
        password = 'root',
        host= 'db',
        port = '3306',
        database = 'test'
    )
    cursor = connection.cursor()
    cursor.execute("SELECT name from place")
    names = cursor.fetchall()
    return render_template('index.html', names = names)
if __name__ == '__main__':
    app.run(host='0.0.0.0')

My docker command is –我的 docker 命令是 –

docker-compose up

Then trying to access http://0.0.0.0:5000/ but get mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'db:3306' and jinja2.exceptions.TemplateNotFound:然后尝试访问http://0.0.0.0:5000/但得到 mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'db:3306' and jinja2.exceptions.TemplateNotFound:

jinja2.exceptions.TemplateNotFound jinja2.exceptions.TemplateNotFound

Your flask folder paths are wrong, it should be either an absolute path like /app/templates and /app/static or a relative one like templates abnd static .您的烧瓶文件夹路径是错误的,它应该是像/app/templates/app/static这样的绝对路径,或者像templates abnd static这样的相对路径。

mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'db:3306' mysql.connector.errors.InterfaceError: 2003: 无法连接到 'db:3306' 上的 MySQL 服务器

If your init.sql script is correct then there is nothing wrong with your database, you just have let it initialize properly (wait for the mysqld: ready for connections message) before sending your first request.如果您的 init.sql 脚本是正确的,那么您的数据库没有任何问题,您只需在发送第一个请求之前让它正确初始化(等待mysqld: ready for connections消息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM