Skip to content
Latchkey

Docker "failed to solve: lstat /var/lib/docker: permission denied" in CI

The daemon stores layers and overlay state under /var/lib/docker, a directory owned by root and unreadable to the build. When the build context root or a bind mount resolves into that directory, BuildKit cannot lstat it and fails with "permission denied".

What this error means

A docker build/buildx build fails while preparing the context with failed to solve: lstat /var/lib/docker/...: permission denied. It often happens when the build context is set to / or a parent of the daemon root.

docker
ERROR: failed to solve: lstat /var/lib/docker/overlay2: permission denied

Common causes

The build context includes the daemon root

Running docker build . from / or another directory that contains /var/lib/docker makes BuildKit try to walk root-owned overlay state.

A bind mount pointing into /var/lib/docker

A volume or --mount=type=bind,source= aimed at the daemon root exposes paths the build user cannot read.

A rootless build reaching a root-owned path

A rootless builder running as an unprivileged user cannot lstat root-owned daemon directories.

How to fix it

Scope the build context to your project

  1. Run the build from your source directory, not / or the daemon root.
  2. Pass an explicit context path so BuildKit never walks system directories.
Terminal
cd /home/runner/work/app/app
docker build -t myorg/app:ci .

Exclude the daemon root from the context

  1. If the context legitimately sits above the project, add the daemon path to .dockerignore.
  2. Better, restructure so the context never overlaps /var/lib/docker.
.dockerignore
# .dockerignore
var/lib/docker
**/docker/overlay2

How to prevent it

  • Always build from a tight, project-scoped context directory.
  • Never set the build context to / or a parent of the Docker daemon root.

Frequently asked questions

What causes ""lstat /var/lib/docker""?
Running docker build . from / or another directory that contains /var/lib/docker makes BuildKit try to walk root-owned overlay state.
How do I fix "lstat /var/lib/docker"?
Scope the build context to your project

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card