0

I have a folder ./lib/my-python-library that contains a python package on the host system:

./Dockerfile
./lib/my-python-library/setup.py
                      my_python_library
                      ...

Now, I copy that path into my docker container with

COPY ./lib/my-python-library /my-python-library

RUN cd /my-python-library && pip install -e .

Which works. I can now import the library when I execute bash and run python -c 'import my_python_library.

However, I want to be able to modify the code on the fly without having to restart the container, which usually works. However, when I mount that specific library directory with docker-compose:

docker-compose.yml (snippet):

services:
  web:
    build: .
    env_file:
      - ./.env
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./app/:/app/
      - ./lib/my-python-library:/my-python-library/

And run the container, docker-compose creates an empty directory named my_python_library in the root of my project host and mounts that new empty directory. Instead, it should mount my-python-library.

Is that a general limitation of docker-compose that directories with dashes are not mountable or am I doing something wrong?

Update: It seems docker-compose is unable to mount the dashed . When I rename the directory on the host to my_python_library and make the corresponding changes in the docker-compose.yml

This works:

./lib/my_python_library/setup.py
                        my_python_library
                        ...
Soerendip
  • 7,684
  • 15
  • 61
  • 128
  • Could you clear up what is and what should be? It is quite odd to me that you jump from *I have `my_python_library` in my app* to *I mount the folder `lib/my-python-library` to `my-python-library`*. For now, with what you are explaining, the behaviour you observer makes sense, `my_python_library` is empty because you are mounting on `my-python-library` – β.εηοιτ.βε Apr 19 '21 at 19:21
  • Added more explanation. – Soerendip Apr 19 '21 at 23:17
  • Odd, I do not observer the same: https://gist.github.com/b-enoit-be/bccaba6294345ac798bbc061dea03bf2 – β.εηοιτ.βε Apr 20 '21 at 21:36
  • Now the thing is: when you mount a folder from compose, this **will** totally overwrite the folder of the container at run time. So that means that whatever was done at build time of the container will be lost. Totally unrelated to what you try is this other question where I explained this deeper that what a comment allows me to: https://stackoverflow.com/questions/43769756/composer-install-doesnt-install-packages-when-running-in-dockerfile/46458246#46458246 – β.εηοιτ.βε Apr 20 '21 at 21:39
  • So to make a parallel of what is said on that other question in the Python world, what you should do is to run your `pip install` in an entrypoint. More on that: https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile – β.εηοιτ.βε Apr 20 '21 at 21:44
  • That is strange. – Soerendip Apr 20 '21 at 21:52

1 Answers1

1

I just had the exact same problem (volume names coming from the .env file). I fixed the issue by quoting the volumes:

services:
  web:
    build: .
    env_file:
      - ./.env
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - "./app/:/app/"
      - "./lib/my-python-library:/my-python-library/"

I wasn't able to find something upstream (it might be reported at some point to at least have a warning or throw an error.