Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 301
IdanB Avatar asked Sep 02 '25 15:09

IdanB


2 Answers

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. :D

like image 105
Ariel Carvalho Avatar answered Sep 05 '25 15:09

Ariel Carvalho


Install build dependencies ( specially - pkg-config )

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config
like image 36
Dïnuth Perera Avatar answered Sep 05 '25 15:09

Dïnuth Perera