Docker Alpine:加载 MySQLdb 模块时出错

Docker Alpine: Error loading MySQLdb module(Docker Alpine:加载 MySQLdb 模块时出错)

本文介绍了Docker Alpine:加载 MySQLdb 模块时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MariaDB 构建基于 AlpineDjango 应用程序的映像,但我不知道应该添加哪个依赖项我的 Dockerfile 以便我的应用程序可以正确连接到数据库.

I am building an Alpine based image of a Django application with MariaDB and I can't figure out which dependency I should add to my Dockerfile so that my app could properly connect to the DB.

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

好吧,我以为我做到了.从我读到的在本文中,这个讨论,mariadb-devAlpine 相当于基于 Debian 的系统中的 default-libmysqlclient-dev.此外,Alpine 中的 mysql-client 包 只是一个虚拟的包(包含 mariadb-devmariadb-client 等).这是Dockerfile:

Well, I thought I did. From what I read in this article, in this discussion, mariadb-dev is in Alpine the equivalent of default-libmysqlclient-dev in Debian based system. Furthermore, the mysql-client package in Alpine is merely a dummy package (containing mariadb-dev, mariadb-client, etc etc). Here is the Dockerfile:

# pull official base image
FROM python:3.7-alpine

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# set work directory
WORKDIR /usr/src/cms

# install mysqlclient
RUN apk update 
    && apk add --virtual build-deps gcc python3-dev musl-dev 
    && apk add --no-cache mariadb-dev
    && apk del build-deps

# install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /usr/src/cms/Pipfile
RUN pipenv install --skip-lock --system --dev

# copy entrypoint.sh
COPY ./entrypoint.sh /usr/src/cms/entrypoint.sh

# copy project
COPY . /usr/src/cms/

# run entrypoint.sh
ENTRYPOINT ["/usr/src/cms/entrypoint.sh"]

我尝试添加 mariadb-client,以使用 mysql-client 代替.我还尝试添加 RUN pip install django-mysql.似乎什么都没有改变.我成功地构建了 Postgres Django 应用程序,没有任何问题,但是,当谈到 MySQl 与 MariaDB//Debian 与 Alpine 时,我感到困惑.有什么见解吗?

I tried to add mariadb-client, to use mysql-client instead. I also tried to add RUN pip install django-mysql. Nothing seems to change. I successfully build Postgres Django apps without any problem but, when it comes to MySQl vs MariaDB // Debian vs Alpine, I feel confused. Any insight?

推荐答案

您似乎缺少 MySQLdb Python 模块,您应该为该模块安装 mysqlclient Python包:pip install mysqlclient.

It seems you're missing the MySQLdb Python module, for which you should install the mysqlclient Python package: pip install mysqlclient.

在 Alpine 上,pip 将从源代码构建 mysqlclient,因此您需要 gccmusl-dev 用于此设置步骤,因此你需要将 apk del build-deps 推迟到 Python 模块安装之后.

On Alpine, pip will build mysqlclient from source, so you'll need gcc and musl-dev for this setup step, hence you'll need to postpone apk del build-deps to after Python modules are installed.

固定的 Dockerfile 片段:

Fixed Dockerfile snippet:

RUN apk update 
    && apk add --virtual build-deps gcc python3-dev musl-dev 
    && apk add --no-cache mariadb-dev

...

RUN pip install mysqlclient  

RUN apk del build-deps

这篇关于Docker Alpine:加载 MySQLdb 模块时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Docker Alpine:加载 MySQLdb 模块时出错

基础教程推荐