gpt4 book ai didi

docker - 有谁有一个简单的 Dockerfile 和命令行示例,可以将外部目录从 linux 挂载到 docker 镜像中?

转载 作者:行者123 更新时间:2023-12-02 11:43:42 26 4
gpt4 key购买 nike

我正在寻找的是 Linux 上的 Dockerfile 和 docker build+run 命令,以通过容器内的安装点查看/var/tmp。这里的问题都是复杂的情况,或者涉及 OS/X 和 Windows,或者尝试做的不仅仅是简单地安装卷。我的情况是我只是尝试将/var/tmp 挂载到/foobar 的 busybox 镜像,运行包含该镜像的容器,然后使用“ls/foobar”查看内容。

在带有 aufs 的 Linux 4.0.1 上运行“Docker version 1.6.1, build 97cd073”使用本地存储库。

http://docs.docker.com/userguide/dockervolumes/

注意:

Mount a Host Directory as a Data Volume

In addition to creating a volume using the
-v flag you can also mount a directory from
your Docker daemon's host into a container.

<snip>

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp
training/webapp python app.py

This will mount the host directory, /src/webapp,
into the container at /opt/webapp.

Note: If the path /opt/webapp already exists
inside the container's image, its contents
will be replaced by the contents of
/src/webapp on the host to stay consistent
with the expected behavior of mount

This is very useful for testing, for example we can mount our source
code inside the container and see our application at work as we change
the source code. The directory on the host must be specified as an
absolute path and if the directory doesn't exist Docker will
automatically create it for you.

我正在使用获取其“/data”目录的本地存储库通过“-v”切换到 docker run:

docker run -d -p 5000 \
-v '/var/lib/docker/registry:/data/registry/storage' \
'kampka/registry';

这似乎可以作为主机的/var/lib/docker/registry 目录获取添加到其中的条目。

所以我为自己尝试了一个简单的测试:构建 busybox 的最小副本可以访问主机系统上的/var/tmp。

FROM        localhost:5000/lembark/busybox
MAINTAINER lembark@wrkhors.com

VOLUME [ "/foobar" ]

ENV PATH /bin
WORKDIR /

ENTRYPOINT [ "/bin/sh" ]

此时运行“docker build”会执行 VOLUME 命令,但不会创建挂载点:

$ docker build --tag="localhost:5000/lembark/hak" . ;

Sending build context to Docker daemon 7.68 kB
Sending build context to Docker daemon
Step 0 : FROM localhost:5000/lembark/busybox
---> c1a1f5abbf79
Step 1 : MAINTAINER lembark@wrkhors.com
---> Using cache
---> b46677881767
Step 2 : VOLUME /foobar
---> Running in 7127bdbcfb56
---> bcf9c3f1c441
Removing intermediate container 7127bdbcfb56
Step 3 : ENV PATH /bin
---> Running in 89f92c815860
---> 780fea54a67f
Removing intermediate container 89f92c815860
Step 4 : WORKDIR /
---> Running in aa3871c408a1
---> 403190e9415b
Removing intermediate container aa3871c408a1
Step 5 : ENTRYPOINT /bin/sh
---> Running in 4850561f7ebd
---> 77c32530b4a9
Removing intermediate container 4850561f7ebd
Successfully built 77c32530b4a9

步骤 2 中的“VOLUME/foobar”似乎表明安装点应该在运行时可用。

此时使用其中之一

docker run --rm -t -i                     localhost:5000/lembark/hak;
docker run --rm -t -i -v /foobar localhost:5000/lembark/hak;
docker run --rm -t -i -v /var/tmp:/foobar localhost:5000/lembark/hak;

留给我:

# ls -al /foobar
ls: /foobar: No such file or directory

在 VOLUME 之前添加 mkdir 会让我留下/foobar具有匿名卷的目录,而不是来自/var/tmp 的映射:

...

RUN [ "mkdir", "/foobar" ]
VOLUME [ "/foobar" ]

# made a local ./foobar directory, added that to the image.

COPY [ "foobar", "/foobar" ]

其中的 Bofh 与/foobar 一起离开,但无法映射任何外部到它的目录。相反,我不断收到匿名卷:

 # mount | grep foobar;
/dev/mapper/vg00-var--lib on /var/lib/docker/aufs/mnt/dd12f3e11a6fcb88627412a041b7c910e4d32dc1bf0c15330899036c59d7b3d9/foobar type xfs (rw,noatime,nodiratime,attr2,inode64,logbufs=8,logbsize=256k,logdev=/dev/vg02/var-lib-extlog,noquota)

无论有没有 mkdir、VOLUME、COPY 或 -v 的组合,我都无法查看/var/tmp undef/foobar。

谢谢

最佳答案

这对我有用

Dockerfile

FROM busybox:latest
VOLUME /foo/bar

命令

$ docker build -t bb .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM busybox:latest
---> 8c2e06607696
Step 1 : VOLUME /foo/bar
---> Running in a94615dd3353
---> fa500ba91831
Successfully built fa500ba91831
$ docker run -it -v /tmp:/foobar bb ls /foobar
[contents of my /tmp directory]

请注意,您不需要 VOLUME 命令来执行此操作。 docker run -it -v/tmp:/foobar busybox:latest ls/foobar 也能正常工作。

关于docker - 有谁有一个简单的 Dockerfile 和命令行示例,可以将外部目录从 linux 挂载到 docker 镜像中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com