Shooting Stars
Shooting Stars

Reputation: 835

failed to compute cache key: "/requirements.txt" not found: not found when build script is in child directory

I am trying to build my Dockerfile from a child folder context.

this is my build file in build.sh

#!/bin/bash -ex
docker build -t app:latest -f ../Dockerfile .

This is my Dockerfile

FROM app:latest
WORKDIR /app
# This path must exist as it is used as a mount point for testing
# Ensure your app is loading files from this location

FROM ubuntu:20.04

RUN apt update
RUN apt install -y python3-pip

# Install Dependencies
COPY requirements.txt .
RUN pip3 install  --no-cache-dir -r requirements.txt

This is my requirements.txt

Flask
pandas
requests
gunicorn
pytest

When I attempt to run build.sh within the scripts folder I get this error

#8 ERROR: "/requirements.txt" not found: not found
------
 > [stage-1 4/7] COPY requirements.txt .:
------
failed to compute cache key: "/requirements.txt" not found: not found

When I just do the docker build command in the command line in the app directory I will do this:

docker build -t app:latest -f Dockerfile .

This will work, however going into the child directory and attempting to build it using the bash script will fail with the requirements.txt caching issue.

How do I successfully build my docker container from the child folder?

My File Structure

Upvotes: 0

Views: 2486

Answers (1)

David Maze
David Maze

Reputation: 159040

The docker build command takes a path to a context directory

docker build [... options ...] .
#                              ^ this path

When the Dockerfile COPY requirements.txt . it is always relative to the path at the end of the docker build command. It doesn't matter where the Dockerfile itself is physically located.

If you want to build an image from a parent directory, where the Dockerfile is located in that parent directory, you need to specify the path. If the Dockerfile is named Dockerfile and is in the root of the context directory (the standard recommended location) then you do not need a docker build -f option.

cd $HOME/testing-docker
docker build -t app .

cd $HOME/testing-docker/scripts
docker build -t app ..
#                   ^^ build the parent directory
#                      but no -f option
#                      the Dockerfile is in the default place

# Any way to specify the path will work
cd
docker build -t app $HOME/testing-docker

Upvotes: 1

Related Questions