7

I am new to docker. I currently have docker-compose.yml where volume I want to mount my current directory to '/usr/share/data' in the container. So something like:

volumes:
    - ${PWD}/data:/usr/share/data/

I am using windows with a Linux Subsystem and want to know why isn't it able to get the PWD variable. The same code on a Linux machine works fine. Please tell me how to set the environment PWD. Do I need to do it manually or can i do it using the same docker-compose file. Using . instead of ${PWD} gives no directory error and I have seen many forums saying that it is a windows problem.

A.Hamza
  • 219
  • 1
  • 4
  • 13

2 Answers2

3

You need to use ${PWD} instead of {$PWD}. You can also use dot . instead of ${PWD}.

anemyte
  • 17,618
  • 1
  • 24
  • 45
  • I don't have a Windows machine to test, but I believe that Windows with Linux Subsystem has some issues when relative paths are used in the compose file (`.`). – Neo Anderson Sep 17 '20 at 18:38
  • That was a typing error I am using ${PWD}. It gives the error described above. How to set it. Do I need to set it manually in windows environment variables??? And if so, how to i do it, like what should be the value of PWD if project is in D:/f1/f2 – A.Hamza Sep 17 '20 at 18:40
  • @NeoAnderson that is what I read and it gives error for me too. – A.Hamza Sep 17 '20 at 18:41
  • This needs to be run in powershell. If you are running the command in command prompt then use %cd%. This is how it works in docker cli (without using docker compose). Refer: https://stackoverflow.com/questions/41485217/mount-current-directory-as-a-volume-in-docker-on-windows-10 – Devesh Mar 03 '22 at 12:32
2

In order to share Windows folders with Docker containers when running, you first need to configure the Shared Drives option in Docker settings:

Right click on docker app > Settings > Shared drives > Check D:

Then . and ${PWD} should work in compose:

volumes:
    - ./data:/usr/share/data/
Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
  • @A.Hamza, you don't have to set the PWD environment variable. It is always equal to the absolute path of your current context. – Neo Anderson Sep 17 '20 at 19:03
  • I have got no shared drives option in the settings. My windows 10 version is 2004 OS Build 19041.508 – A.Hamza Sep 17 '20 at 19:31
  • I have tried switching to Linux container by right clicking docker icon. Still there is no Shared drive option in settings – A.Hamza Sep 17 '20 at 19:36
  • I just found out in WSL 2 there is no such option for shared drives. You can mount directly using `docker run -v c:/.../your-folder:/mount ...` but isn't it the same setting that we are using docker-compose volumes for? Here is the link [link](https://stackoverflow.com/questions/51816407/how-to-set-the-shared-drives-in-docker-for-windows) – A.Hamza Sep 17 '20 at 19:41