'"COPY failed: " While Building a Python Docker Image

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


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mushroomator