docker-compose error - unable to read file

7.8k views Asked by At

I have a simple docker file for a test django application. I am running docker-compose as root user and I can read the requirements.txt file, but docker is erroring out. Any advice?

root@testdeploy:/app/test-project# docker-compose build
db uses an image, skipping
Building web...
Step 0 : FROM python:2.7
 ---> 93d77aec17a0
Step 1 : ENV PYTHONUNBUFFERED 1
 ---> Using cache
 ---> 25e2164606e7
Step 2 : WORKDIR /app/test-project/
 ---> Using cache
 ---> 366c21333f1f
Step 3 : RUN pip install -r /app/test-project/requirements.txt
 ---> Running in 75599e289c5e
Could not open requirements file: [Errno 2] No such file or directory: '/app/test-project/requirements.txt'
Service 'web' failed to build: The command [/bin/sh -c pip install -r /app/test-project/requirements.txt] returned a non-zero code: 1

Reading the file from command line.

root@testdeploy:/app/test-project# head -5 /app/test-project/requirements.txt
Cheetah==2.4.4
Django==1.7
Landscape-Client==14.01
PAM==0.4.2
Pillow==2.6.1

My docker file.

FROM python:2.7
ENV PYTHONUNBUFFERED 1
WORKDIR /app/test-project/
RUN pip install -r /app/test-project/requirements.txt
ADD . /app/test_project/
2

There are 2 answers

1
Paul On BEST ANSWER

The docker build executes in a container separate from your host. The container has its own filesystem and is unaware of files on the host system.

You may copy files to the container by using

COPY /path/on/host /path/on/container

in the Dockerfile

Currently pip needs files, but they have not been added.

In your case, you can move the ADD line at the bottom to before the pip command. It would also be better practice to use COPY, but that is not the bug.

Like this:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
COPY . /app/test_project/
WORKDIR /app/test-project/
RUN pip install -r /app/test-project/requirements.txt

If that still doesn't work, you need to adjust . to be the correct /path/to/test-project on the host.

0
captnolimar On

One issue to be aware of for future searchers is that the path is relative to the directory of the current Dockerfile that docker-compose is executing.

This means that if your file is in the same level as your docker-compose.yml and your Dockerfile is in another directory, you should move it to the directory where the Dockerfile is.