从镜像文件中提取文件

从镜像文件中提取文件

偶尔会遇到调试容器需要查看内部文件,但是容器内没有bash或者sh,无法通过 docker run 或者 kubectl exec 进入容器内部的情况。

遇到上述问题,可以从image文件下手,image文件本质上还是一个打包文件,可以通过 tar 命令解压,以下是一些常用操作:

  • 查看image的层次结构
[root@worker-1 ~]# docker history registry.kube.com/broker/k8s-broker-postgresql:1.0-SNAPSHOT --no-trunc 
IMAGE                                                                     CREATED             CREATED BY          SIZE                COMMENT
sha256:38ff7de0cb017a7dc39f9b5ae2ab1e5086571b60a35a5faaa17246f4b645cffb   2 months ago        jib-gradle-plugin   8.31kB              
<missing>                                                                 2 months ago        jib-gradle-plugin   2.2kB               
<missing>                                                                 2 months ago        jib-gradle-plugin   605kB               
<missing>                                                                 2 months ago        jib-gradle-plugin   33.7MB              
<missing>                                                                 49 years ago        bazel build ...     99.8MB              
<missing>                                                                 49 years ago        bazel build ...     1.93MB              
<missing>                                                                 49 years ago        bazel build ...     16.8MB              
  • 导出image到本地
[root@worker-1 ~]# docker save -o k8s-broker-postgresql.tar registry.kube.com/broker/k8s-broker-postgresql:1.0-SNAPSHOT
  • 查看本地tar文件的结构
[root@worker-1 ~]# tar -tvf k8s-broker-postgresql.tar 
drwxr-xr-x 0/0               0 2018-11-22 17:29 01102906d17f0c60c86ef99e4356f26b570ec3b3d847ab15061501538445aebc/
-rw-r--r-- 0/0               3 2018-11-22 17:29 01102906d17f0c60c86ef99e4356f26b570ec3b3d847ab15061501538445aebc/VERSION
-rw-r--r-- 0/0             482 2018-11-22 17:29 01102906d17f0c60c86ef99e4356f26b570ec3b3d847ab15061501538445aebc/json
-rw-r--r-- 0/0       100014080 2018-11-22 17:29 01102906d17f0c60c86ef99e4356f26b570ec3b3d847ab15061501538445aebc/layer.tar
drwxr-xr-x 0/0               0 2018-11-22 17:29 07bcd7d2d3168a40244ca2311bbc6dc1cd0f5646175c61ee48809ace75a04a0e/
-rw-r--r-- 0/0               3 2018-11-22 17:29 07bcd7d2d3168a40244ca2311bbc6dc1cd0f5646175c61ee48809ace75a04a0e/VERSION
-rw-r--r-- 0/0             406 2018-11-22 17:29 07bcd7d2d3168a40244ca2311bbc6dc1cd0f5646175c61ee48809ace75a04a0e/json
-rw-r--r-- 0/0        18391040 2018-11-22 17:29 07bcd7d2d3168a40244ca2311bbc6dc1cd0f5646175c61ee48809ace75a04a0e/layer.tar
-rw-r--r-- 0/0            1544 2018-11-22 17:29 38ff7de0cb017a7dc39f9b5ae2ab1e5086571b60a35a5faaa17246f4b645cffb.json
drwxr-xr-x 0/0               0 2018-11-22 17:29 3a0015a7c32bfb71b8fc4f763208f9272e198eadd5a6c94b56abf8e7963b288e/
-rw-r--r-- 0/0               3 2018-11-22 17:29 3a0015a7c32bfb71b8fc4f763208f9272e198eadd5a6c94b56abf8e7963b288e/VERSION
-rw-r--r-- 0/0             482 2018-11-22 17:29 3a0015a7c32bfb71b8fc4f763208f9272e198eadd5a6c94b56abf8e7963b288e/json
-rw-r--r-- 0/0        33718272 2018-11-22 17:29 3a0015a7c32bfb71b8fc4f763208f9272e198eadd5a6c94b56abf8e7963b288e/layer.tar
drwxr-xr-x 0/0               0 2018-11-22 17:29 516bb5f7a9b7c50eff3f37cdf8136e7fa9f65ced4d84b1a4f4d0db25ddbdf0f7/
-rw-r--r-- 0/0               3 2018-11-22 17:29 516bb5f7a9b7c50eff3f37cdf8136e7fa9f65ced4d84b1a4f4d0db25ddbdf0f7/VERSION
-rw-r--r-- 0/0             482 2018-11-22 17:29 516bb5f7a9b7c50eff3f37cdf8136e7fa9f65ced4d84b1a4f4d0db25ddbdf0f7/json
-rw-r--r-- 0/0         1966080 2018-11-22 17:29 516bb5f7a9b7c50eff3f37cdf8136e7fa9f65ced4d84b1a4f4d0db25ddbdf0f7/layer.tar
drwxr-xr-x 0/0               0 2018-11-22 17:29 5ca112839bf378fb4c4654e4f32a5798895fd2433acdc9970f17618c889d8af0/
-rw-r--r-- 0/0               3 2018-11-22 17:29 5ca112839bf378fb4c4654e4f32a5798895fd2433acdc9970f17618c889d8af0/VERSION
-rw-r--r-- 0/0             482 2018-11-22 17:29 5ca112839bf378fb4c4654e4f32a5798895fd2433acdc9970f17618c889d8af0/json
-rw-r--r-- 0/0            6144 2018-11-22 17:29 5ca112839bf378fb4c4654e4f32a5798895fd2433acdc9970f17618c889d8af0/layer.tar
drwxr-xr-x 0/0               0 2018-11-22 17:29 b765fef9020c0b3cbe99d450dc51c57fb0637209d3da5fe8cb4efa2bdc952125/
-rw-r--r-- 0/0               3 2018-11-22 17:29 b765fef9020c0b3cbe99d450dc51c57fb0637209d3da5fe8cb4efa2bdc952125/VERSION
-rw-r--r-- 0/0             996 2018-11-22 17:29 b765fef9020c0b3cbe99d450dc51c57fb0637209d3da5fe8cb4efa2bdc952125/json
-rw-r--r-- 0/0           13824 2018-11-22 17:29 b765fef9020c0b3cbe99d450dc51c57fb0637209d3da5fe8cb4efa2bdc952125/layer.tar
drwxr-xr-x 0/0               0 2018-11-22 17:29 d5d92757c71f5f9420c7dce57428a9e6a4ab9f00797bee1f928073c446b1d873/
-rw-r--r-- 0/0               3 2018-11-22 17:29 d5d92757c71f5f9420c7dce57428a9e6a4ab9f00797bee1f928073c446b1d873/VERSION
-rw-r--r-- 0/0             482 2018-11-22 17:29 d5d92757c71f5f9420c7dce57428a9e6a4ab9f00797bee1f928073c446b1d873/json
-rw-r--r-- 0/0          608768 2018-11-22 17:29 d5d92757c71f5f9420c7dce57428a9e6a4ab9f00797bee1f928073c446b1d873/layer.tar
-rw-r--r-- 0/0             710 1970-01-01 08:00 manifest.json
-rw-r--r-- 0/0             135 1970-01-01 08:00 repositories
  • 解压出所有的layer.tar文件,这些文件对应了image文件中的每一层
[root@worker-1 ~]# tar -xvf k8s-broker-postgresql.tar */*.tar
01102906d17f0c60c86ef99e4356f26b570ec3b3d847ab15061501538445aebc/layer.tar
07bcd7d2d3168a40244ca2311bbc6dc1cd0f5646175c61ee48809ace75a04a0e/layer.tar
3a0015a7c32bfb71b8fc4f763208f9272e198eadd5a6c94b56abf8e7963b288e/layer.tar
516bb5f7a9b7c50eff3f37cdf8136e7fa9f65ced4d84b1a4f4d0db25ddbdf0f7/layer.tar
5ca112839bf378fb4c4654e4f32a5798895fd2433acdc9970f17618c889d8af0/layer.tar
b765fef9020c0b3cbe99d450dc51c57fb0637209d3da5fe8cb4efa2bdc952125/layer.tar
d5d92757c71f5f9420c7dce57428a9e6a4ab9f00797bee1f928073c446b1d873/layer.tar
  • 解压所有layer中的文件或者特定layer中的文件
[root@worker-1 ~]# mkdir target
[root@worker-1 ~]# for layer in */layer.tar; do tar -xvf $layer -C target/; done;
[root@worker-1 ~]# ll target/
total 4
drwxr-xr-x.  5 root root   50 Jan  1  1970 app
drwxr-xr-x.  2 root root    6 Jan  1  1970 bin
drwxr-xr-x.  2 root root    6 Jan  1  1970 boot
drwxr-xr-x.  2 root root    6 Jan  1  1970 dev
drwxr-xr-x. 11 root root 4096 Feb 15 15:06 etc
drwx------.  2 root root    6 Jan  1  1970 home
drwxr-xr-x.  3 root root   30 Jan  1  1970 lib
drwxr-xr-x.  2 root root   34 Feb 15 15:06 lib64
drwxr-xr-x.  2 root root    6 Jan  1  1970 proc
drwx------.  2 root root    6 Jan  1  1970 root
drwxr-xr-x.  2 root root    6 Jan  1  1970 run
drwxr-xr-x.  2 root root    6 Jan  1  1970 sbin
drwxr-xr-x.  2 root root    6 Jan  1  1970 sys
drwxrwxrwt.  2 root root    6 Jan  1  1970 tmp
drwxr-xr-x.  9 root root   92 Jan  1  1970 usr
drwxr-xr-x. 11 root root  116 Jan  1  1970 var
  • target目录中得到的是容器内文件系统视图,如果同一个文件同时出现在多个layer中,考虑文件覆盖
  • 如果iamge文件比较大,可以根据image层次结构中layer的大小来推测对应的layer.tar文件,单独解压
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页