Installing Mysqlclient in Python Slim Docker Image

December 29, 2022

Installing the mysqlclient python package in a python:3.11-slim docker container fails because the slim version leaves out the libraries needed to compile mysqlclient. Rather than using the full python:3.11 base image that is much larger, you can install the mysqlclient dependencies manually:

FROM python:3.11-bullseye

# Install mysqlclient debian package dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
   libmariadb-dev-compat gcc                   `: MySQL client` \
&& rm -rf /var/lib/apt/lists/*

# Install mysqlclient python package
RUN pip install --no-cache-dir mysqlclient