Docker无法识别共享路径:/etc/localtime未共享问题求助
Great question—let's break down why this is happening and walk through the fixes:
Why Sharing /private Isn't Enough
First, let's clarify the OS X directory structure here:
/etcon OS X is actually a symbolic link to/private/etc—that part you got right.- But your
/etc/localtimeis another symbolic link, pointing to/var/db/timezone/zoneinfo/America/New_York.
The problem? /var is a top-level directory separate from /private, so even though you shared /private, Docker can't access the target of the /etc/localtime link because /var isn't in your shared directories. Docker doesn't automatically resolve cross-directory symlinks outside of shared paths.
Solutions to Try
1. Mount the Actual Timezone File Directly
Skip the middle symlink entirely and mount the real timezone file from your Mac into the container:
docker run -v /var/db/timezone/zoneinfo/America/New_York:/etc/localtime:ro [your-image]
The :ro flag makes it read-only, which is the standard way to mount timezone files.
2. Add /var to Docker's Shared Directories
If you want to keep using the /etc/localtime symlink, add /var to Docker's file sharing list:
- Open Docker Desktop > Preferences > Resources > File Sharing
- Click the
+button, navigate to/var, and add it - Click "Apply & Restart" to save changes
Now Docker can access the /var/db/timezone/... path that /etc/localtime points to, and the symlink will resolve correctly.
3. Use the TZ Environment Variable (Simplest Option)
Most modern Linux-based containers support the TZ environment variable, which lets you set the timezone without mounting any files at all:
docker run -e TZ="America/New_York" [your-image]
This is more reliable than file mounting, as it avoids symlink and file-sharing quirks entirely.
内容的提问来源于stack exchange,提问作者user1079065

