Search code examples
pythondockeralpine-linuxmysql-connector-python

Docker Alpine build fails on mysqlclient installation with error: Exception: Can not find valid pkg-config name


I'm encountering a problem when building a Docker image using a Python-based Dockerfile. I'm trying to use the mysqlclient library (version 2.2.0) and Django (version 4.2.2). Here is my Dockerfile:

FROM python:3.11-alpine
WORKDIR /usr/src/app
COPY requirements.txt .
RUN apk add --no-cache gcc musl-dev mariadb-connector-c-dev && \
    pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

The problem arises when the Docker build process reaches the point of installing the mysqlclient package. I get the following error: Exception: Can not find valid pkg-config name To address this issue, I tried adding pkgconfig to the apk add command, Unfortunately, this didn't help and the same error persists.

I would appreciate any guidance on how to resolve this issue.

Thank you in advance.


Solution

  • I've managed to solve the issue and here's how I did it: Here is the new Dockerfile:

    FROM python:3.11-alpine
    WORKDIR /usr/src/app
    COPY requirements.txt .
    RUN apk add --no-cache --virtual build-deps gcc musl-dev libffi-dev2 pkgconf mariadb-dev && \
        apk add --no-cache mariadb-connector-c-dev && \
        pip install --no-cache-dir -r requirements.txt && \
        apk del build-deps
    COPY . .
    CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
    

    requirements.txt:

    mysqlclient==2.2.0
    Django~=4.2.0
    

    I hope this will help someone who visits this post in the future.