Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 976
noisyboiler Avatar asked Sep 05 '14 09:09

noisyboiler


2 Answers

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

like image 72
Chris McKinnel Avatar answered Sep 18 '22 15:09

Chris McKinnel


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
like image 38
Belter Avatar answered Sep 19 '22 15:09

Belter