1 概述

反向生成 Dockerfile总结了下面几个场景 :

2 反向生成Dockerfile内容工具

2.1 Docker history && nerdctl history命令

2.1.1 docker history

docker historydocker自带命令工具,一般情况下它可以满足我们查看构建过程需求,但是有一些不足,它的整个展示结果逆序的和我们的Dockerfile是相反的, docker history 命令会还有一个局限性,镜像必须是本地存在的,所以镜像需要提前先 pull 下来。

2.1.1.1 用法
docker history [OPTIONS] IMAGE

$ docker history -h
Flag shorthand -h has been deprecatedplease use --help

Usage: docker history [OPTIONS] IMAGE

Show the history of aimage

Options:
      --format string   Pretty-print images using a Go template
  -H, --human           Print sizeand datein humareadable format (default true)
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs
2.1.1.2 示例
$ docker history nginx:latest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
a6bd71f48f68        13 days ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B
<missing&gt;           13 days ago         /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B
<missing&gt;           13 days ago         /bin/sh -c #(nop)  EXPOSE 80                    0B
<missing>           13 days ago         /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B
<missing>           13 days ago         /bin/sh -c #(nop) COPY file:9e3b2b63db9f8fc7…   4.62kB
<missing>           13 days ago         /bin/sh -c #(nop) COPY file:57846632accc8975…   3.02kB
<missing>           13 days ago         /bin/sh -c #(nop) COPY file:3b1b9915b7dd898a…   298B
<missing>           13 days ago         /bin/sh -c #(nop) COPY file:caec368f5a54f70a…   2.12kB
<missing>           13 days ago         /bin/sh -c #(nop) COPY file:01e75c6dd0ce317d…   1.62kB
<missing>           13 days ago         /bin/sh -c set -x     &amp;&amp; groupadd --system -…   112MB
<missing>           13 days ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bookworm   0B
<missing>           13 days ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.8.2        0B
<missing>           13 days ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.25.3     0B
<missing>           13 days ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B
<missing>           13 days ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           13 days ago         /bin/sh -c #(nop) ADD file:d261a6f6921593f1e…   74.8MB

2.1.2 nerdctl history

nerdctl history 和 docker history 的区别在于 :

nerdctl history也是只可以满足我们查看构建过程需求,并不是很全面;

用法 :

nerdctl history nginx:latest

如果只是想查看构建的过程,history 参数已经足够了,只是它没能生成一个完整的 Dockerfile;

2.2 dfimage工具

dfimage 只是一个镜像的名称,由 alpine 官方制作的,其实里面运行了一个工具,叫做 Whaler;

Whaler 是一个 Go 程序,旨在将 docker 镜像逆向工程得到创建它的 Dockerfile 中;

当前执行以下操作

如果想要个人构建 whaler 镜像,whaler 的 github 项目里面也提供了 Dockerfile,只需要下载好 whaler 的源码包,稍稍修改一下就可以构建了

已经构建好的镜像,也可以直接拿来用

用法 :

alias dfimage="docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock:ro alpine/dfimage"
$ alias whaler="docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock:ro pegleg/whaler"

$ dfimage nginx:1.16
$ whaler nginx:1.16

Analyzing nginx:1.16
Docker Version: 18.09.7
GraphDriver: overlay2
Environment Variables
|PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|NGINX_VERSION=1.16.1
|NJS_VERSION=0.3.8
|PKG_RELEASE=1~buster

Open Ports
|80

Image user
|User is root

Potential secrets:
Dockerfile:
CMD ["bash"]
LABEL maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>
ENV NGINX_VERSION=1.16.1
ENV NJS_VERSION=0.3.8
ENV PKG_RELEASE=1~buster
RUN set -x  
        &amp;&ampaddgroup --system --gid 101 nginx  
        &amp;&ampadduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginuser" --shell /bin/false --uid 101 nginx  
        &amp;& apt-get update  
        && apt-get install --no-install-recommends --no-install-suggests -y gnupgca-certificates  
        && NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; found=''; for server in ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu ; do echo "Fetching GPG key $NGINX_GPGKEY from $server"; apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY"  
        && found=yes  
        && breakdone; test -z "$found"  
        && echo >&2 "errorfailed to fetch GPG key $NGINX_GPGKEY"  
        && exit 1; apt-get remove --purge --auto-remove -y gnupg1  
        && rm -rf /var/lib/apt/lists/*  
        && dpkgArch="$(dpkg --print-architecture)"  
        && nginxPackages=" nginx=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-xslt=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-geoip=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-${PKG_RELEASE} "  
        && case "$dpkgArch" in amd64|i386) echo "deb https://nginx.org/packages/debianbuster nginx" >> /etc/apt/sources.list.d/nginx.list  
        && apt-get update ;; *) echo "deb-src https://nginx.org/packages/debianbuster nginx" >> /etc/apt/sources.list.d/nginx.list  
        && tempDir="$(mktemp -d)"  
        && chmod 777 "$tempDir"  
        && savedAptMark="$(apt-marshowmanual)"  
        && apt-get update  
        && apt-get build-dep -y $nginxPackages  
        && ( cd "$tempDir"  
        && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" apt-get source --compile $nginxPackages )  
        && apt-marshowmanual | xargs apt-marauto > /dev/null  
        && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; }  
        && ls -lAFh "$tempDir"  
        && ( cd "$tempDir"  
        && dpkg-scanpackages . > Packages )  
        && grep '^Package: ' "$tempDir/Packages"  
        && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list  
        && apt-get -o Acquire::GzipIndexes=false update ;; esac  
        && apt-get install --no-install-recommends --no-install-suggests -y $nginxPackages gettext-base  
        && apt-get remove --purge --auto-remove -y ca-certificates  
        && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list  
        && if [ -n "$tempDir" ]; then apt-get purge -y --auto-remove  
        && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; fi
RUN ln -sf /dev/stdout /var/log/nginx/access.log  
        && ln -sf /dev/stderr /var/log/nginx/error.log
EXPOSE 80
STOPSIGNAL SIGTERM
CMD ["nginx" "-g" "daemooff;"]

输出信息分别为:

原文地址:https://blog.csdn.net/ygq13572549874/article/details/134792670

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_41592.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注