4

I am new to Docker and trying to deploy a django project with the same. But after building the docker-compose I am getting this error:

ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt' ERROR: Service 'web' failed to build: The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

I might be messing up in setting the correct path so here is my project structure

~/Desktop/Projects/ToDoApp
   ToDoApp
     settings.py
   docker-compose.yml
   Dockerfile
   manage.py
   requirements.txt

Here is my dockerfile

#pull official base image
FROM python:3

#set envionment variables
ENV PYTHONUNBUFFERED 1

# Adding requirements file
ADD requirements.txt ToDoApp/ToDoApp

#set work directory
WORKDIR /ToDoApp

#install dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

CMD ["python", "./ToDoApp/manage.py runserver 0.0.0.0:8000"]

and this is my docker-compose.yml

version: '3'

services:
  web:
    build: .
    command: python /ToDoApp/manage.py runserver 0.0.0.0:8000
    volumes:
        - .:/Desktop/Projects/ToDoApp
    ports:
        - "8000:8000"
    environment:
      - SECRET_KEY = please_change_me
6
  • 1
    I think because you put requirements.txt in a subdirectory, ToDoApp/ToDoApp/, or, provided that subdir didn't exist, you renamed it to ToDoApp/ToDoApp.
    – 9769953
    Commented Apr 24, 2019 at 9:40
  • 1
    Note that your ADD command uses a relative directory, while WORKDIR uses an absolute directory. Unless your Docker working directory was / first, I assume this won't be correct.
    – 9769953
    Commented Apr 24, 2019 at 9:41
  • this may help Commented Apr 24, 2019 at 9:43
  • my requirements.txt is in ToDoApp that's why i added it to ToDoApp/ToDoApp in my dockerfile. Where do you suggest thwefile should be? Commented Apr 24, 2019 at 9:45
  • do WORKDIR /ToDoApp/ToDoApp and try Commented Apr 24, 2019 at 9:46

2 Answers 2

11

Several issues I'm seeing:

  • The ADD command you use creates a file called ToDoApp/ToDoApp, it doesn't even create a sub directory.
  • ADD is unneeded (you're not extracting a tar or downloading from a URL) so that can switch to a COPY.
  • You need to copy your code.
  • The RUN commands can be reordered for better cache efficiency.
  • Use relative paths and the WORKDIR correctly.
  • Args need to be separated when you use the json syntax

The resulting Dockerfile looks like:

FROM python:3

#set envionment variables
ENV PYTHONUNBUFFERED 1

# run this before copying requirements for cache efficiency
RUN pip install --upgrade pip

#set work directory early so remaining paths can be relative
WORKDIR /ToDoApp

# Adding requirements file to current directory
# just this file first to cache the pip install step when code changes
COPY requirements.txt .

#install dependencies
RUN pip install -r requirements.txt

# copy code itself from context to image
COPY . .

# run from working directory, and separate args in the json syntax
CMD ["python", "./manage.py", "runserver", "0.0.0.0:8000"]
0
0

Try this:

FROM python:3

#set envionment variables
ENV PYTHONUNBUFFERED 1

#set work directory
WORKDIR /ToDoApp

# Adding requirements file
ADD requirements.txt /ToDoApp/

#install dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

CMD ["python", "./ToDoApp/manage.py runserver 0.0.0.0:8000"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.