I stumbled with an error when I tried to build an image from my local machine and got this error: failed to solve: error from sender: context canceled

My Dockerfile looked like this:

ARG REGISTRY
FROM $REGISTRY/node:20.9-alpine as build

WORKDIR /app

COPY . /app

The error:

$ docker compose build beheer-web
[+] Building 5.7s (7/13)                                                                                                                                                                   docker:default
 => [beheer-web internal] load .dockerignore                                                                                                                                                         0.0s
 => => transferring context: 34B                                                                                                                                                                     0.0s
 => [beheer-web internal] load build definition from Dockerfile                                                                                                                                      0.0s
 => => transferring dockerfile: 557B                                                                                                                                                                 0.0s
 => [beheer-web internal] load metadata for nginx:1.25.3-alpine                                                                                                          4.3s
 => [beheer-web internal] load metadata for node:20.9-alpine                                                                                                             4.9s
 => CANCELED [beheer-web internal] load build context                                                                                                                                                0.8s
 => => transferring context: 79.47MB                                                                                                                                                                 0.7s
 => [beheer-web stage-1 1/3] FROM nginx:1.25.3-alpine@sha256:f2802c2a9d09c7aa3ace27445dfc5656ff24355da28e7b958074a0111e3fc076                                            0.0s
 => [beheer-web build 1/5] FROM node:20.9-alpine@sha256:cb2301e2c5fe3165ba2616591efe53b4b6223849ac0871c138f56d5f7ae8be4b                                                 0.0s
failed to solve: error from sender: context canceled

The problem was because I tried copying the current directory into the Docker image
COPY . /app

Since this is an IntelliJ project, it tried to also copy the .idea directory which is causing the problem since the files are being used.
The problem is that Docker is throwing this error because there are files in used that we try to copy.

While it does not make sense to copy the .idea folder the solution is to create a .dockerignore file and exclude files and directories which is similar as a .gitignore file.

I also added some angular and NPM directories since we build the application while building the Docker image.

.dockerignore

.idea
node_modules
.angular
dist