Docker
symbolic link
containerization
host directory
file system

Docker bind mount with symlinks inside the mounted directory

Codemia Knowledge Hub ·

A bind mount makes a host directory visible inside the container, symbolic links included. The links themselves come through as links, and the container resolves them against its own filesystem, not the host's. A link that points somewhere outside the mounted directory only works if that target path also exists in the container, for example because you bind-mount it as well.

Bind mounts versus other mount types

Docker supports several mount options:

  • Volumes: Managed by Docker and can be manipulated via Docker commands.
  • Bind mounts: Link a directory or file from the host directly into the container.
  • tmpfs mounts: Store data in host memory.

Bind mounts are the relevant type here, because they let you pick a specific host directory to expose inside the container.

A symbolic link (symlink) is a file that points to another file or directory. On Unix-like systems they are often used to keep a stable path to the current version of something without moving the data.

Consider this setup on the host:

bash
1# Directory containing symbolic links
2$ mkdir /data
3
4# Subdirectories and files
5$ mkdir /target
6$ touch /target/file1.txt
7$ touch /target/file2.txt
8
9# Creating symbolic link
10$ ln -s /target /data/symlink

Here /data/symlink is a symbolic link to /target.

Mounting the directory with docker run

To bind mount /data:

bash
docker run -it --rm \
  --mount type=bind,source=/data,target=/app/data \
  ubuntu:latest /bin/bash

This starts an Ubuntu container with the host's /data directory available at /app/data.

Inside the container, you can inspect the link:

bash
1# In the running container
2$ ls -l /app/data
3
4# Output
5lrwxrwxrwx 1 root root 7 Apr 11 19:44 symlink -> /target
6
7$ ls -l /app/data/symlink/
8
9# Output
10-rw-r--r-- 1 root root 0 Apr 11 19:40 file1.txt
11-rw-r--r-- 1 root root 0 Apr 11 19:40 file2.txt

The link is resolved inside the container, so /app/data/symlink points at /target in the container's filesystem. The listing above only shows the files if /target is also available in the container (for instance, with a second --mount type=bind,source=/target,target=/target). Without that, the absolute link is broken in the container even though it works on the host. Links that point to paths inside the mounted directory keep working, because those paths exist on both sides.

Permissions, platforms and security

  • Permission management: Symbolic links keep the permissions of the original files or directories. Make sure the user in the container has the permissions needed to read the targets.
  • Cross-platform compatibility: Check that links resolve correctly, especially on Windows, where symbolic link behavior can differ.
  • Security implications: Mounting host directories, especially ones containing symlinks, can expose more of the host filesystem to the container. Limit user permissions inside the container.
FeatureDescriptionExample Usage
Bind MountMounts a directory or file from host to container--mount type=bind,source=...,target=...
Symbolic LinkFile pointing to another file/directoryln -s /source /link
PermissionInherits from the pointed targetchmod on /target for /link permissions
Host AccessAccess to more of the host's file systemUseful for apps requiring host data
  • Broken links: A link breaks if its target is moved or deleted. Regular checks and logging help catch this.
  • Performance: Reading large amounts of data through symbolic links can add delay. Tools such as Docker stats help with performance tuning.
  • Circular links: Avoid them, since they can cause infinite loops when traversed.
  • Links to network storage: Symbolic links may point to automounted network storage, but DNS and network configuration then need to line up with the container's networking.

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