gpt4 book ai didi

docker:从图像中提取图层

转载 作者:IT老高 更新时间:2023-10-28 12:47:20 26 4
gpt4 key购买 nike

让我们以鲸鱼图像为例。 docker 历史 显示如下:

IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
6b362a9f73eb 17 months ago /bin/sh -c #(nop) ENV PATH=/usr/local/bin:/us 0 B
<missing> 17 months ago /bin/sh -c sh install.sh 30.37 kB
<missing> 17 months ago /bin/sh -c git reset --hard origin/master 43.27 kB
<missing> 17 months ago /bin/sh -c #(nop) WORKDIR /cowsay 0 B
<missing> 17 months ago /bin/sh -c git clone https://github.com/moxie 89.9 kB
<missing> 17 months ago /bin/sh -c apt-get -y update && apt-get insta 58.58 MB
<missing> 18 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 18 months ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 1.895 kB
<missing> 18 months ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic 194.5 kB
<missing> 18 months ago /bin/sh -c #(nop) ADD file:f4d7b4b3402b5c53f2 188.1 MB

我想提取显示 ADD file:bla 的图层。有这样做的工具/方法吗?

最佳答案

在这种特定情况下,ADD 命令似乎将基础镜像添加到文件系统。如果你运行 docker history --no-trunc docker/whalesay,完整的命令是:

/bin/sh -c #(nop) ADD file:f4d7b4b3402b5c53f266bb7fdd7e728493d9a17f9ef20c8cb1b4759b6e66b70f in /

docker history 报告特定层为 188MB。让我们更详细地看一下这些层:

$ docker save docker/whalesay -o whalesay.tar
$ tar tvf whalesay.tar

...
-rw-r--r-- 0/0 197181952 2015-05-25 22:04 cc88f763e297503d2407d6b462b2b390a6fd006b30f51c8efa03dd88fa801b89/layer.tar
...

看起来是个不错的候选人!您现在可以提取该层并从中提取文件。

$ tar xf whalesay.tar cc88f763e297503d2407d6b462b2b390a6fd006b30f51c8efa03dd88fa801b89/layer.tar
$ tar xf cc88f763e297503d2407d6b462b2b390a6fd006b30f51c8efa03dd88fa801b89/layer.tar etc/passwd

如果您希望从层中提取特定文件,但您不知道哪个层,您可以这样做。首先,提取所有层:

$ tar xf whalesay.tar

现在您已将所有层作为单独的 .tar 文件。让我们找一个文件:

$ for layer in */layer.tar; do tar -tf $layer | grep docker.cow && echo $layer; done
usr/local/share/cows/docker.cow
0523c5a0c4588dde33d61d171c41c2dc5c829db359f4d56ab896ab1c185ed936/layer.tar
cowsay/cows/docker.cow
40e8ae7bb4e5b9eaac56f5be7aa614ed50f163020c87ba59e905e01ef0af0a4f/layer.tar
cowsay/cows/docker.cow
f9bc8676543761ff3033813257937aeb77e9bc84296eaf025e27fe01643927cf/layer.tar

最后,从你想要的层中提取文件:

$ tar xf 0523c5a0c4588dde33d61d171c41c2dc5c829db359f4d56ab896ab1c185ed936/layer.tar \
usr/local/share/cows/docker.cow

这将使用相对于当前目录的完整路径提取该文件。

$ cat usr/local/share/cows/docker.cow 
##
## Docker Cow
##
$the_cow = <<EOC;
$thoughts
$thoughts
$thoughts
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\\______ o __/
\\ \\ __/
\\____\\______/
EOC

关于docker:从图像中提取图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40575752/

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