Mount current directory as a volume in Docker on Windows 10
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Docker is a powerful platform that enables developers to build, deploy, and manage applications in isolated containers. One of the essential features of Docker is the ability to mount volumes, allowing data to persist beyond the lifecycle of the container instances. This article explores how to mount the current directory as a volume in Docker on Windows 10, thus enabling seamless data sharing between the host and Docker containers.
Understanding Docker Volumes
In Docker, a volume is a persistent data storage mechanism that allows containers to read from and write data to a location on the host file system. Volumes are suitable for maintaining persistent data, and they play a crucial role in sharing data between the host and container or among multiple containers. There are several types of volumes in Docker, including named volumes and bind mounts. This article focuses on bind mounts, which are used to mount a specific directory from the host to the container.
Setting Up Docker on Windows 10
Before you start, ensure that Docker Desktop is correctly installed and running on your Windows 10 machine. Docker Desktop provides a user-friendly way to manage containers and is compatible with Windows Subsystem for Linux (WSL) 2, which offers improved system integration.
Steps to Install Docker on Windows 10:
- Download Docker Desktop: Visit Docker's official website and download the Docker Desktop installer for Windows.
- Run the Installer: Run the installer and follow the instructions to complete the installation.
- Enable WSL 2: If it's not already enabled, enable Windows Subsystem for Linux on your Windows 10 machine.
- Start Docker Desktop: Launch Docker Desktop and sign in with your Docker Hub account if prompted.
- Verify Installation: Open PowerShell or Command Prompt and run
docker --versionto verify the installation.
Mounting Current Directory as a Volume
To mount the current directory as a volume in Docker, you need to use the -v or --volume flag in the docker run command, specifying the host path and the container path.
Example:
Assume you have a directory, C:\Users\YourName\project, containing files you want to share with a Docker container. To mount this directory:
- Open PowerShell or Command Prompt in Windows 10.
- Navigate to the Directory:
- Run a Docker Container with Volume Mount:
Explanation:
${PWD}is a variable that holds the current working directory path./appis the directory within the Docker container where the files will be accessible.alpineis a lightweight Docker image used in this example.- The
lscommand lists the files in the/appdirectory within the container.
Key Considerations for Mounting Directory:
- Permissions: Ensure that proper permissions are set for the host directory to allow reading and writing by the Docker container.
- Path Format: Use Windows path format for host directories and Unix path format within the container.
- WSL Integration: Due to WSL 2 integration with Docker Desktop, you can seamlessly access Linux paths and perform operations using Linux commands.
Practical Usage Scenarios
- Development Environment: Easily synchronize code changes with the container environment, allowing for rapid development and testing.
- Data Analysis: Mount directories containing datasets into data processing containers to enable efficient analysis without copying data.
- Configuration Management: Share configuration files between the host and container to ensure consistency across deployments.
Summary Table: Mounting Current Directory
| Aspect | Description |
| Volume Type | Bind Mount |
| Command Example | docker run -v ${PWD}:/app <image> |
| Host Directory Format | Windows Path |
| Container Path Format | Unix Path |
| Use Case | Data Sharing, Development, Configuration Management |
| Environment | Docker Desktop on Windows 10 with WSL 2 Integration |
Conclusion
Mounting the current directory as a volume in Docker on Windows 10 simplifies the process of data sharing and collaborative development. By leveraging Docker volumes, developers can create more robust and efficient workflows, bridging the gap between the host environment and isolated containers. As you explore more advanced Docker features, combining these with volume mounts can lead to more versatile and scalable applications.

