Docker Volumes and “print working directory” Command Syntax


I often use Docker to run an application in a container as I’m writing and testing code. That involves creating a volume that points the container to a path on my machine. The challenge with setting up volumes is that the “print working directory” command that is often used to easily identify the location of your source code on the host machine is different depending on what command terminal you’re using (especially on Windows).

Here’s a quick summary that shows the syntax for “print working directory” in different command terminals when using volumes (if you’re new to volumes you can read more about them here). An nginx container path is shown to provide a simple example of a volume pointing to the current working directory on the host machine.

Windows Command Window Syntax

You can use the %cd% syntax to represent the current working directory:

 -v %cd%:/usr/share/nginx/html

PowerShell Command Window Syntax

You can use the ${PWD} syntax to represent the current working directory:

 -v ${PWD}:/usr/share/nginx/html

Windows Subsystem for Linux (WSL) with a Windows Directory Syntax

If you’re referencing a Windows directory from WSL2 you can use the following syntax.
NOTE: It’s recommended you reference a directory that is within WSL rather than within Windows for performance reasons.

-v /mnt/c/username/some-windows-directory:/usr/share/nginx/html

Git Bash on Windows Syntax

You can use the /$(pwd) syntax to represent the current working directory:

-v /$(pwd):/usr/share/nginx/html

Mac/Linux Syntax

You can use $(pwd) syntax to represent the current working directory:

-v $(pwd):/usr/share/nginx/html

There are additional variations of the “print working directory” syntax shown above. If you prefer to use a different one please leave a comment with the information – share the knowledge!

Discuss on Twitter



Join the free Code with Dan Development Newsletter!