<pre><code>PS C:\Users\gaius&gt; Write-Output $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  592     



PS C:\Users\gaius&gt; Write-Output $pwd

Path          
----          
C:\Users\gaius
</code></pre>

<p>That appears to work as expected, what versions of things are you using?</p>

<p>Compare to Linux:</p>

<pre><code>gaius@klossy:~$ pwsh
PowerShell 6.2.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /home/gaius&gt; echo $pwd

Path
----
/home/gaius

PS /home/gaius&gt; 
</code></pre>

<p>(<code>echo</code> is just an alias for <code>Write-Output</code>)</p>

<p>If you must have exact commonality between Windows and Linux there are a few solutions, Git comes with Bash for Windows, there's WSL, etc. </p>

CC BY-SA 4.0
2019-08-26T18:00:07.433Z
This answer makes me look like a bit of an idiot :)  -- and certainly show the gaps in my PowerShell knowledge. The developer claimed that `${PWD}` wasn't working for them Unfortunately, I have no way of verifying this, not having access to a Windows machine. But certainly it looks like it should work (I'm using Powershell 6.2.2 on Linux)
Bruce Becker
CC BY-SA 4.0
2019-08-26T18:36:00.683Z
@BruceBecker added the Linux to my answer
Gaius
CC BY-SA 4.0
2019-08-26T18:42:07.673Z
Actually now that I think about it some more, just recommend your Windows devs use Git Bash if they absolutely must do things the same way as Linux
Gaius
CC BY-SA 4.0
2019-08-26T17:32:15.427Z
2019-08-26T18:41:14.023Z
2019-08-26T18:41:14.023Z
Gaius
<p>I think my case may be related as I also tried to configure environment for both Windows and Linux. I only have to mention that I am using <code>docker-compose</code> on WSL1 (Windows Subsystem for Linux) configured following by: <a href="https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly" rel="nofollow noreferrer">this</a></p>

<p>I suggest you to try </p>

<p>My solution is (using the: <a href="https://docs.docker.com/compose/compose-file/#long-syntax-3" rel="nofollow noreferrer">long-syntax volumes definition</a> from official docs):</p>

<pre><code>version: '3.7'

services:
  web:
    build: ./DirectoryWithDockerfile
    volumes:
      - type: bind
        source: ./DirectoryIWantToMountInDockerContainer
        target: /path/where/mount/source
        volume:
          nocopy: true  # flag to disable copying of data from a container when a volume is created
    ports:
      - 8000:8000
</code></pre>

<p>I don't know why short syntax doesn't worked on Windows for me (and for you):</p>

<pre><code>   volumes:  # this doesn't work and I don't know why
     - ./DirectoryIWantToMountInDockerContainer/:/path/where/mount/source
</code></pre>

<p>My Docker version: 19.03.5</p>

<p>docker-compose version: 1.25.4</p>

CC BY-SA 4.0
2020-02-17T13:50:05.847Z
2020-02-17T14:11:19.337Z
2020-02-17T14:11:19.337Z
rozacek
<p>2022 update
If you want to have a persistent database for docker(Windows) MySQL using docker Hyper-V</p>
<ol>
<li><p>Make a folder at any location where you want to have your DB data
for eg. D:\DockerDB</p>
</li>
<li><p>Now, use Docker in Hyper-v mode.</p>
</li>
<li><p>Go to settings -&gt; Resources -&gt; File sharing -&gt; add you DB data folder here.(D:\DockerDB) and apply and restart.
<a href="https://i.sstatic.net/Emycq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Emycq.png" alt="enter image description here" /></a></p>
</li>
</ol>
<p>In your docker-compose.yml file add volumes as below</p>
<pre><code>mysqldb:
    container_name: mysqldb
    image: mysql
    volumes:
      - D:/DockerDB:/var/lib/mysql
</code></pre>
<p>Now your data will persist on DB on your local machine.</p>

CC BY-SA 4.0
2022-11-17T13:44:37.160Z
2022-11-17T13:44:37.160Z
Tarun Bisht
<pre><code>volumes:
  - /usr/local/db-backup:/var/lib/mysql # On Linux, For backup data from the docker container to the host
  - D:/Docker_volumes/mysql/db-backup:/var/lib/mysql # On Windows, you need to config File Sharing with "D:" mounted when starting docker.
</code></pre>

CC BY-SA 4.0
2020-04-13T02:41:30.293Z
Welcome to DevOps.  Thanks for your contribution.  You've shown how to do a volume in each Linux and Windows, but not how the OP could have one `docker-compose.yml` which works for both.
chicks
CC BY-SA 4.0
2020-04-10T05:09:53.563Z
2020-04-10T05:09:53.563Z
Pham Duc Toan
<p>Yes.  Just use ./ for you current directory that the Docker-compose file is in. Your &quot;working directory&quot; for the compose file is just &quot;./&quot;.  If you are trying to set a directory below that it would look something like: <pre><code>volumes:</p>
<ul>
<li>./DirectoryIWantToTarget:/tmp</code></pre></li>
</ul>
<p>There's an example of this in the Docker-Compose documentation <a href="https://docs.docker.com/compose/compose-file/#short-syntax-3" rel="noreferrer">here</a>.  This approach makes the solution cross-platform as well.</p>

CC BY-SA 4.0
2019-08-26T16:43:21.097Z
2021-09-10T15:49:54.357Z
2021-09-10T15:49:54.357Z
Wesley Rolnick
<p>We have developers working on an app using both Windows and Linux. The application is built within a Docker container, and ships a docker-compose specification for the build environment.</p>

<p>The local directory is mounted as a volume: </p>

<pre><code>    volumes:
      - ${PWD}:/tmp
</code></pre>

<p>however <strong>this doesn't work in Windows</strong> because <code>$PWD</code> is not defined. </p>

<p>My question is:</p>

<p><strong>Can we have a single <code>docker-compose.yml</code> to satisfy both the Windows and Linux developers?</strong></p>

<p>The obvious way to do this seems to me to have two docker-compose files, one for each OS.</p>

<docker-compose><linux><windows><environment-variables><volume>
Docker compose volume syntax valid for Windows and Linux
Bruce Becker
# Docker compose volume syntax valid for Windows and Linux

(Asked by: username_0 on 2019-08-26)

We have developers working on an app using both Windows and Linux. The application is built within a Docker container, and ships a docker-compose specification for the build environment.
The local directory is mounted as a volume: 
    volumes:
      - ${PWD}:/tmp

however this doesn't work in Windows because $PWD is not defined. 
My question is:
Can we have a single docker-compose.yml to satisfy both the Windows and Linux developers?
The obvious way to do this seems to me to have two docker-compose files, one for each OS.

**Tags:** docker-compose, linux, windows, environment-variables, volume


---

## 5 answers

### Answer by username_6 on 2019-08-26. Score: 10 | Accepted answer

Yes.  Just use ./ for you current directory that the Docker-compose file is in. Your "working directory" for the compose file is just "./".  If you are trying to set a directory below that it would look something like: volumes:

./DirectoryIWantToTarget:/tmp

There's an example of this in the Docker-Compose documentation here.  This approach makes the solution cross-platform as well.


---

### Answer by username_1 on 2019-08-26. Score: 3

PS C:\Users\gaius> Write-Output $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  592     



PS C:\Users\gaius> Write-Output $pwd

Path          
----          
C:\Users\gaius

That appears to work as expected, what versions of things are you using?
Compare to Linux:
gaius@klossy:~$ pwsh
PowerShell 6.2.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /home/gaius> echo $pwd

Path
----
/home/gaius

PS /home/gaius> 

(echo is just an alias for Write-Output)
If you must have exact commonality between Windows and Linux there are a few solutions, Git comes with Bash for Windows, there's WSL, etc.

> **Comments:**
> **username_0**: This answer makes me look like a bit of an idiot :)  -- and certainly show the gaps in my PowerShell knowledge. The developer claimed that `${PWD}` wasn't working for them Unfortunately, I have no way of verifying this, not having access to a Windows machine. But certainly it looks like it should work (I'm using Powershell 6.2.2 on Linux)  
> **username_1**: @BruceBecker added the Linux to my answer  
> **username_1**: Actually now that I think about it some more, just recommend your Windows devs use Git Bash if they absolutely must do things the same way as Linux  

---

### Answer by username_2 on 2020-02-17. Score: 2

I think my case may be related as I also tried to configure environment for both Windows and Linux. I only have to mention that I am using docker-compose on WSL1 (Windows Subsystem for Linux) configured following by: this
I suggest you to try 
My solution is (using the: long-syntax volumes definition from official docs):
version: '3.7'

services:
  web:
    build: ./DirectoryWithDockerfile
    volumes:
      - type: bind
        source: ./DirectoryIWantToMountInDockerContainer
        target: /path/where/mount/source
        volume:
          nocopy: true  # flag to disable copying of data from a container when a volume is created
    ports:
      - 8000:8000

I don't know why short syntax doesn't worked on Windows for me (and for you):
   volumes:  # this doesn't work and I don't know why
     - ./DirectoryIWantToMountInDockerContainer/:/path/where/mount/source

My Docker version: 19.03.5
docker-compose version: 1.25.4


---

### Answer by username_3 on 2022-11-17. Score: 0

2022 update
If you want to have a persistent database for docker(Windows) MySQL using docker Hyper-V

Make a folder at any location where you want to have your DB data
for eg. D:\DockerDB

Now, use Docker in Hyper-v mode.

Go to settings -> Resources -> File sharing -> add you DB data folder here.(D:\DockerDB) and apply and restart.



In your docker-compose.yml file add volumes as below
mysqldb:
    container_name: mysqldb
    image: mysql
    volumes:
      - D:/DockerDB:/var/lib/mysql

Now your data will persist on DB on your local machine.


---

### Answer by username_4 on 2020-04-10. Score: 0

volumes:
  - /usr/local/db-backup:/var/lib/mysql # On Linux, For backup data from the docker container to the host
  - D:/Docker_volumes/mysql/db-backup:/var/lib/mysql # On Windows, you need to config File Sharing with "D:" mounted when starting docker.

> **Comments:**
> **username_5**: Welcome to DevOps.  Thanks for your contribution.  You've shown how to do a volume in each Linux and Windows, but not how the OP could have one `docker-compose.yml` which works for both.  

---

