Python module not found while run from `docker-compose` (ok when run from `docker run`)

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:


FROM python:2-stretch
RUN mkdir -p /code
ENV DJANGO_SETTINGS_MODULE composeexample.settings
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/


version: '3'

    image: postgres
    build: .
    command: python -c "import haystack" # haystack is pulled from git instead of direct build by pip. see requirements.txt
      - .:/code
      - "8000:8000"
      - db


-e git+

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

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


> 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

Try to add module to requirements.txt and run

docker-compose build

@hamiid , Did you solve it? Could you please share your solution. I have the same issue