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

10

28

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.

Species answered 22/6, 2023 at 15:18 Comment(2)
if ti didn't worked, which was the Exception when executing adding pkg-config ?Koger
@Koger same error: Exception: can not find valid pkg-config name.Species
S
1

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.

Species answered 18/7, 2023 at 7:33 Comment(0)
A
32

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

Align answered 26/6, 2023 at 20:40 Comment(0)
E
13

Install build dependencies ( specially - pkg-config )

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config
Essay answered 16/11, 2023 at 11:12 Comment(1)
awesome, I'd installed all the packages instead of pkg-config then I installed it and it's working properlyPiezoelectricity
T
9

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

Tilley answered 22/6, 2023 at 23:57 Comment(0)
P
7

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.

Portuguese answered 27/7, 2023 at 14:38 Comment(0)
S
1

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.

Species answered 18/7, 2023 at 7:33 Comment(0)
A
1

if u are using Mac, u can use this commands

brew install pkg-config && pip3 install mysqlclient==2.2.2

that will work without issue

Anomalistic answered 8/2 at 20:25 Comment(0)
B
1

I had the same problem on python:3.9-slim. Here's what worked for me:

FROM python:3.8-slim 
RUN apt update && apt install -y pkg-config gcc \
    default-libmysqlclient-dev pkg-config
RUN pip install mysqlclient==2.2.1
Blondellblondelle answered 15/3 at 11:9 Comment(0)
K
0

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"]
Koger answered 22/6, 2023 at 15:49 Comment(4)
not working in my system thanks for tryingSpecies
which is the output error?Koger
Exception: Can not find valid pkg-config name. Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually thanks for helpSpecies
Same. Not working on the latest Alpine. Same error as @SpeciesGujarat
P
0

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.

Pasteurization answered 4/7, 2023 at 13:16 Comment(0)
G
0

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; \
Gujarat answered 3/9, 2023 at 20:52 Comment(1)
apk add pkgconfig and apk add --no-cache gcc ... could be combined in 1 call: apk add --no-cache pkgconfig gcc musl-dev mariadb-dev mariadb-connector-c-dev;Seta

© 2022 - 2024 — McMap. All rights reserved.