noisyboiler
noisyboiler

Reputation: 341

Docker Setup with a Mysql Container for a Python App

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

Upvotes: 5

Views: 16216

Answers (4)

Jake Boomgaarden
Jake Boomgaarden

Reputation: 3596

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

Upvotes: 0

Belter
Belter

Reputation: 3807

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

Upvotes: 10

BezBran
BezBran

Reputation: 161

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.

Upvotes: 2

Chris McKinnel
Chris McKinnel

Reputation: 15082

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

Upvotes: 14

Related Questions