How to Mount a Local Folder into a Docker Container
Purpose
Bind-mount a local folder into a Docker container, enabling persistent storage and easy sharing of files between host and container.
1. Create the Local Folder
Create a folder on the host that you want to share:
mkdir -p ~/docker-data/myapp
2. Set Correct Permissions
Ensure the folder is accessible by Docker containers:
chmod 755 ~/docker-data/myapp
(Adjust permissions based on your security requirements.)
3. Run a Container with the Folder Mounted
Use the -v
flag in Docker:
docker run -d \
--name my-container \
-v ~/docker-data/myapp:/app/data \
my-docker-image
- Left side (
~/docker-data/myapp
): Host path. - Right side (
/app/data
): Path inside the container.
4. Verify the Mount
Inside the container, the folder should appear:
docker exec -it my-container ls /app/data
If you create files inside /app/data
, they will appear inside your host folder too.
✅ Summary
- Bind-mounts connect host folders to containers.
- Use absolute paths when mounting folders in production.
- Ensure correct permissions to avoid container access issues.
🛠️ Useful Commands
- Check container mounts:
docker inspect my-container
- Restart containers easily:
docker restart my-container
No Comments