10

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.

5 Answers 5

10

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.

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.

3
  • 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) Aug 26, 2019 at 18:00
  • 1
    @BruceBecker added the Linux to my answer
    – Gaius
    Aug 26, 2019 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, 2019 at 18:42
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

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.
1
  • 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, 2020 at 2:41
0

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

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

  2. Now, use Docker in Hyper-v mode.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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