skip to Main Content

This is how my current folder structure looks like:

enter image description here

I am present in the FinTechExplained_Python_Docker folder. My Dockerfile looks like this:

FROM python:3.8-slim-buster

WORKDIR /src

COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "main.py"]

However, when I run this command
docker build --tag FinTechExplained_Python_Docker .

I get this error

ERROR [3/5] COPY requirements.txt requirements.txt                    0.0s
------
 > [3/5] COPY requirements.txt requirements.txt:
------
failed to compute cache key: "/requirements.txt" not found: not found

What am I doing wrong?

Edit:

I also tried changing it to:

COPY str/requirements.txt requirements.txt:

but then I would still get the error that:

failed to compute cache key: "/src/requirements.txt" not found: not found

maybe the second COPY statement is also to be changed but not sure how

3

Answers


  1. When building image from Dockerfile it searches file from directory where Dockerfile is located (FinTechExplained_Python_Docker in your case).

    So basically requirements located at FinTechExplained_Python_Docker/src/requirements.txt, but docker searches them at FinTechExplained_Python_Docker/requirements.txt.

    To fix this you have to change 5th line to:

    COPY src/requirements.txt requirements.txt
    
    Login or Signup to reply.
  2. You need to specify the source of your COPY statements relative to the build context, like this

    FROM python:3.8-slim-buster
    
    WORKDIR /src
    
    COPY src/requirements.txt requirements.txt
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY src/ .
    
    CMD [ "python", "main.py"]
    
    Login or Signup to reply.
  3. Oh, I think I got why you are having failed to compute cache key....

    When you are copying file in Dockerfile using COPY you have to pass Directory where file should be saved as second argument.
    Like COPY LOCAL_PATH_TO_FILE SERVER_PATH_TO_DIRECTORY

    In your case:

    FROM python:3.8-slim-buster
    
    WORKDIR /src
    
    COPY src/requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY src/ .
    
    CMD [ "python", "main.py"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search