43

I've got a docker-compose.yml like this:

db:
  image: mongo:latest
  ports:
    - "27017:27017"
server:
  image: artificial/docker-sails:stable-pm2
  command: sails lift
  volumes:
    - server/:/server
  ports:
    - "1337:1337"
  links:
    - db

server/ is relative to the folder of the docker-compose.yml file. However when I docker exec -it CONTAINERID /bin/bash and check /server it is empty.

What am I doing wrong?

Hedge
  • 16,142
  • 42
  • 141
  • 246

9 Answers9

33

Aside from the answers here, it might have to do with drive sharing in Docker Setting. On Windows, I discovered that drive sharing needs to be enabled.

In case it is already enabled and you recently changed your PC's password, you need to disable drive sharing (and click "Apply") and re-enable it again (and click "Apply"). In the process, you will be prompted for your PC's new password. After this process, run your docker command (run or compose) again enter image description here

papigee
  • 6,401
  • 3
  • 29
  • 31
  • Worked for me after I also turned off my VPN – J. Doe Mar 15 '21 at 14:38
  • The command of the picture: `docker run --rm -v c:/Users:/data alpine ls /data`. – questionto42 Mar 21 '21 at 09:08
  • This didn't work for me with docker on mac. The directory was still empty in the container even after explicitly allowing file sharing for the directory I was mounting. I ended up having to move the folder to within the user's home directory to get it to work (see Csongor Halmai's answer). – TechPrime Aug 18 '21 at 15:06
30

Try using:

volumes:
  - ./server:/server

instead of server/ -- there are some cases where Docker doesn't like the trailing slash.

Sergey
  • 3,253
  • 2
  • 33
  • 55
ldg
  • 9,112
  • 2
  • 29
  • 44
16

As per docker volumes documentation,

https://docs.docker.com/engine/tutorials/dockervolumes/#/mount-a-host-directory-as-a-data-volume

The host-dir can either be an absolute path or a name value. If you supply an absolute path for the host-dir, Docker bind-mounts to the path you specify. If you supply a name, Docker creates a named volume by that name

atv
  • 1,950
  • 4
  • 21
  • 26
  • 10
    Good addition, this can be confusing when trying to mount a directory by using a relative path into the container like this `docker run -v myDir:/containerdir ...` because it will silently create a docker volume (as referenced in the answer) named myDir and bind it, but it won't make the contents of myDir available in the container. In this case using the absolute path works, e.g., `docker run --volume $(pwd)/myDir:/containerdir ...` – cw' Sep 30 '17 at 08:08
14

I had similar issue when I wanted to mount a directory from command line:

docker run -tid -p 5080:80  -v /d/my_project:/var/www/html/my_project nimmis/apache-php5

The container has been started successfully but the mounted directory was empty.

The reason was that the mounted directory must be under the user's home directory. So, I created a symlink under c:\Users\<username> that mounts to my project folder d:\my_project and mounted that one:

 docker run -tid -p 5080:80  -v /c/Users/<username>/my_project/:/var/www/html/my_project nimmis/apache-php5
Csongor Halmai
  • 3,239
  • 29
  • 30
3

If you are using Docker for Mac then you need to go to:

Docker Desktop -> Preferences -> Resources -> File Sharing

and add the folder you intend to mount. See the screenshot:

enter image description here

Lasma
  • 345
  • 2
  • 5
1

I don't know if other people made the same mistake but the host directory path has to start from /home

So my msitake was that in my docker-compose I was WRONGLY specifying the following:

services:
    myservice:
        build: .
        ports:
            - 8888:8888
        volumes:
          - /Desktop/subfolder/subfolder2:/app/subfolder

When the host path should have been full path from /home. something like:

services:
    myservice:
        build: .
        ports:
            - 8888:8888
        volumes:
          - home/myuser/Desktop/subfolder/subfolder2:/app/subfolder
KZiovas
  • 3,491
  • 3
  • 26
  • 47
0

On Ubuntu 20.04.4 LTS, with Docker version 20.10.12, build e91ed57, I started observing a similar symptom with no apparent preceding action. After a docker-compose -p production-001 -f deploy/docker-compose.yml up -d --build command, with no changes to one of the services (production-001-volumeConsumingService is up-to-date), a part of the volumes stopped mounting.

# deploy/docker-compose.yml
version: "3"
services:
...
    volumeConsumingService:
        container_name: production-001-volumeConsumingService 
        hostname: production-001-volumeConsumingService
        image: group/production-001-volumeConsumingService
        build:
            context: .
            dockerfile: volumeConsumingService.Dockerfile
        depends_on:
          - anotherServiceDefinedEarlier
        restart: always
        volumes:
            - ../data/certbot/conf:/etc/letsencrypt # mouning
            - ../data/certbot/www:/var/www/certbot # not mounting
            - ../data/www/public:/var/www/public # not mounting
            - ../data/www/root:/var/www/root # not mounting
        command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
        networks:
            - default
            - external
...
networks:
        external:
              name: routing

A workaround that seems to be working is to enforce a restart on the failing service immediately after the docker-compose -p production-001 -f deploy/docker-compose.yml up -d --build command:

docker-compose -p production-001 -f deploy/docker-compose.yml up -d --build && docker stop production-001-volumeConsumingService && docker start production-001-volumeConsumingService

In the case when the volumes are not mounted after a host reboot, adding a cron task to restart the service once should do.

Danny Apostolov
  • 479
  • 3
  • 8
0

In my case, the volume was empty because I did not use the right path format without quotes.

If you have a relative or absolute path with spaces in it, you do not need to use double quotes around the path, you can just use any path with spaces and it will be understood since docker-compose has the ":" as the delimiter and does not check spaces.

Ways that do not work (double quotes are the problem!):

  volumes:
    - "MY_PATH.../my server":/server
    - "MY_PATH.../my server:/server" (I might have missed testing this, not sure!)
    - "./my server":/server
    - ."/my server":/server
    - "./my server:/server"
    - ."/my server:/server"

Two ways how you can do it (no double quotes!):

  volumes:
    - MY_PATH.../my server:/server
    - ./my server:/server
questionto42
  • 7,175
  • 4
  • 57
  • 90
0

In my case, in Windows, the WSL2 distro was a mapped network drive. After disconnecting the mapped drive and restarting Docker, the mounted volume worked OK.

rhy
  • 1