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.

Gaius
  • 1,096
  • 10
  • 18
Bruce Becker
  • 3,783
  • 4
  • 20
  • 41

5 Answers5

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.

dovidweisz
  • 103
  • 2
Wesley Rolnick
  • 2,772
  • 12
  • 26
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.

Gaius
  • 1,096
  • 10
  • 18
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

rozacek
  • 121
  • 2
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.
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.