60

I'm trying to mount my source files to a directory in docker located at /var/dynamo. I'd like to be able to run Docker locally and use my local files as I am working.

with the run command:

docker run -v $(pwd):/var/dynamo -d -t -p 8001:8001 --name dynamo davesrepo/dynamo

with my Dockerfile

FROM python:3.5.1
WORKDIR /var/dynamo
RUN pip3 install tornado
EXPOSE 8001

However, when I bash into the container docker exec -it dynamo /bin/bash and run ls -la the directory is empty!

root@7d3d338891aa:/var/dynamo# ls -la
total 4
drwxr-xr-x  2 root root   40 Dec 29 03:37 .
drwxr-xr-x 24 root root 4096 Dec 29 03:36 ..

Running docker inspect dynamo shows the directory mounted properly

[
{
    "Id": "7d3d338891aae32e001a3bb31d6be17802d238526830396472b011599a97212a",
    "Created": "2015-12-29T03:37:01.416445958Z",
    "Path": "python3",
    "Args": [],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 1314,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2015-12-29T03:37:01.516253705Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "19697c9b5c7561b113d817821280971f063ea0187c82343c0832348fb6863c9a",
    "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/7d3d338891aae32e001a3bb31d6be17802d238526830396472b011599a97212a/resolv.conf",
    "HostnamePath": "/mnt/sda1/var/lib/docker/containers/7d3d338891aae32e001a3bb31d6be17802d238526830396472b011599a97212a/hostname",
    "HostsPath": "/mnt/sda1/var/lib/docker/containers/7d3d338891aae32e001a3bb31d6be17802d238526830396472b011599a97212a/hosts",
    "LogPath": "/mnt/sda1/var/lib/docker/containers/7d3d338891aae32e001a3bb31d6be17802d238526830396472b011599a97212a/7d3d338891aae32e001a3bb31d6be17802d238526830396472b011599a97212a-json.log",
    "Name": "/dynamo",
    "RestartCount": 0,
    "Driver": "aufs",
    "ExecDriver": "native-0.2",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "",
    "ExecIDs": null,
    "HostConfig": {
        "Binds": [
            "/Users/dave/Sites/davesrepo/dynamo:/var/dynamo"
        ],
        "ContainerIDFile": "",
        "LxcConf": [],
        "Memory": 0,
        "MemoryReservation": 0,
        "MemorySwap": 0,
        "KernelMemory": 0,
        "CpuShares": 0,
        "CpuPeriod": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "CpuQuota": 0,
        "BlkioWeight": 0,
        "OomKillDisable": false,
        "MemorySwappiness": -1,
        "Privileged": false,
        "PortBindings": {
            "8001/tcp": [
                {
                    "HostIp": "",
                    "HostPort": "8001"
                }
            ]
        },
        "Links": null,
        "PublishAllPorts": false,
        "Dns": [],
        "DnsOptions": [],
        "DnsSearch": [],
        "ExtraHosts": null,
        "VolumesFrom": null,
        "Devices": [],
        "NetworkMode": "default",
        "IpcMode": "",
        "PidMode": "",
        "UTSMode": "",
        "CapAdd": null,
        "CapDrop": null,
        "GroupAdd": null,
        "RestartPolicy": {
            "Name": "no",
            "MaximumRetryCount": 0
        },
        "SecurityOpt": null,
        "ReadonlyRootfs": false,
        "Ulimits": null,
        "LogConfig": {
            "Type": "json-file",
            "Config": {}
        },
        "CgroupParent": "",
        "ConsoleSize": [
            0,
            0
        ],
        "VolumeDriver": ""
    },
    "GraphDriver": {
        "Name": "aufs",
        "Data": null
    },
    "Mounts": [
        {
            "Source": "/Users/dave/Sites/davesrepo/dynamo",
            "Destination": "/var/dynamo",
            "Mode": "",
            "RW": true
        }
    ],
    "Config": {
        "Hostname": "7d3d338891aa",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "ExposedPorts": {
            "8001/tcp": {}
        },
        "Tty": true,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": [
            "APP_ENV=development",
            "PYTHONPATH=$PYTHONPATH:/var/dynamo",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "LANG=C.UTF-8",
            "PYTHON_VERSION=3.5.1",
            "PYTHON_PIP_VERSION=7.1.2"
        ],
        "Cmd": [
            "python3"
        ],
        "Image": "davesrepo/dynamo",
        "Volumes": null,
        "WorkingDir": "/var/dynamo",
        "Entrypoint": null,
        "OnBuild": null,
        "Labels": {},
        "StopSignal": "SIGTERM"
    },
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "71d99a7d11908a9d1c0156fd33eb3f0ff5ef9f6697bb77103fe2caa1cf488530",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "8001/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "8001"
                }
            ]
        },
        "SandboxKey": "/var/run/docker/netns/71d99a7d1190",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "1cc99b0de89700368f31b43248c90ea45cec756712a2bd2ba2dc57293a5d0a72",
        "Gateway": "172.17.0.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.2",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "MacAddress": "02:42:ac:11:00:02",
        "Networks": {
            "bridge": {
                "EndpointID": "1cc99b0de89700368f31b43248c90ea45cec756712a2bd2ba2dc57293a5d0a72",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:02"
            }
        }
    }
}
]

Docker version is 1.9.1

digitaldavenyc
  • 1,302
  • 1
  • 12
  • 24

12 Answers12

30

Docker & Virtualbox seem to have an issue with mounting a volume outside of the /Users directory. The only way to fix the issue is to delete the docker machine image, properly set the /Users/yourname directory as the share folder in Virtualbox and create a new docker machine image.

Steps to fix the issue:

  1. docker-machine stop dev
  2. docker-machine rm dev
  3. docker-machine create --driver virtualbox dev
  4. eval "$(docker-machine env dev)"
  5. docker build -t davesrepo/dynamo -f ./Dockerfile .
  6. docker run -v $(pwd):/var/dynamo -d -t -p 8001:8001 --env-file ./dynamo.env --name dynamo davesrepo/dynamo
  7. docker exec -it dynamo /bin/bash
  8. ls

root@42f9e47fa2de:/var/dynamo# ls Dockerfile README.md __init__.py __pycache__ bin config.ini requirements.txt seed.sql tests

Files!

nmgeek
  • 2,127
  • 1
  • 23
  • 31
digitaldavenyc
  • 1,302
  • 1
  • 12
  • 24
  • I am sure you hit on the cause of the problem: a folder sharing limitation in Virtualbox is the root cause. But what is different with the way the machine is created here vs. how you created it when the sharing did not work? How do you 'properly set the /Users/yourname directory as the share folder'? Or how do you _not_ properly set the share folder? – nmgeek Jun 05 '16 at 04:39
  • 2
    When I originally setup Docker, I placed my files in /var/dynamo. This is obviously outside of the OSX User directory, this is what Virtualbox defaults to as far as I know. You properly set the files by opening Virtualbox, clicking 'settings' for your docker VM and selecting 'share folders'. From there you can double click on the machine folders and change the path. It's pretty straight forward stuff. – digitaldavenyc Jun 30 '16 at 01:53
  • Finally resolved a mystery related to this and I think it's worth recording here. In addition to VirtualBox and some mentioned 'boot2docker', some other build of docker also has this limit of restricting to $HOME. I installed docker with 'snap' and found out, after lots of time spent and frustration, by executing 'snap info docker', and found the limit right there in the description. Is it able to find out if this limit is in place by a command? – Bing Ren May 16 '20 at 05:17
15

I dont know if this is helpful to anyone but it took me about an hour trying to figure out why the mounted directories were empty since I was not getting any error messages and I could see the mounted directories within my container but without any files.

Short answer: the host directory path has to start from /home

So 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
  • Very helpful, I still don't understand why it is not working with a relative path. – Benjamin Nov 26 '21 at 17:43
  • I don't know why, but using an absolute path starting with `/home` worked for me while a path starting with `${pwd}` failed (though it resolves to home). Thanks! – phi Apr 19 '23 at 09:26
8

I found out that I changed my windows password and docker requires windows credentials to mount volumes, you can update it settings > shared drives.

Kara
  • 6,115
  • 16
  • 50
  • 57
roy
  • 585
  • 5
  • 14
  • thanks that was very helpful. In the current version of docker (2.0.0.3) in windows 10, I went to `settings > shared drives` and had to select the drive, then hit **Reset credentials**, select the drive again and hit the **Apply** button and finally was able to enter the new credentials. – Martin Mar 14 '19 at 13:17
3

Check you docker context :) I forgot to change it back.

$ docker context use default
Jarda Pavlíček
  • 1,636
  • 17
  • 16
3

Wrong source for volume

Nothing else helped and this was rather easy;

If you run docker inspect [containername], checking the "Mounts" section and the source on your volume is not correctly set, looking like the following: (Example for mongodb)

 "Mounts": [
  {
    "Type": "volume",
    "Name": "b1cdd07ca4d5621eda77481824e5068194023f4407ed2674ec81a1573f2d0410",
    "Source": "/var/lib/docker/volumes/b1cdd07ca4d5621eda77481824e5068194023f4407ed2674ec81a1573f2d0410/_data",
    "Destination": "/data/configdb",
    "Driver": "local",
    "Mode": "rw",
    "RW": true,
    "Propagation": ""
  }
]

Set it manually

As stated in the docs source and target of volumes can be specified manually.

volumes:
  - type: bind
    source: ./volumes/mongodb
    target: /data/db

hopefully resulting in

{
  "Type": "bind",
  "Source": "/home/username/app/backend/volumes/mongodb",
  "Destination": "/data/db",
  "Mode": "",
  "RW": true,
  "Propagation": "rprivate"
}
nonNumericalFloat
  • 1,348
  • 2
  • 15
  • 32
1

It's very strange. I tried to check this issue with my docker 1.6.0 so I created an image similair to yours:

FROM ubuntu:utopic
WORKDIR /var/dynamo
EXPOSE 8001

Then I mounted the $(pwd) to the container and I was able to exchange files between host filesystem and container:

➜  ololo  echo $(pwd)                                                                       
/tmp/ololo
➜  ololo  ls         
Dockerfile  kikiboi
➜  ololo  docker run --rm -it -v $(pwd):/var/dynamo volume_issue bash
root@9b15875997ba:/var/dynamo# ls
Dockerfile  kikiboi
root@9b15875997ba:/var/dynamo# touch container_file
root@9b15875997ba:/var/dynamo# exit
➜  ololo  ls
container_file  Dockerfile  kikiboi

Also you need to ensure that /Users/dave/Sites/davesrepo/dynamo on your host and /var/dynamo inside your container have the same inode number. Use stat utility fot this:

➜  ~  stat -c %i /tmp/ololo
1835031
➜  ~  docker run --rm -it -v /tmp/ololo:/var/dynamo ubuntu:utopic stat -c %i /var/dynamo
1835031
Vitaly Isaev
  • 5,392
  • 6
  • 45
  • 64
  • Can you clarify what the stat utility is and how to find the inode? – digitaldavenyc Dec 29 '15 at 15:05
  • Perhaps this is an issue with newer versions of Docker. I'm on version 1.9.1 – digitaldavenyc Dec 29 '15 at 15:17
  • In your example you are creating a file in docker to share with the host. I'm not trying to share files from docker to the host.... it's the other way around, from the host to docker. To see changes as I am developing. – digitaldavenyc Dec 29 '15 at 16:39
  • @digitaldavenyc I'm doing the same thing: you can see that files from ```/tmp/ololo``` (host) still exist in ```/var/dynamo``` folder (container). Probably you really need to downgrade ```docker```. We use ```1.6``` and ```1.7``` with ```Centos 6```. Everything is OK – Vitaly Isaev Dec 29 '15 at 19:30
  • @digitaldavenyc I have added ```stat``` use case – Vitaly Isaev Dec 29 '15 at 19:36
  • Downgrading docker to 1.6.0 fixed the issue! Glad it's fixed, very frustrated with docker that downgrading fixed something as critical as mounting volumes. Thanks for all the help! – digitaldavenyc Dec 30 '15 at 00:18
  • 2
    @digitaldavenyc would you be able to open an issue on GitHub? Mounting of host-directories (permits they are inside `/Users` for boot2docker) should not have changed since docker 1.6, and we haven't had reports for this, so I'm interested to have this looked into. Could be something in your setup. I don't think downgrading to an old version should be the solution. – thaJeztah Dec 30 '15 at 10:13
  • @thaJeztah Sure I commented on a separate thread but I'll open up a new issue. – digitaldavenyc Dec 30 '15 at 15:18
1

In my case it appeared that my WSL didn't see anything on the drive (D) where my mounted folder was located. But other drives (C, E, G) were visible just fine. This was resulting for Docker not see anything.

TLDR: this is what helped me

  1. In PowerShell run wsl
  2. Run mount -t drvfs D: /mnt/host/d. Where D: is your Windows drive and /mnt/host/d the path where the contents of the D: will be accessible. /mnt/host/d must be similar to your other drives locations to know this you can run df -h command.

enter image description here

End.

From now your drive and all it contents must be visible by wsl as well as by Docker when you mount you directory.

But I can't tell why this drive wasn't mounted originally. I use fresh Windows 11 and installed wsl during Docker installation.

simply good
  • 991
  • 1
  • 12
  • 26
0

You might need to include something like this in Dockerfile, the important is the VOLUME to specify the mountable directories in the container then only -v $(pwd):/var/dynamo will be valid

RUN mkdir -p /var/dynamo
VOLUME /var/dynamo
WORKDIR /var/dynamo
Visruth
  • 3,430
  • 35
  • 48
  • This won't work when your using virtualbox verson of docker machine. Virtualbox can't load files this way and therefore neither can docker machine. – digitaldavenyc Jan 19 '18 at 20:00
  • 1
    @digitaldavenyc you didn't mention anything about Virtualbox in the question! – Visruth Sep 25 '20 at 18:33
0

my situation was win10 WSL ubuntu. I had docker-compose and docker engine installed on ubuntu, docker desktop on windows. It turns out the docker run -v $(pwd):<containerPathName> command has to be in the windows.

0

I had a similar issue which took almost 2 days to fix. My docker compose file had 2 services integrated. The second service able to get the CSV file from the source code the defined volume. But the first service could not. I defined the container/path correctly for both volumes.

I tried with changing the host/path but the issue persisted. At last I just changed the container/path to /var/opt which is the standard Linux directory. And the issue resolved. In fact the WORKDIR of the Dockerfile of this service is not /var/opt but /workplace. But when using /workplace in the docker-compose.yml, it was not able to get the CSV file.

Jay
  • 168
  • 2
  • 8
0

If you have run into a similar problem running from a the command line then as mentioned above use the full path name from root on a linux based machine.

docker run --entrypoint bash -v /Users/<username>/cool_tech_stuff/data:/data -it ubuntu

-3

I use 1.9 and this works fine:

Docker run  -d -t --restart=on-failure:3 -h docker_console -p 8880:80 -v ~/docker_console/var/www:/var/www --name docker_console repo/docker_console

It connects ~/docker_console/var/www on the host to /var/www in the container. What if you use a directory name and not a variable?

Visruth
  • 3,430
  • 35
  • 48
Chris
  • 103
  • 1
  • 6
  • Read the accepted response, the issue was mounting outside the user directory on the host. – digitaldavenyc Jan 21 '18 at 02:59
  • Your also not reading the issue properly or the accepted response, the issue was that the mounted volume for VirtualBox was moved outside of the user directory, which VirtualBox can't do. – digitaldavenyc Jan 21 '18 at 03:09