Docker
Windows 10
mount volume
current directory
Docker tutorial

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:

  1. Download Docker Desktop: Visit Docker's official website and download the Docker Desktop installer for Windows.
  2. Run the Installer: Run the installer and follow the instructions to complete the installation.
  3. Enable WSL 2: If it's not already enabled, enable Windows Subsystem for Linux on your Windows 10 machine.
  4. Start Docker Desktop: Launch Docker Desktop and sign in with your Docker Hub account if prompted.
  5. Verify Installation: Open PowerShell or Command Prompt and run docker --version to 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:

  1. Open PowerShell or Command Prompt in Windows 10.
  2. Navigate to the Directory:
bash
   cd C:\Users\YourName\project
  1. Run a Docker Container with Volume Mount:
bash
   docker run --rm -v ${PWD}:/app -w /app alpine ls

Explanation:

  • ${PWD} is a variable that holds the current working directory path.
  • /app is the directory within the Docker container where the files will be accessible.
  • alpine is a lightweight Docker image used in this example.
  • The ls command lists the files in the /app directory within the container.

Key Considerations for Mounting Directory:

  1. Permissions: Ensure that proper permissions are set for the host directory to allow reading and writing by the Docker container.
  2. Path Format: Use Windows path format for host directories and Unix path format within the container.
  3. 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

  1. Development Environment: Easily synchronize code changes with the container environment, allowing for rapid development and testing.
  2. Data Analysis: Mount directories containing datasets into data processing containers to enable efficient analysis without copying data.
  3. Configuration Management: Share configuration files between the host and container to ensure consistency across deployments.

Summary Table: Mounting Current Directory

AspectDescription
Volume TypeBind Mount
Command Exampledocker run -v $&#123;PWD&#125;:/app <image>
Host Directory FormatWindows Path
Container Path FormatUnix Path
Use CaseData Sharing, Development, Configuration Management
EnvironmentDocker 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.


Course illustration
Course illustration

All Rights Reserved.