Docker Alpine: Error loading MySQLdb module(Docker Alpine:加载 MySQLdb 模块时出错)
问题描述
我正在使用 MariaDB 构建基于 Alpine 的 Django 应用程序的映像,但我不知道应该添加哪个依赖项我的 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-dev
在Alpine 相当于基于 Debian 的系统中的 default-libmysqlclient-dev
.此外,Alpine 中的 mysql-client 包 只是一个虚拟的包(包含 mariadb-dev、mariadb-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
,因此您需要 gcc
和 musl-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 模块时出错
基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01