unable to prepare context: path "...\\api" not found from
docker compose build means the folder named by a service's build:
key does not exist where Compose looked for it. The path in the message is the full path
Compose tried; create the folder there with its Dockerfile, or point build: at
the folder that does exist.
A relative build: path such as ./api was resolved against the
folder that holds the Compose file. Compare the printed path with the real location of
your Dockerfile and fix whichever side is wrong.
The error
PS> docker compose build
Image fix9-cmp-c8-api Building
#1 [internal] load local bake definitions
#1 reading from stdin 729B done
#1 DONE 0.0s
unable to prepare context: path "C:\\...\\c8\\api" not found
The path was shortened here; the rest is as printed. The command exited with code 1.
docker compose up -d --build printed the same lines and exited with code 1
too. The file, in a folder c8 that had no api subfolder:
name: fix9-cmp-c8
services:
api:
build: ./api
image: fix9-cmp-c8-api
Why it happens
The value of build: is the build context, the folder the image is built
from. In the output, ./api was turned into an absolute path under the Compose
file's folder, c8\api, and the build stopped because that folder was missing.
Nothing was built and no container was created.
The base of that path is the Compose file's folder, not the folder you run the command
from. Run from the parent folder with -f, the message named the same
c8\api path:
PS> docker compose -f c8\compose.yaml build
...
unable to prepare context: path "C:\\...\\c8\\api" not found
So the folder to check is the one beside the Compose file. The full path in the message shows exactly which folder was expected.
The fix
Here the fix was to create the folder the file names, with a Dockerfile in it:
FROM alpine
CMD ["sleep", "300"]
PS> docker compose build
Image fix9-cmp-c8-api Building
...
#6 naming to docker.io/library/fix9-cmp-c8-api:latest done
...
Image fix9-cmp-c8-api Built
PS> docker compose up -d
...
Container fix9-cmp-c8-api-1 Started
PS> docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
fix9-cmp-c8-api-1 fix9-cmp-c8-api "sleep 300" api 1 second ago Up Less than a second
The build found the context, tagged the image with the name from the
image: key, and the service started from it. If your Dockerfile already
exists somewhere else, change the build: line to that folder instead of
moving files; the path is relative to the folder that holds the Compose file.
How it was reproduced
One service with build: ./api and no api folder beside the
Compose file, run with docker compose build and
docker compose up -d --build from Windows PowerShell 5.1 on Docker Engine
29.9.3 and Docker Compose v5.7.0 (Docker Desktop on Windows 11); the fix added
api\Dockerfile on the cached alpine image. The project and image
names carry this test's fix9-cmp- prefix and were removed with
docker compose down -v --rmi local.
Frequently asked
- What does 'unable to prepare context: path not found' mean in docker compose build?
- The folder named by the service's build key does not exist. The message prints the full path that was tried; in this test ./api was resolved under the Compose file's folder.
- Is the build path in docker-compose.yml relative to the Compose file?
- In this test it was. build: ./api in a file inside the c8 folder was resolved to c8\api, both when the command ran in c8 and when it ran from the parent folder with -f.
- How do I fix a missing build context in Docker Compose?
- Create the folder the build key names, with its Dockerfile, or change the build key to the folder where the Dockerfile really is. Then run docker compose build again.
More decoded errors in the Fixes category. When the
service has neither build: nor image:, see
Docker
Compose: service has neither an image nor a build context.