Docker Community Forums

Share and learn in the Docker community.

Mounting the host vm

I want to mount the host vm in Docker for Windows in my container (for backup purpose). I found a short article about it which says to run

docker container run --rm -it -v /:/host alpine

to do this. I tried and it works fine. Now i wanted to put it into a docker-compose file. However

volumes:
  - / :/host:ro

doesn’t work. I don’t get an error message, the folder is just empty. The space in front of the colon was necessary, without it I got the error

invalid volume specification: '/host_mnt/host:ro'

I also tried using single and double quotes. But I don’t think that is the problem. I tried mounting /var instead / and got the same empty directory.

Does someone know how to set this up in docker-compose or is this a bug?

Just remove the space character between / and :host:ro in your volume declaration.

Like I said in the question, if I remove the space I get an error.

Here is an example docker-compose.yml I used to test the scenario:

version: '2.3'

services:
  rootvolume-mount-test:
    image: alpine
    command: ls /hostroot
    volumes:
      - /:/hostroot:ro

start with docker-compose up and check the output!

This is the output I get

docker-compose -f "test.docker-compose.yml" up -d --build
Creating network "docker_default" with the default driver
Creating docker_rootvolume-mount-test_1 ... error

ERROR: for docker_rootvolume-mount-test_1  Cannot create container for service rootvolume-mount-test: invalid volume specification: '/host_mnt/hostroot:ro'

ERROR: for rootvolume-mount-test  Cannot create container for service rootvolume-mount-test: invalid volume specification: '/host_mnt/hostroot:ro'
ERROR: Encountered errors while bringing up the project.

Ah, this is because you do run docker-compose on windows and not inside the docker vm it uses to actualy run the containers. The docker-compose cli client does more magic than it’s linux counterpart. If you use the Windows docker-compose client, only the Hosts filesystem can be shared.

I thought docker compose is just a (advanced) wrapper around docker run to simplify long and complex run commands? I would expect it to work with compose if it works with run. So I have to write a script instead or is there a way to make docker compose work?

Docker-Compose is not realy a wrapper, it is a single node orchestrator that interacts with the Docker Engines api. Though on Windows, it has to do additional magic and orchestrate Hyper-V to actualy mount the local filesystem into the vm, and then let the engine bind it to a container.

I always felt the Windows and MacOS version of Docker are halfed assed, as they delegate everything to a minimum Linux vm. Most things work like they would on a native Linux system, though when it commes to volumes and networks, its a whole different beast.

$ NAME="ENV" docker-compose up
Recreating lab_ubuntu_1 ... done
Attaching to lab_ubuntu_1
ubuntu_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ubuntu_1  | HOSTNAME=7ee3ce6e3965
ubuntu_1  | NAME=ENV
ubuntu_1  | HOME=/root
lab_ubuntu_1 exited with code 0

This will help you to override the value defined in the .env file by setting an environment variable in the host’s shell