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

Question:

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.

Asked By: IdanB

||

Answers:

Have you tried updating before adding pkgconfig ? I’ve tried and it works perfectly.

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

mysqlclient 2.2.0 added a dependency on pkg-config, which might not exist as such on alpine (update: it appears to, now). See https://github.com/PyMySQL/mysqlclient/issues/620

You might have luck pinning to a prior version of mysqlclient

Answered By: Dave W. Smith

I’m using python:3.11.3-slim-bullseye instead of python:3.11-alpine image, but I had the same problem.
So you have 2 options:

  1. Downgrade the mysqlclient to a previous version, ex: mysqlclient==2.1.1.
  2. And now, since the pkg-config is needed for mysqlclient==2.2.0 and on. Add pkg-config to the container. Would be something like this…
FROM python:3.11.3-slim-bullseye

RUN apt-get update 
    && apt-get upgrade -y 
    && apt-get install -y gcc default-libmysqlclient-dev pkg-config 
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY . .

RUN pip install --upgrade pip 
    && pip install mysqlclient 
    && pip install -r requirements.txt

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

TIP: Maybe you’re lacking the instalation of the default-libmysqlclient-dev or libmysqlclient in the container.

Hope it helps. 😀

Answered By: Ariel Carvalho

Disclaimer: I’m using a local WSL2 environment so this solution might not be applicable to your situation.

I followed the build instructions at mysqlclient PyPi page so you may consider adding those build instructions to your Dockerfile.

Answered By: Thinh Nguyen

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.

Answered By: IdanB

I had this issue inside of a slim Docker image (python3.10-slim).

sudo apt update && sudo apt install pkg-config

This did the trick for me.

Answered By: Frexom

I am using Alpine 3.18.3, Django >= 4.2.0, and mysqlclient >= 2.2.0. To get this combination working the following Dockerfile built successfully and app started in the container:

FROM python:alpine
apk update && apk upgrade; 
apk add pkgconfig; 
apk add --no-cache gcc musl-dev mariadb-dev mariadb-connector-c-dev; 
Answered By: Brady