5

I have one container running "dockerfile/mysql" which I can connect to and interact with. I'm trying to build another image for a Python application that will read from a mysql db. The problem is that the Python app requires MySQL-python and attempts to install it on setup. Because this container does not hold the mysql server, i end up with;

Downloading/unpacking MySQL-python
Downloading MySQL-python-1.2.5.zip (108kB): 108kB downloaded
Running setup.py (path:/tmp/pip_build_vagrant/MySQL-python/setup.py) egg_info for package MySQL-python
sh: 1: mysql_config: not found
Traceback (most recent call last):
  File "<string>", line 17, in <module>
  File "/tmp/pip_build_vagrant/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
Complete output from command python setup.py egg_info:
sh: 1: mysql_config: not found

which is fully understandable.

How should i set up my Python app container (which is using SQLAlchemy) to read from the mysql container?

Thanks

4 Answers 4

14

Add apt-get install -y libmysqlclient-dev to your Dockerfile.

Sign up to request clarification or add additional context in comments.

2 Comments

i'm aware that doing so would solve my problem but won't this add a mysql server to my app container? which i don't want.
Nope, it installs the client, not the server. To install the server, apt-get install -y mysql-server - not what you want.
10

If you use python:3.5-alpine, you can install mysqlclient by adding following code into your Dockerfile:

RUN set -e; \
        apk add --no-cache --virtual .build-deps \
                gcc \
                libc-dev \
                linux-headers \
                mariadb-dev \
                python3-dev \
                postgresql-dev \
        ;

The whole Dockerfile will be like this:

# Version: 0.0.1
FROM python:3.5-alpine
ENV PYTHONUNBUFFERED 1
RUN set -e; \
        apk add --no-cache --virtual .build-deps \
                gcc \
                libc-dev \
                linux-headers \
                mariadb-dev \
                python3-dev \
                postgresql-dev \
        ;
RUN mkdir /djcode
WORKDIR /djcode
ENV REFRESHED_AT 2017-12-25
ADD requirements.txt /djcode/
RUN pip install --no-cache-dir -r /djcode/requirements.txt
RUN pip install uwsgi
ADD . /djcode/
EXPOSE 6001

Comments

2

To avoid unproportional increasing of the image size (extra 300MB), you can delete the packages from the image after mysqlclient has been built. So you can do something like this:

  1. In your dockerfile add the following lines:

    COPY ./apk_deps.sh ./apk_deps.sh
    RUN ./apk_deps.sh
    
  2. Create apk_deps.sh file with the following lines:

    #! /bin/sh
    set -e
    echo "apk add --no-cache --virtual .build-deps gcc libc-dev linux-headers mariadb-dev python3-dev"
    apk add --no-cache --virtual .build-deps gcc libc-dev linux-headers mariadb-dev python3-dev
    echo "pip install mysqlclient"
    pip install mysqlclient
    echo "apk del .build-deps"
    apk del .build-deps
    apk add --no-cache mariadb-client-libs
    

This way my image size increased only by 7MB.

Comments

0

if you're trying to set up mysql on alpine linux, the correct resolution to this problem is to add the mariadb-connector-c package with this command

apk update && apk add mariadb-connector-c

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.