-2

I'm trying to create a Docker image using the following Dockerfile.

# syntax=docker/dockerfile:1
FROM python:latest
WORKDIR /project4
COPY pythonCode1.py /project4/pythonCode1.py
COPY requirements.txt /project4/requirements.txt
RUN pip3 install -r requirements.txt
CMD ["python3 ", "pythonCode1.py"]

I get the Error:

COPY failed: file not found in build context or excluded by .dockerignore: stat pythonCode1.py: file does not exist

I have not set up a .dockerignore file and I'm building in a directory that contains:

project4 
       |-- Dockerfile
       |-- pythonCode1.py
       |-- requirements.txt

I read some other posts that refered to the docker context, mine just had the default:

`default *   Current DOCKER_HOST based configuration   unix:///var/run/docker.sock swarm
Watson221
  • 73
  • 7
  • where do you run the `docker build` command from and what does it look like? You'll have to run it from within the `project4` folder. – Mushroomator May 01 '22 at 22:30
  • I run it from the directory project4. I use the command ```docker build - < Dockerfile``` – Watson221 May 01 '22 at 22:32

1 Answers1

2

The problem is that by using

docker build - < Dockerfile

the Build Context does not include the whole dicrectory therefore the file pythonCode1.py is unknown to the Docker Engine.

Use the following docker build command instead.

# the . marks this directory as the directory with the Dockerfile and sets the build context 
docker build -t myrepo/myimage .

More information on build context and what it is and why it is required in this blog post.

Mushroomator
  • 6,516
  • 1
  • 10
  • 27