4

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.

3

Yes. Just use ./ for you current directory that the Docker-compose file is in. Docker-Compose does not allow you to use a context that is located above the compose file, so everything the compose file will use is below the folder with the .yml. 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.

| improve this answer | |
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.

| improve this answer | |
  • 1
    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 Aug 26 '19 at 18:00
  • 1
    @BruceBecker added the Linux to my answer – Gaius Aug 26 '19 at 18:36
  • 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 Aug 26 '19 at 18:42
0

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

| improve this answer | |
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.
| improve this answer | |
  • 1
    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 Apr 13 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.