0

My task is to Dockerize a Python flask application. Part of this application makes use of sqlite to manage the users. The issue I am facing is python is complaining that it cannot find the sqlalchemy-migrate distribution. In order to isolate the application I wrote a tiny to import the same modules as the application.

from sqlalchemy import *
from migrate import *

import pkg_resources

__version__ = pkg_resources.get_provider(
    pkg_resources.Requirement.parse('sqlalchemy-migrate')).version

print __version__

It fails immediately in the second line

Step 23 : RUN python /opt/ipaccess/PresenceInsight2-1.2.1-LTE/sql_repo/test.py
 ---> Running in 84cfb8cdabba
Traceback (most recent call last):
  File "/opt/ipaccess/PresenceInsight2-1.2.1-LTE/sql_repo/test.py", line 2, in <module>
    from migrate import *
  File "/usr/lib/python2.6/site-packages/migrate/__init__.py", line 14, in <module>
    pkg_resources.Requirement.parse('sqlalchemy-migrate')).version
  File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 176, in get_provider
    return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 648, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 546, in resolve
    raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: sqlalchemy-migrate
The command '/bin/sh -c python /opt/ipaccess/PresenceInsight2-1.2.1-LTE/sql_repo/test.py' returned a non-zero code: 1

When I add a RUN command to list all sql distributions I get the output which tells me the distribution is installed.

Step 23 : RUN pip list | grep sql
 ---> Running in c35ef7e5217f
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
sqlalchemy-migrate (0.10.0)
sqlparse (0.1.18)

Additionally I dumped the file list for anything sql related and got the following

Step 23 : RUN ls -Rtl /usr/lib/python2.6/site-packages | grep sql
 ---> Running in f342ebceb1a0
drwxr-xr-x  2 root root   146 Feb 22 11:43 sqlalchemy_migrate-0.10.0.dist-info
drwxr-xr-x  3 root root  4096 Feb 22 11:43 sqlparse
drwxr-xr-x  2 root root   112 Feb 22 11:43 sqlparse-0.1.18-py2.6.egg-info
drwxr-xr-x  2 root root    78 Feb 22 11:42 flask_sqlalchemy
-rw-r--r-- 1 root root 14357 Feb 22 11:43 ansisql.pyc
-rw-r--r-- 1 root root 11422 Feb 22 11:42 ansisql.py
-rw-r--r-- 1 root root  3758 Feb 22 11:43 mysql.pyc
-rw-r--r-- 1 root root 10678 Feb 22 11:43 sqlite.pyc
-rw-r--r-- 1 root root  2164 Feb 22 11:42 mysql.py
-rw-r--r-- 1 root root  7967 Feb 22 11:42 sqlite.py
-rw-r--r-- 1 root root 2387 Feb 22 11:43 sql.pyc
-rw-r--r-- 1 root root 2802 Feb 22 11:42 sql.py
drwxr-xr-x 2 root root  49 Feb 22 11:43 sql_script
/usr/lib/python2.6/site-packages/migrate/versioning/templates/sql_script:
/usr/lib/python2.6/site-packages/sqlalchemy_migrate-0.10.0.dist-info:
/usr/lib/python2.6/site-packages/sqlparse:
-rw-r--r-- 1 root root 26788 Feb 22 11:43 sql.pyc
-rw-r--r-- 1 root root 20897 Feb 22 11:42 sql.py
/usr/lib/python2.6/site-packages/sqlparse/engine:
/usr/lib/python2.6/site-packages/sqlparse-0.1.18-py2.6.egg-info:
drwxr-xr-x 2 root root 103 Feb 22 11:42 sqlalchemy
/usr/lib/python2.6/site-packages/wtforms/ext/sqlalchemy:
/usr/lib/python2.6/site-packages/flask_sqlalchemy:
-rw-r--r-- 2 root root  4737 Jul  1  2015 sqlitelockfile.pyc
-rw-r--r-- 2 root root  4737 Jul  1  2015 sqlitelockfile.pyo
-rw-r--r-- 1 root root  5540 Jun 30  2015 sqlitelockfile.py
-rw-r--r-- 1 root root  55184 Jul 12  2016 sqlitesack.pyc
-rw-r--r-- 1 root root  54756 Jul 12  2016 sqlitesack.pyo
-rw-r--r-- 2 root root   5789 Jul 12  2016 sqlutils.pyc
-rw-r--r-- 2 root root   5789 Jul 12  2016 sqlutils.pyo
-rwxr-xr-x 1 root root  70422 Jul 12  2016 sqlitesack.py
-rwxr-xr-x 1 root root   6422 Jul 12  2016 sqlutils.py
 ---> 83d8f4a1d247

My Dockerfile

FROM centos:6.8

MAINTAINER Carl Wainwright

RUN yum -y install epel-release && yum clean all
# Install python dependencies
RUN \
    yum update -y && yum -y install openldap-servers openldap-clients expect python-devel \
                                    openldap-devel mysql-devel libselinux-python python-pip \
                                    libssl-devel libffi-devel gcc vim \
    yum clean all

ENV APP_NAME presenceinsight2
ENV DIST_NAME PresenceInsight2
ENV APP_VER 1.2.1-LTE
ENV FULL_APP_NAME $DIST_NAME-$APP_VER
ENV TARGET_PATH /opt/ipaccess
ENV APP_TARGET_PATH $TARGET_PATH/$FULL_APP_NAME
ENV SQL_REPO $TARGET_PATH/$FULL_APP_NAME/sql_repo/
ENV CONF_PATH /etc/$APP_NAME
ENV LOG_PATH /var/log/$APP_NAME

RUN \
    mkdir -p $APP_TARGET_PATH \
    mkdir -p $CONF_PATH \
    mkdir -p $LOG_PATH \
    useradd prsncins

WORKDIR $APP_TARGET_PATH

COPY centos_install_slapd.sh /tmp/centos_install_slapd.sh

RUN /tmp/centos_install_slapd.sh presencecell --create_certificate
RUN /tmp/centos_install_slapd.sh presencecell --update_config --unattended

# Setup DB
COPY sql_repo $SQL_REPO
COPY requirements.txt $APP_TARGET_PATH

RUN pip install -r requirements.txt

# Test script (which fails)
RUN python /opt/ipaccess/PresenceInsight2-1.2.1-LTE/sql_repo/test.py

RUN \
    python $APP_TARGET_PATH/sql_repo/manage.py version_control --repository=sql_repo --url=sqlite:///$CONF_PATH/app.db \
    touch $APP_TARGET_PATH/db-version-control-run.txt \
    python $APP_TARGET_PATH/sql_repo/manage.py db_version --repository=sql_repo --url=sqlite:///$CONF_PATH/app.db \
    python $APP_TARGET_PATH/sql_repo/manage.py upgrade --repository=sql_repo --url=sqlite:///$CONF_PATH/app.db

# Install application
COPY dist/$FULL_APP_NAME.tar.gz /tmp/

RUN tar zxvf /tmp/$FULL_APP_NAME.tar.gz -C $TARGET_PATH/

EXPOSE 8484

CMD python app/run.py

Any ideas as to why this is failing in Docker?

If I setup a virtualenv on my localmachine and install the same pip distributions the test.py script executes successfully.

Carl Wainwright
  • 328
  • 5
  • 18
  • Show the Dockerfile, and you may `docker run -it your_image bash` and then check your files are present (or not) – user2915097 Feb 22 '17 at 14:08
  • Added Dockerfile. I cannot run the image as it fails to build, though I could in theory comment out everything forward from the point of failure to complete the build process - which I will try now. – Carl Wainwright Feb 22 '17 at 14:23
  • So I did as I suggested and was able to bash into the image. I can confirm the sqlalchemy-migrate distribution is present using the 'pip list' command. Additionally I verified that there is a folder under '/usr/lib/python2.6/site-packages/' for this distribution 'sqlalchemy_migrate-0.10.0.dist-info' – Carl Wainwright Feb 22 '17 at 14:28
  • fix the failing `docker build` to begin – user2915097 Feb 22 '17 at 14:38
  • 1
    If I could fix the build I would not have posted the question ;-) – Carl Wainwright Feb 22 '17 at 15:17
  • 1
    is your PYTHONPATH set properly? That's usually the problem. Try `which python` and see where it's looking for the files. – MrE Feb 22 '17 at 16:32

1 Answers1

0

My guess is that your PYTHONPATH is not set properly and you may have the files installed, but it's not looking at the right place.

MrE
  • 19,584
  • 12
  • 87
  • 105
  • PYTHONPATH is not set (same goes for my virtualenv). The output of `which python` points to _/usr/bin/python_ – Carl Wainwright Feb 22 '17 at 17:03
  • I ran python with verbose settings and oddly enough I can see that imports of sqlalchemy and migrate go fine until it after it imports `migrate.changeset.databases.ibmdb2` – Carl Wainwright Feb 22 '17 at 17:17