Docker
Symlink
Containerization
File System
DevOps

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:

text
1project/
2  Dockerfile
3  app/
4  shared -> ../shared
5../shared/
6  config.json

This Dockerfile looks like it should work but does not:

dockerfile
FROM alpine:3.20
WORKDIR /app
COPY shared/config.json ./config.json

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:

text
1workspace/
2  Dockerfile
3  project/
4  shared/

The build then copies both explicitly:

dockerfile
1FROM alpine:3.20
2WORKDIR /app
3COPY project/ ./project/
4COPY shared/config.json ./project/config.json

Build from workspace:

bash
docker build -t myapp /path/to/workspace

The dependency is now visible and reproducible. Hidden filesystem reach-through is exactly what Docker tries to prevent.

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.

bash
1docker run --rm \
2  -v "$PWD/data:/data" \
3  alpine:3.20 \
4  ls -l /data

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:

  1. Can the container see the symlink entry?
  2. 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:

bash
ls -l project
readlink project/shared
docker build -t symlink-test project

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:

bash
docker run --rm -it -v "$PWD/project:/app" alpine:3.20 sh
ls -l /app
cat /app/shared/config.json

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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design