skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    Here is also my final solution:

    1. Build the mysqlclient wheel
        FROM python:3.11.7-slim-bookworm as mariadb_client_builder
        
        # Install build dependencies
        RUN apt-get update && 
            apt-get install -y gcc pkg-config curl
        
        RUN curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | bash && 
            apt-get update && 
            apt-get install -y libmariadb-dev
        
        # Install mysqlclient, building it into a wheel file
        RUN pip wheel --no-cache-dir --wheel-dir=/usr/src/app/wheels mysqlclient
    
    1. Using the wheel at your final image:
        COPY --from=mariadb_client_builder /usr/src/app/wheels /wheels
    
        # Setup virtualenv
        RUN pip install --upgrade pip && 
            pip install setuptools virtualenv && 
            python -m virtualenv /venv && 
            . /venv/bin/activate && 
            pip install --no-cache /wheels/* && 
            pip install -r requirements.txt && 
            deactivate
    

    Make sure mysqlclient isn't part of your requirements.txt anymore, as we installed it directly from the pre-compiled wheel into our venv.

    Attention, the Pypi mysqlclient also needs the libmariadb-dev package to be installed in your final image, so you are absolutely required to also do the following in your final image:

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

    You basically need the libmariadb-dev package for building the wheel and also to later connect with your final image to the mariadb/mysql instance.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search