Search code examples
pythondockermariadbdriver

Installing mariaDB client efficiently inside docker


my Python application requires the following package -> https://pypi.org/project/mysqlclient/

The pain for me here is that the installation of this packages requires the following build stage at my Dockerfile:

RUN curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash && \
    apt-get update && \
    apt-get install -y mariadb-client libmariadb-dev-compat libmariadb-dev

To make this RUN command properly work, you also need to install gcc and pkg-config, otherwise the mariadb-client can't be compiled during installation. This way of installation is kinda ugly inside docker as it massively blows up the image size due to the installation of gcc ...

How can I have these packages installed without having to install gcc ??? Are there any pre-compiled binaries I can use here?

Thanks in advance.


Solution

  • Use a multi-stage build to build the mysqlclient wheel in the first stage, then copy it over to the runtime stage. This leaves gcc and friends only in the build stage.

    You can of course do this for all of your requirements.txt too – pip wheel -w /deps -r requirements.txt or whatnot.

    FROM python:3.12 as mysqlclient-build
    RUN curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | bash 
    RUN apt-get update
    RUN apt-get install -y mariadb-client libmariadb-dev-compat libmariadb-dev
    RUN pip wheel -w /deps mysqlclient
    
    FROM python:3.12
    COPY --from=mysqlclient-build /deps/*.whl /deps
    RUN pip install /deps/*.whl && rm -rf /deps
    # ... the rest of your actual application bits
    

    Depending on whether the mysqlclient wheel ends up statically linking the runtime libmariadb library, you may or may not need to also apt-get install that in the runtime stage.