我们如何使用docker来使用peewee和mysql?

问题描述 投票:0回答:1

我正在尝试使用Docker连接到Mysql数据库,但是出现以下错误:

peewee.ImproperlyConfigured: MySQL driver not installed!

这是我的Dockerfile

FROM python:3.7
ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY . /app

RUN pip install peewee
RUN python create_db.py

这是我的docker-compose.yml

version: '3.3'

services:

    app-mariadb:
        image: mariadb:10.4     
        container_name: app-mariadb                
        ports:
            - "3320:3306"
        environment:
            MYSQL_ROOT_PASSWORD: helloWORLD123
            MYSQL_DATABASE: sp_odesi
            MYSQL_USER: root
            MYSQL_PASSWORD: helloWORLD123

    app-python:
        build: .        
        container_name: app-python      
        command: python create_db.py
        restart: always        
        working_dir: /app
        depends_on:
            - app-mariadb
        volumes:
            - ./:/app
        environment:
            MYSQL_HOST: db
            MYSQL_DATABASE: sp_odesi
            MYSQL_USER: root
            MYSQL_PASSWORD: helloWORLD123
            MYSQL_PORT: 3306
            DATABASE: MARIADB

create_db.py

from peewee import *
from datetime import datetime
import os


db = MySQLDatabase(
        "sp_odesi",user="root",
        passwd="helloWORLD123",port=3306
    )

class Surveys(Model):
    """Surveys

    """
    survey_name     = CharField(max_length=100, unique=True, null=False)

    class Meta:
        database = db

if __name__ == '__main__':
    Surveys.create_table()
    print("Create table")
python-3.x docker mysql-connector peewee
1个回答
0
投票

required a mysql client library to be installed。从[]更改Dockerfile中的以下行

RUN pip install peewee PyMySQL

RUN pip install peewee
© www.soinside.com 2019 - 2024. All rights reserved.