Docker-alpine: pip install mysql-python without installing complete mariadb package

Created on 2 Jun 2016  Β·  23Comments  Β·  Source: gliderlabs/docker-alpine

mysql-python relies on mysql_config for its installation. Trying to pip install it without the complete mariadb package (where mysql_config is found https://pkgs.alpinelinux.org/contents?file=mysql_config&path=&name=&branch=edge&repo=main&arch=armhf), results on an error:

/app # pip install mysql-python
Collecting mysql-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 110kB 729kB/s 
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/tmp/pip-build-GEIJC6/mysql-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-GEIJC6/mysql-python

After installing mariadb-dev package (with mariadb as a dependency) it can find mysql_config and succesfully install mysql-python.

On other distros (debian based) its enought to install libmysqlclient-dev. I suggest to rethink the mariadb related package to allow the installation of python modules without the need to install the entire server. Thanks a lot!

upstream

Most helpful comment

for alpine v3.8

RUN apk add --no-cache mariadb-connector-c-dev ;\
    apk add --no-cache --virtual .build-deps \
        build-base \
        mariadb-dev ;\
    pip install mysqlclient;\
    apk del .build-deps 

All 23 comments

Since this issue deals with packaging I think the alpinelinux irc channel or mailing list would be more helpful. You can find them at http://alpinelinux.org/community/.

Thanks, I have send an email to the devel mailing list.

apk add py-mysqldb

Thanks, I have send an email to the devel mailing list.

I searched this and for reference: http://lists.alpinelinux.org/alpine-devel/5358.html

Any updates on this ? looks like sill there's no way to get mysql-config without entire maria db ? I want to have an alpine image so that i can run django.. but i am unable to install mysqlclient package because it can not fund mysql-config

any new on this? still not able to install the mysql-client package without the full maria-db dependencies (500mb!!!).
furthermore, the apk --update add py-mysqldb before the pip requirement installation does not help me either.
thanks in advance

solution:
https://stackoverflow.com/questions/25682408/docker-setup-with-a-mysql-container-for-a-python-app

in 2 words:
Add apt-get install -y libmysqlclient-dev to your Dockerfile

@Bonus05 that does not work in Alpine.

@keyban I was able to work around it by doing something like this:

RUN apk --no-cache add --virtual build-dependencies \
      build-base \
      py-mysqldb \
      gcc \
      libc-dev \
      libffi-dev \
      mariadb-dev \
      && pip install -qq -r requirements.txt \
      && rm -rf .cache/pip \
      && apk del build-dependencies

RUN apk -q --no-cache add mariadb-client-libs

In the first step I install all the requirements I need for native python deps, then remove them. In the second step I install just mariadb-client-libs which has the files necessary for pip install mysql-python to work.

In Alpine 3.7, I found this was all I needed:

FROM python:3.6-alpine3.7

RUN apk add --no-cache mariadb-dev build-base

which brings in:

(1/26) Installing binutils-libs (2.28-r3)
(2/26) Installing binutils (2.28-r3)
(3/26) Installing gmp (6.1.2-r1)
(4/26) Installing isl (0.18-r0)
(5/26) Installing libgomp (6.4.0-r5)
(6/26) Installing libatomic (6.4.0-r5)
(7/26) Installing pkgconf (1.3.10-r0)
(8/26) Installing libgcc (6.4.0-r5)
(9/26) Installing mpfr3 (3.1.5-r1)
(10/26) Installing mpc1 (1.0.3-r1)
(11/26) Installing libstdc++ (6.4.0-r5)
(12/26) Installing gcc (6.4.0-r5)
(13/26) Installing musl-dev (1.1.18-r2)
(14/26) Installing libc-dev (0.7.1-r0)
(15/26) Installing g++ (6.4.0-r5)
(16/26) Installing make (4.2.1-r0)
(17/26) Installing fortify-headers (0.9-r0)
(18/26) Installing build-base (0.5-r0)
(19/26) Installing libressl2.6-libtls (2.6.3-r0)
(20/26) Installing libressl-dev (2.6.3-r0)
(21/26) Installing zlib-dev (1.2.11-r1)
(22/26) Installing mariadb-common (10.1.28-r1)
(23/26) Installing mariadb-client-libs (10.1.28-r1)
(24/26) Installing libaio (0.3.110-r1)
(25/26) Installing mariadb-libs (10.1.28-r1)
(26/26) Installing mariadb-dev (10.1.28-r1)

Beware: mariadb-dev is 127.11 MB which is a very unlike solution for a docker container.

@lumberj mariadb-client-libs doesn't seem to include mysql_config: https://pkgs.alpinelinux.org/contents?file=mysql_config&path=&name=&branch=&repo=&arch=

That file (required for both the mysql-python and mysqlclient python client libraries) is in mariadb-dev.

Were you making some other point that I'm missing with that comment? Would be great to not need the ~100MB dependency, but currently I don't see a way to do without it.

@paultiplady I believe that mariadb-dev only required to build the mysql python dependencies. The snippet I pasted in the earlier comment has been working for me https://github.com/gliderlabs/docker-alpine/issues/181#issuecomment-348608168

@lumberj is correct. The trick is to temporarily install mariadb-dev which is only needed during build and then delete it. You can save some bandwitdh by installing the runtime deps before deleting the build deps.

Something like:

Docker RUN apk add --no-cache --virtual .build-deps mariadb-dev ... \ && pip install ...\ && apk add --virtual .runtime-deps mariadb-client-libs \ && apk del .build-deps

Aha, got it it. Thanks folks!

py-mysqldb works for python2.

bash-4.3# apk -L info py-mysqldb
py-mysqldb-1.2.5-r0 contains:                                                                                                               
usr/lib/python2.7/site-packages/_mysql.so

For python 3:

apk update &&\
apk add python3 python3-dev mariadb-dev build-base &&\
pip3 install mysqlclient &&\
apk del python3-dev mariadb-dev build-base &&\
apk add mariadb-client-libs

any updates on this issue?

Workaround is to "go-around" the problem. Don't fix this. I learn a lot doing workarounds and ended up doing my own build :D

sudo apt install default-libmysqlclient-dev
this single line helped me only 5mb was used @lumberj @jhourlad

mariadb-client-libs doesn't do the trick anymore, but py-mysqldb does.

but when i run this command, ruturn the following error

RUN apk --no-cache add --virtual build-dependencies \
      build-base \
      py-mysqldb \
      gcc \
      libc-dev \
      libffi-dev \
      mariadb-dev \
      && cd /webserver \
      && pip install --index-url  https://pypi.douban.com/simple -qq -r requirements.txt \
      && apk add --virtual .runtime-deps mariadb-client-libs \
      && apk del .build-deps

err

The command '/bin/sh -c apk --no-cache add --virtual build-dependencies       build-base       py-mysqldb       gcc       libc-dev       libffi-dev       mariadb-dev       && cd /webserver       && pip install --index-url  https://pypi.douban.com/simple -qq -r requirements.txt       && apk add --virtual .runtime-deps mariadb-client-libs       && apk del .build-deps' returned a non-zero code: 1

@M2shad0w use alpine v3.4~3.7,there is no mariadb-client-libs at v3.3 and v3.8
https://pkgs.alpinelinux.org/packages?name=mariadb-client-libs&branch=v3.7&arch=x86_64

for alpine v3.8

RUN apk add --no-cache mariadb-connector-c-dev ;\
    apk add --no-cache --virtual .build-deps \
        build-base \
        mariadb-dev ;\
    pip install mysqlclient;\
    apk del .build-deps 
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dinogun picture dinogun  Β·  3Comments

filiptodoric picture filiptodoric  Β·  3Comments

kooksee picture kooksee  Β·  4Comments

mterzo picture mterzo  Β·  4Comments

oarmstrong picture oarmstrong  Β·  4Comments