如何在Docker for Mac镜像中访问共享文件夹?
Hey there! Let me break down exactly how to get to those shared folders you set up in Docker for Mac's "File Sharing" preferences once you're logged into your container via SSH.
Step 1: Confirm Your Shared Folders Are Enabled
First, double-check that you've actually selected the folders you want to share in Docker for Mac:
- Open Docker for Mac, go to Preferences > File Sharing
- Make sure the checkbox next to your target folder (like
/Users/your-usernameor a custom directory) is checked - Click "Apply & Restart" if you made changes
Step 2: Mount the Shared Folder When Starting the Container
The key thing here is that Docker won't automatically make your shared folders available inside the container—you need to explicitly mount them when you launch the container using the -v (volume) flag.
For example, if you want to share your local /Users/your-username/dev folder and access it at /app inside the container, run your container like this:
docker run -d -p 22:22 --name my-dev-container -v /Users/your-username/dev:/app your-image-name
-p 22:22maps the container's SSH port to your host (so you can SSH into it)-v /host/path:/container/pathcreates the mount between your shared host folder and the container directory
Step 3: Access the Folder After SSHing In
Once you SSH into the container (e.g., ssh user@localhost -p 22), you can navigate directly to the mounted path you specified:
cd /app ls
You'll see all the files from your host's shared folder right there!
Troubleshooting Tip: Permissions
If you run into permission errors, it's usually because the user inside the container doesn't have access to the mounted directory. A quick fix is to adjust the permissions on the host side, or specify the user when running the container with the --user flag (e.g., --user $(id -u):$(id -g) to match your host user's UID/GID).
内容的提问来源于stack exchange,提问作者geoidesic

