-1

I'm using Docker 19. I have this in my docker-compose.yml file. I'm trying to mount a volume from my local machine ...

  python:
    build: ./
    env_file: /Users/davea/Documents/workspace/my_python_project/tests/.test_env
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=${LOCAL_DB_PASSWORD}
      - DB_HOST=sql-server-db
      - DB_NAME=${LOCAL_DB_DB}
      - DB_USER=${LOCAL_DB_USERNAME}
      - DB_PASS=${LOCAL_DB_PASSWORD}
      - DB_PORT=1433
    volumes:
    - /Users/davea/Documents/workspace/my_python_project:/my-app
    depends_on:
      - sql-server-db

How do I reference this volume in my Dockerfile? I tried this

WORKDIR /my-app
...
RUN pip3 install -r /my-app/requirements.txt

but am getting this error

ERROR: Could not open requirements file: [Errno 2] No such file or directory: '/my-app/requirements.txt'

I have verified that "/Users/davea/Documents/workspace/my_python_project/requirements.txt" is a valid file on my system.

Dave
  • 15,639
  • 133
  • 442
  • 830

2 Answers2

0

Not sure what your motivation is, but I don't think you can bind a volume at build time, so the idea won't work.

Alternatively, if your motivation is to make it dynamic (maybe dynamic requirements file) then you can make use of ARGS:

ARG REQ_FILE=requirements.txt
ADD $REQ_FILE .  # assuming you already are inside your my-app workdir
...
RUN pip3 install -r $REQ_FILE

And with that just run build:

docker build --build-arg REQ_FILE=requirements.txt ...
dreyus95
  • 164
  • 3
  • Hi, You mentioned "assuming you already are inside your my-app workdir", but I am not inside that directory. The requirements.txt file is located one level above my Dockerfile. How would I copy the file in that event? – Dave Aug 03 '20 at 14:15
  • Try building the image from 1 folder above Dockerfile folder then. – dreyus95 Aug 03 '20 at 14:47
0

You don't. Volumes are intended to hold data, not code; you should COPY your code into your image and delete the volumes: line.

A very routine minimal Python Dockerfile can look like:

FROM python:3.8
WORKDIR /my-app
COPY . ./
RUN pip3 install -r requirements.txt
CMD ["./my-app.py"]

Since the COPY line is there, the image contains everything it needs to run itself, including its own code and a default command. That means it's available at build time, and it means you can delete the volumes: block from your docker-compose.yml file.

Many of the things you can specify in the docker-compose.yml just aren't visible or accessible at build time. This includes volumes, networks, and environment variable settings; your build can't connect to other containers.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • The files I need are one directory level up from my Dockerfile. I was under the impression I can't copy files from a level above. – Dave Aug 03 '20 at 14:14
  • 1
    You can run `docker build` from one level higher, and include the subpaths in places like `COPY` instructions. See also [How to include files outside of Docker's build context?](https://stackoverflow.com/questions/27068596/how-to-include-files-outside-of-dockers-build-context). – David Maze Aug 03 '20 at 16:55