How to Mount Current Directory as a Volume in Docker on Windows 10?

Docker provides a powerful solution for containerizing applications and simplifying the development and deployment process.

A frequent question that we get asked by our readers is “How to Mount Current Directory as a Volume in Docker on Windows 10?”

One of the key features of Docker is the ability to mount volumes, which allows seamless sharing of files and directories between the host machine and Docker containers.

Mount Current Directory as a Volume in Docker on Windows 10
Mount Current Directory as a Volume in Docker on Windows 10

In this guide, we will walk you through the steps to mount the current directory as a volume in Docker on Windows 10. By following these instructions, you’ll be able to conveniently access and modify files from your local machine within a Docker container. Let’s get started!

Prerequisites

Before proceeding, ensure that you have the following prerequisites:

  1. Docker Desktop installed and running on your Windows 10 machine. You can download Docker Desktop from the official Docker website (https://www.docker.com/products/docker-desktop).
  2. Mounting the Current Directory as a Volume
    To mount the current directory as a volume in Docker, follow these steps:

Step 1: Open a Command Prompt or PowerShell window

# Open Command Prompt or PowerShell

Step 2: Navigate to the directory you want to mount as a volume

# Change directory to the target directory
cd C:\my-project

Step 3: Run the Docker container with the volume mapping

# Mount the current directory as a volume within the container
docker run -v "$(pwd):/app" <image_name>
  • Replace <image_name> with the name of the Docker image you want to use.

Verifying the Volume Mounting

To verify that the volume is mounted correctly, follow these steps:

Step 1: Run the Docker container with the volume mapping

# Run the Docker container with volume mapping
docker run -v "$(pwd):/app" <image_name>
  • Replace <image_name> with the name of the Docker image you want to use.

Step 2: Open a new Command Prompt or PowerShell window

Step 3: Access the container’s shell

# Execute the following command to access the container's shell
docker exec -it <container_name> sh
  • Replace <container_name> with the name or ID of the running container.

Step 4: Navigate to the target directory within the container

# Change directory to the target directory within the container
cd /app
  • Now, you can interact with the files and directories within the container. Any changes made to files within the container’s /app directory will reflect in the host machine’s current directory.

Additional Considerations While Trying to Mount Working Directory as a Volume in Docker

When working with mounted volumes, consider the following points:

  • Ensure that the target directory within the container exists. If not, create it within the Docker image or using the container’s shell.
  • Exercise caution while modifying files within the container, as changes made within the container may affect the host machine’s files.
  • Be aware of file permissions. Files and directories mounted from the host may retain their original permissions, which could cause permission-related issues within the container. Adjust permissions as necessary.
  • If you encounter file synchronization or permission-related issues, consider using the appropriate Docker volume driver.

Wrapping Up

Mounting the current directory as a volume in Docker on Windows 10 allows for seamless file sharing between the host machine and Docker containers.

By following the steps outlined in this guide, you can conveniently work with your local files within a Docker container, simplifying your development and deployment workflows.

Experiment with different configurations and leverage the power of Docker to enhance your application development experience.

Additional Resources

You can learn more about the related topics by checking out the following tutorials:

Sources: