I am running into a very strange behavior.
In a nutshell, importing a python module that’s built from src
does not work when it’s invoked from docker-compose
's command:
key, however it just works fine if it’s invoked from docker run
!!!
I came across this while trying this tutorial. Here are my files in a directory called minimal
:
Dockerfile:
FROM python:2-stretch
RUN mkdir -p /code
WORKDIR /code
ENV PYTHONUNBUFFERED 1
ENV DJANGO_SETTINGS_MODULE composeexample.settings
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker-compose.yml:
version: '3'
services:
db:
image: postgres
web:
build: .
command: python -c "import haystack" # haystack is pulled from git instead of direct build by pip. see requirements.txt
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
requirements.txt
Django==1.5.5
-e git+https://github.com/toastdriven/django-haystack.git@6b67401aea8203fa172ae49a83c2c5356322d8b5#egg=django-haystack
psycopg2==2.5.4
Whoosh==2.6.0
This is what I did to test using compose
:
docker-compose up --build
resulting in:
Successfully built dacd52422510
Successfully tagged minimal_web:latest
minimal_db_1 is up-to-date
Recreating minimal_web_1 ... done
Attaching to minimal_db_1, minimal_web_1
web_1 | Traceback (most recent call last):
web_1 | File "<string>", line 1, in <module>
web_1 | ImportError: No module named haystack
I get the same import error if I do:
docker-compose run -w /code --entrypoint "bash" web
root@ac0b0bf733be:/code$ python
Python 2.7.16 (default, Jun 11 2019, 02:02:48)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import haystack
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named haystack
However if I use docker run
:
docker run -ti --rm minimal_web bash
root@9cd69200519e:/code$ python
Python 2.7.16 (default, Jun 11 2019, 02:02:48)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import haystack
>>>
Everything seems to be ok.
The error only happens when I try to import haystack
which is pulled/built from git, which causes a src
folder be created within container:
root@344cbf269363:/code# ls -l /code/src
total 8
drwxr-xr-x 8 root root 4096 Jun 26 15:54 django-haystack
-rw-r--r-- 1 root root 185 Jun 26 15:54 pip-delete-this-directory.txt
root@344cbf269363:/code#
Attempting to import
any other module (django, whoosh, ...
) works just fine in both scenarios.
So what I am missing here? How can I run this successfully using docker-compose
?
Full code/settings to reproduce the issue is available here.
The image I built is on the hub hamiid/compose_run:1
Thanks in advance
⇒ docker-compose --version
docker-compose version 1.23.2, build 1110ad01
⇒ docker --version
Docker version 18.09.2, build 6247962
macOS 10.13.6
and
> docker-compose --version
docker-compose version 1.24.0, build unknown
> docker --version
Docker version 18.09.6-ce, build 481bc77156
> uname -a
Linux manjaro-macpro 4.19.49-1-MANJARO #1 SMP PREEMPT Sun Jun 9 20:24:20 UTC 2019 x86_64 GNU/Linux