通过Docker Image找回Dockerfile

最近项目组上持续集成,于是利用jenkins的docker镜像做二次开发,原本写好的Dockerfile在生成docker image后不小心被误删,一番折腾后终于找了回来。

如下图所示:

  • jenkins为原始镜像
  • auto-jenkins为使用Dockerfile生成的镜像

1

使用history命令查询auto-jenkins镜像所执行的所有命令:

1
docker history auto-jenkins --no-trunc

2

可以从查询结果中识别出Dockerfile以外的命令,然后自下而上,找回Dockerfile的执行命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FROM jenkins

USER root

RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
RUN sed -i 's|security.debian.org|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list
RUN apt-get update && apt-get upgrade -y && apt-get install -y apt-utils sudo
RUN apt-get install -y build-essential curl libunwind8 gettext apt-transport-https
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg \
&& mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg \
&& sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list' \
&& apt-get update && apt-get install -y dotnet-sdk-2.0.0
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - && apt-get install -y nodejs
RUN npm install -g cnpm --registry=https://registry.npm.taobao.org
RUN apt-get clean && apt-get autoclean
RUN rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV JAVA_OPTS=-Duser.timezone=Asia/Shanghai

USER jenkins

综上,从Docker Image找回Dockerfile,需满足如下条件:

  • 需存在Dockerfile生成的Docker Image
  • 利用Docker Image只能找到任何已提交到Image的指令,如Dockerfile生成的Image,或Container提交的Image。如果在Container中执行的指令没有Commit到Image,则不能用Image的history命令查询。

通过Docker Image找回Dockerfile

https://wurang.net/docker_image_to_dockerfile/

作者

Wu Rang

发布于

2017-10-30

更新于

2021-12-06

许可协议

评论