Docker COPY and symlinks outside the build context
Codemia Knowledge Hub ·
Docker will not follow a symlink to files outside the build context. COPY and ADD can only read files inside the context, and a symlink pointing elsewhere is blocked for security reasons. Runtime bind mounts behave differently: the container sees the link, but the target only resolves if that path is also mounted. The fix is to change the project layout or the build context root.
The build context is the boundary
docker build . sends a build context to the builder, and only files inside it are available to COPY and ADD. A symlink inside the context does not grant access to files outside it.
Take this host layout:
This Dockerfile looks like it should work but does not:
project/shared exists as a symlink, but its target lives outside the build context, and Docker does not treat that as permission to copy arbitrary host files.
The fix is structural: move the needed files under the build context, choose a larger context root, or use an additional named context if your build setup supports it.
Build from a parent directory
One common solution is to move the Dockerfile up a level so both directories are inside the context:
The build then copies both explicitly:
Build from workspace:
The dependency is now visible and reproducible. Hidden filesystem reach-through is exactly what Docker tries to prevent.
Symlinks in runtime bind mounts
At runtime, a bind mount exposes a real host path to the container, and symlink behavior depends on the mounted directory and the host filesystem.
If /data/link.txt is a symlink, the container sees it. Whether opening it works depends on where the target points and whether that target is reachable inside the container. So there are two questions:
- Can the container see the symlink entry?
- Can the target path be resolved inside the container?
A symlink to /Users/name/secrets.txt on the host usually fails in the container unless that host path is also mounted.
Is it a build or a runtime problem?
Reduce it to a tiny repro:
If the build fails on COPY, it is almost certainly the build context boundary. If the image builds but the running container cannot open the file, check the resolved path from inside the container:
Mistakes to avoid
- Expecting Docker to follow symlinks like a local shell. It does not, because image builds must be reproducible and must not read arbitrary host files.
- Building from the filesystem root. It technically works, but sends an enormous context, slows builds and makes it more likely that secrets end up in the image.
- Forgetting
.dockerignore. Even with a larger safe context, exclude.git,node_modules, logs and credentials to keep the build smaller and safer.
Related reading
- Docker for Desktop runs the Kubernetes - Ip address is not working
- Docker for Mac - Kubernetes - reference local image
- Docker for Windows error Hardware assisted virtualization and data execution protection must be enabled in the BIOS
- Docker for Windows stuck at Kubernetes is Starting after updating to version 2.1.1.0 Edge or Stable
- Docker forever in Docker is starting.. at Windows task
- Docker how to build an image from a non-master branch on Github repository
- Docker gets error failed to compute cache key not found - runs fine in Visual Studio
- Docker Got permission denied while trying to connect to the Docker daemon socket at unix///var/run/docker.sock
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.