Volume binding using docker compose on Windows
Asked Answered
P

10

69

I recently upgraded my Docker Toolbox on Windows 10, and now my volume mounts no longer work. I've tried everything. Here is the current mount path:

volumes:
  - C:\Users\Joey\Desktop\backend:/var/www/html

I receive an invalid bind mount error.

Piscatelli answered 26/12, 2016 at 16:49 Comment(0)
N
57

Use:

volumes:
  - "C:/Users/Joey/Desktop/backend:/var/www/html"

Putting the whole thing in double quotes and using forward slashes worked for me. I was on windows 10 in windows 10 using Linux containers through WSL2

This answer was from Spenhouet given here.

Neal answered 14/5, 2021 at 6:52 Comment(2)
this works if there are no spaces in the windows file path.Hickey
Confirmed. Even for a relative like "./backend:/app" the double quotes were necessary.Davisson
H
33
  1. Share nfs path using docker settings

enter image description here 2. execute following command

docker run --rm -v c:/Users:/data alpine ls /data
  1. Set path in docker compose file as shown below enter image description here

  2. File copied to windows

enter image description here

Halloween answered 27/2, 2020 at 13:7 Comment(1)
Please note that step one is only needed in Hyper-V mode as noted in this answer: stackoverflow.com/a/63346254Brader
P
12

I think you have to set COMPOSE_CONVERT_WINDOWS_PATHS=1, see here.

Docker Machine should do it automatically: https://github.com/docker/machine/pull/3830

Pinnace answered 27/12, 2016 at 4:53 Comment(0)
J
5

This solution worked for me, in docker-compose.yml :

    volumes:
      - c/Users/Cyril/django:/mydjango

(Windows 10 with WSL2 and Docker Desktop)

Jens answered 26/7, 2021 at 19:49 Comment(0)
D
5

On windows 10, solved the problem with adding the last one / at the end of host and mount path, like that:

volumes:
  - '/c/work/vcs/app/docker/i18n/:/usr/app/target/i18n/'

Without adding the last one / mounted path contained some docker system folders and symlinks.

Ducan answered 10/10, 2022 at 12:39 Comment(0)
R
4

It seems you are using an absolute path located inside C:\Users dir, that didn't work for me either, and if you are using Docker-Toolbox see below.

Overview

Forwarding the ./ relative path in volumes section will automatically get resolved by docker-compose to the directory containing docker-compose.yml file (for example, if your project is in %UserProfile%/my-project then ./:/var/www/html gets /c/Users/my-name/my-project:/var/www/html).

The problem is that currently (using DockerToolbox-19.03.1) only the /c/Users directory gets shared with the Virtual-Machine (toolbox puts docker itself in the VM, which means it has no access to your file system, except mounted shared-directories).

Conclusion

So, basically placing your project there (C:\Users\YOUR_USER_NAME) should make ./ work. But not even that worked for me, and we ended up with below _prepare.sh script:

#!/bin/bash

VBoxManage='/c/Program Files/Oracle/VirtualBox/VBoxManage'

# Defines variables for later use.
ROOT=$(dirname $0)
ROOT=$(cd "$ROOT"; pwd)
MACHINE=default
PROJECT_KEY=shared-${ROOT##*/}

# Prepares machine (without calling "docker-machine stop" command).
#
if [ $(docker-machine status $MACHINE 2> /dev/null) = 'Running' ]; then
    echo Unmounting volume: $ROOT
    eval $(docker-machine env $MACHINE)
    docker-compose down
    docker-machine ssh $MACHINE <<< '
        sudo umount "'$ROOT'";
    '
    "$VBoxManage" sharedfolder remove $MACHINE --name "$PROJECT_KEY" -transient > /dev/null 2>&1
else
    docker-machine start $MACHINE
    eval $(docker-machine env $MACHINE)
fi

set -euxo pipefail
"$VBoxManage" sharedfolder add $MACHINE --name "$PROJECT_KEY" --hostpath "$ROOT" -automount -transient


docker-machine ssh $MACHINE <<< '
    echo Mounting volume: '$ROOT';
    sudo mkdir -p "'$ROOT'";
    sudo mount -t vboxsf -o uid=1000,gid=50 "'$PROJECT_KEY'" "'$ROOT'";
'

docker-compose up -d
docker-machine ssh $MACHINE
bash

Usage:

  • Place a copy of it beside each project's docker-compose.yml file.
  • Run it each time the system is turned on (simply double-click it or its shortcut).
  • Done! relative paths should now work even if your project is in another drive (far away and outside of C:\Users dir).

Note:

  • With a little edit, it should work without docker-compose being required.
  • Consider running docker system prune to free disk-space (or simply add docker system prune --force to the above script, on a new line right after mount command).
Role answered 5/2, 2020 at 7:39 Comment(0)
P
3

I faced with same issue (I'm using Docker Desktop).

My steps were:

1) Place your folder under drive "C"

2) Open "Settings" in Docker Desktop -> "Shared Drives" -> "Reset Credentials" -> select drive "C" -> "Apply"

3) Open terminal and run (as proposed by Docker Desktop):
docker run --rm -v c:/Users:/data alpine ls /data

4) Open your docker-compose.yml and update path in -volumes:

volumes:
  - /data/YOUR_USERNAME/projects/my_project/jssecacerts:/usr/lib/jvm/java-1.8-openjdk/jre/lib/security/jssecacerts/

5) restart docker container

Peyote answered 19/4, 2019 at 15:29 Comment(0)
R
3

this work on my computer:

  mongoservice:
    image : mongo
    container_name: mongodb
    restart: always
    volumes: 
      - //d/tests/leaflet_data/mongo_data/:/data/db
    ports:
     - "27018:27017"
    expose:
     - "27017"

it will put mongo database to d:\tests\leaflet_data\mongo_data

But the best solution for me to do it like this:

volumes: 
      - ./mongo_data/:/data/db

This will put mongo db into the same folder where your docker-compose yml file live. It will create mongo_data in this working dir. Very convenient, just put everything you need in project directory.

Ran answered 2/3, 2023 at 12:13 Comment(0)
C
1

If you're using the new Docker WSL2 backend, some drives may not be mounted in any WSL (and so Docker won't be able to see them either). For example, D: or E: or usb drives. See

To rule out this problem, try running docker-compose from a wsl command line.

Coltish answered 22/4, 2021 at 1:30 Comment(0)
U
1

I solved it by replacing : and '' in the windows path with / at the first of the line.

to be like that:

 volumes:
      -/c/Users/Joey/Desktop/backend:/var/www/html

Please note: c should be small.

Unilobed answered 29/3, 2022 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.