2

I want to install Python's mysqlclient package on a Docker container running Ubuntu. The installation fails because I don't have gcc installed in the container:

x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Dversion_info=(1,3,7,'final',1) -D__version__=1.3.7 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -fabi-version=2 -fno-omit-frame-pointer
    unable to execute 'x86_64-linux-gnu-gcc': No such file or directory
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

However, I do not want to install gcc in the container. Is there a wheel available for mysqlclient? I cannot find any.

3 Answers 3

1

You can install the build dependencies, make the module, then remove them.

Redis is a good example of how to build and cleanup in one step so you don't create a bulky image layer.

RUN buildDeps='gcc libc6-dev make' \
    && set -x \
    && apt-get update && apt-get install -y $buildDeps --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /usr/src/redis \
    && curl -sSL "$REDIS_DOWNLOAD_URL" -o redis.tar.gz \
    && echo "$REDIS_DOWNLOAD_SHA1 *redis.tar.gz" | sha1sum -c - \
    && tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 \
    && rm redis.tar.gz \
    && make -C /usr/src/redis \
    && make -C /usr/src/redis install \
    && rm -r /usr/src/redis \
    && apt-get purge -y --auto-remove $buildDeps
Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking about doing that. I wanted to avoid doing that because apt-get shows that gcc has a lot of dependencies. What I'll do instead build in another container that has everything and generate a wheel that I'll copy over to the container I'll actually use. I assume it's possible to generate a wheel for Linux and reuse it (I ask because such wheels don't exist).
1

I created a container and installed everything I needed to build mysqlclient. I downloaded the .tgz and used python setup.py bdist_wheel to generate the Wheel file. I copied over the wheel file to a directory mounted from my host machine, and installed it on the container where I wanted mysqlclient installed. This way I didn't have to remove the packages I downloaded. I saved the instance of the container with gcc as a separate image for future use.

Comments

0

A good solution for when you need to compile stuff but you don't want to pollute your image with the build stuff is to use multi-stage builds.

With multi-stage builds you build a first image with all tools and data needed for the compilation and you then build a second image where you copy only the result from the first image. It's also useful if you need some secrets (like SSH keys to access git private repos) but don't want those secrets in the final image.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.