Docker Alpine: Error loading MySQLdb module
Asked Answered
G

4

14

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?

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"]

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?

Grayback answered 8/5, 2019 at 20:32 Comment(0)
M
40

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

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.

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
Massage answered 10/5, 2019 at 6:58 Comment(6)
I am doing exactly the same, but still receiving the same error. Can you please help?Dipper
yep. doesn't work for me either. long and lengthy crash of the dependency build.Subservience
The only necessary dependency is mariadb-connector-c. No need to install the big fat mariadb-dev.Tradition
To clarify the comment by @Tradition , you only need mariadb-connector-c for the mysqlclient python module to function post-install, but you DO need mariadb-dev to build it. This is relevant if you are trying to slim your Docker image down.Astringent
so you can install mariadb-dev to build and later uninstall it and keep only with mariadb-connector-cToaster
In this example, deleting build-deps in a dedicated RUN instructions will keep it inside the main image. All above commands should be performed inside the same RUN instrucitons in order to make image thinner.Sevenfold
C
9

Mainly you need to install mariadb-connector-c-dev package. But only this package will give compilation errors. So additionally you will need to add gcc and musl-dev packages into Dockerfile. This will make Django and MySQL work within alpine image.

FROM python:3.8-alpine

RUN apk add gcc musl-dev mariadb-connector-c-dev
Carlettacarley answered 30/6, 2021 at 7:20 Comment(0)
M
1

2023.05.06 - 2023.06.08 During this time I explicitly used the following code and it worked:

RUN apk add musl-dev mariadb-connector-c-dev gcc && \
    pip install mysqlclient && \
    pip cache purge && \
    apk del --rdepends --purge musl-dev gcc

But now, 2023.06.25, I found that I had to add mariadb-dev to successfully install mysqlclient.

Menedez answered 25/6, 2023 at 1:15 Comment(0)
S
0

Just as an addition - you can also just install the apk package instead of the pip module:

RUN apk add --no-cache py3-mysqlclient

It will install the needed dependencies automatically.

Spirt answered 17/6 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.