How to Mount a Host Directory in a Docker Container

TLDR
Use the -v or --volume flag with docker run to mount a host directory into your container. This is useful for sharing code, data, or configuration files between your system and the container.
Prerequisites
- Docker installed
- A directory on your host to mount
Basic Syntax for Mounting a Directory
The -v flag lets you specify a host directory and a container directory, separated by a colon. For example:
# Mount ./data from your host to /app/data in the container
docker run --rm -v $(pwd)/data:/app/data busybox ls /app/data
This command runs ls inside the container, showing the contents of your local ./data directory as seen from /app/data in the container.
Read-Only Mounts
If you want the container to have read-only access, add :ro to the end:
# Mount as read-only
docker run --rm -v $(pwd)/config:/etc/config:ro busybox ls /etc/config
This is helpful for sharing configuration files without letting the container modify them.
Permissions and SELinux Notes
On Linux, make sure your user has permission to access the host directory. If you run into permission issues, try adjusting ownership or using sudo.
Next Steps
Try mounting different directories for development, or use named volumes for persistent data. Explore Docker Compose for more complex setups.
Good luck with your project!
Related Resources
- How to Add a Volume to an Existing Container: add volumes after creation
- Docker Persistent Storage for Databases: database volume patterns
- Docker Data Loss When Container Exits: prevent data loss
- Introduction to Docker: Volumes: volume fundamentals
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
Docker Terminal Simulator
Practice Docker commands in an interactive browser terminal. Learn images, containers, logs, exec, Dockerfile builds, volumes, networks, and Docker Compose with a live Docker daemon visualization.
How Docker Works Under the Hood
Watch what really happens when you run docker run -p 8080:80 nginx, one layer at a time. Step down the whole stack: the CLI, the daemon, the registry pull, containerd, the OCI runtime bundle, runc, the running container, and the shared Linux kernel. Every stage shows the real low-level command you can run yourself, so it doubles as a tour of the primitives that make a container: namespaces, cgroups, and runc. A container is not a small VM, and this shows you why.
We earn commissions when you shop through the links below.
Svix
Webhooks as a service
Svix Dispatch sends your webhooks for you: retries with exponential backoff, signed payloads, idempotency keys, and a delivery log your customers can see.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
How to Prevent Data Loss When Docker Containers Exit
Learn why data disappears when Docker containers stop, how to persist data with volumes and bind mounts, and best practices for managing stateful applications in containers.
Docker Multi-Stage Build Optimization
Learn to create efficient Docker images using multi-stage builds to reduce image size and improve security.
60 minutes
CI/CD Pipeline Setup Checklist
Step-by-step checklist for a production-ready CI/CD pipeline: source control, builds, tests, security scans, deploy gates, secrets, and rollback paths.
1-2 hours