Mount current directory as a volume in Docker on Windows 10
πππ» Welcome to the Docker Dazzle Blog! Today, we're going to tackle a common problem faced by Windows 10 users who want to mount their current directory as a volume in Docker. π³π₯
So you've built a Docker image that's running smoothly, but you're struggling to mount the current path. You'd love to use container executables as commands in your current path, right? We're here to help you make that happen! ππ§
Before we dive into the solution, let's set the stage. You're using Docker version 1.12.5 on Windows 10 via Hyper-V. You've got a drive, let's say it's E, with a folder named "test." Inside "test," there's another folder called "folder on windows host," just to showcase that the command is working. To make this work, your Dockerfile creates a directory called /data
and defines it as both a VOLUME and WORKDIR. So far, so good! π
Now, you want to use the current directory instead of an absolute notation. Makes sense, right? But when you try different approaches, like using pwd
in the volume, you encounter some frustrating error messages. Let's take a look at the failed attempts: π«β
Trying with
($pwd)
:
PS E:\test> docker run --rm -it -v ($pwd):/data mirkohaaser/docker-clitools ls -la
Error: ":/data" is not a valid repository/tag.
Trying with
/($pwd)
:
PS E:\test> docker run --rm -it -v /($pwd):/data mirkohaaser/docker-clitools ls -la
Error: E:\\test is not a valid repository/tag.
Trying with
\Β΄pwd\Β΄
:
PS E:\test> docker run --rm -it -v Β΄$pwdΒ΄:/data mirkohaaser/docker-clitools ls -la
Error: Invalid bind mount spec "Β΄E:\\testΒ΄:/data": invalid mode: /data.
Trying with
pwd
:
PS E:\test> docker run --rm -it -v `$pwd`:/data mirkohaaser/docker-clitools ls -la
Error: "$pwd" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
Oh boy! These error messages can be quite confusing, right? But fear not, the correct syntax is just a heartbeat away! πͺπ‘
To mount the current directory as a volume in Docker on Windows 10, you need to use $(pwd)
instead of ($pwd)
. So, here's the correct command to make your dreams come true: πβ¨
docker run --rm -it -v $(pwd):/data mirkohaaser/docker-clitools ls -la
By using $(pwd)
, you're instructing Docker to fetch the current directory, and then magic happens! You successfully mount the current directory as a volume in the Docker container. Go ahead, give it a try! πͺπββοΈ
We hope this guide has helped you solve the puzzling issue of mounting the current directory as a volume in Docker on Windows 10. If you found this helpful, make sure to share it with your fellow Docker enthusiasts and check out our blog for more Docker tricks and tips. π―π
Have other Docker problems or tech questions? We're here to help! Drop a comment below and let's start a conversation. Together, we can conquer the Dockerverse! ππ»