我尝试将源文件挂载到位于/ var / dynamo的docker中的目录。我希望能够在本地运行Docker并在我工作时使用我的本地文件。
使用run命令:
docker run -v $(pwd):/var/dynamo -d -t -p 8001:8001 --name dynamo davesrepo/dynamo
使用我的 Dockerfile
FROM python:3.5.1
WORKDIR /var/dynamo
RUN pip3 install tornado
EXPOSE 8001
但是,当我打入容器docker exec -it dynamo /bin/bash
并运行ls -la
时,目录为空!
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 ..
正在运行docker inspect dynamo
显示正确安装的目录
[
{
"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版本是1.9.1
答案 0 :(得分:14)
Docker& Virtualbox似乎在/ Users目录之外安装卷时遇到问题。解决此问题的唯一方法是删除docker机器映像,将/ Users / yourname目录正确设置为Virtualbox中的共享文件夹,并创建一个新的docker机器映像。
解决问题的步骤:
docker-machine stop dev
docker-machine rm dev
docker-machine create --driver virtualbox dev
eval "$(docker-machine env dev)"
docker build -t davesrepo/dynamo -f ./Dockerfile .
docker run -v $(pwd):/var/dynamo -d -t -p 8001:8001 --env-file ./dynamo.env --name dynamo davesrepo/dynamo
docker exec -it dynamo /bin/bash
ls
root@42f9e47fa2de:/var/dynamo# ls
Dockerfile README.md __init__.py __pycache__ bin config.ini requirements.txt seed.sql tests
文件!
答案 1 :(得分:6)
有同样的问题,无法在线找到任何解决方案, 最终我发现我更改了Windows密码,并且docker需要Windows凭据才能装载卷,您可以更新设置>共享驱动器。 只是为那些像我一样在虚空中迷失了几天的人指出了另一种可能的解决方法
答案 2 :(得分:2)
这很奇怪。我尝试用docker 1.6.0
检查此问题,因此我为您创建了一个图像similair:
FROM ubuntu:utopic
WORKDIR /var/dynamo
EXPOSE 8001
然后我将$(pwd)
挂载到容器中,我能够在主机文件系统和容器之间交换文件:
➜ 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
此外,您需要确保主机上的/Users/dave/Sites/davesrepo/dynamo
和容器内的/var/dynamo
具有相同的inode
号码。使用stat
实用程序:
➜ ~ stat -c %i /tmp/ololo
1835031
➜ ~ docker run --rm -it -v /tmp/ololo:/var/dynamo ubuntu:utopic stat -c %i /var/dynamo
1835031
答案 3 :(得分:1)
我不知道这是否对任何人有帮助,但我花了大约一个小时才弄清楚为什么挂载的目录是空的,因为我没有收到任何错误消息,而且我可以看到我的容器中的挂载目录但没有任何文件。
简短回答:主机目录路径必须从 /home 开始
所以在我的 docker-compose 中我错误地指定了以下内容:
services:
myservice:
build: .
ports:
- 8888:8888
volumes:
- /Desktop/subfolder/subfolder2:/app/subfolder
当主机路径应该是 /home 的完整路径时。就像是:
services:
myservice:
build: .
ports:
- 8888:8888
volumes:
- home/myuser/Desktop/subfolder/subfolder2:/app/subfolder
答案 4 :(得分:0)
检查您docker context
:)我忘了改回来。
$ docker context use default
答案 5 :(得分:0)
我的情况是win10 WSL ubuntu。我在ubuntu上安装了docker-compose和docker引擎,在Windows上安装了docker桌面。
原来 /**
* empty class
*/
private static class Foo{}
/**
* non generic class that extends from a generic class
*/
private static class ChildA extends GenericClass<Foo>{}
/**
* generic class that extends from a generic class
*/
private static class ClassChildB<T> extends GenericClass<T>{}
private abstract static class GenericClass<T> {
@SuppressWarnings("unchecked")
public GenericClass() {
Type type = getClass().getGenericSuperclass();
/**
* When constructed from ChildA:
* type = generics.Main$GenericClass<generics.Main$Foo>
* with other worlds the generic type
*
* But when constructed from GenericChildB:
* the generic type is just:
* type = generics.Main$GenericClass<T>
* and it throws an error when trying to cast his to an ParameterizedType
* because <T> is not an acctual class.
*
*/
System.out.println(type);
Class<T> classInstance = (Class<T>) ((ParameterizedType)type).getActualTypeArguments()[0];
//Goal is to get an Class<T> object inside of the constructor of GenericClass
System.out.println(classInstance);
}
}
public static void main(String[] args) {
//works :
GenericClass<Foo> genericClassA = new ChildA();
//does not work:
GenericClass<Foo> genericClassB = new GenericChildB<Foo>();
}
命令必须在窗口中。
答案 6 :(得分:0)
没有其他帮助,这很容易;
如果您运行 docker inspect [containername]
,请检查“Mounts”部分并且您的卷上的 source
设置不正确,如下所示:(mongodb 示例)< /strong>
"Mounts": [
{
"Type": "volume",
"Name": "b1cdd07ca4d5621eda77481824e5068194023f4407ed2674ec81a1573f2d0410",
"Source": "/var/lib/docker/volumes/b1cdd07ca4d5621eda77481824e5068194023f4407ed2674ec81a1573f2d0410/_data",
"Destination": "/data/configdb",
"Driver": "local",
"Mode": "rw",
"RW": true,
"Propagation": ""
}
]
如 docs source
和 target
卷中所述,可以手动指定。
volumes:
- type: bind
source: ./volumes/mongodb
target: /data/db
希望结果
{
"Type": "bind",
"Source": "/home/username/app/backend/volumes/mongodb",
"Destination": "/data/db",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
答案 7 :(得分:-1)
您可能需要在Dockerfile
中包含类似的内容,重要的是VOLUME
在容器中指定mountable directories,然后只有-v $(pwd):/var/dynamo
才有效{ p>
RUN mkdir -p /var/dynamo
VOLUME /var/dynamo
WORKDIR /var/dynamo
答案 8 :(得分:-3)
我使用1.9,这很好用:
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
它将主机上的~/docker_console/var/www
连接到容器中的/var/www
。
如果使用目录名而不是变量怎么办?