根据docker镜像反推dockerfile

Dockerfile 是一个文本文件,其中包含我们为了构建 Docker 镜像而手动执行的所有命令。
Docker 可以从 Dockerfile 中读取指令来自动构建镜像。我们可以使用 docker build 命令来创建一个自动构建。

# docker history jenkins/inbound-agent:latest --no-trunc

我们有时候想知道官方镜像的dockerfile是怎么写的,需要根据docker image 反推docker file
后来发现docker history命令可以帮助我们实现这个需求,具体如下:

# docker history --format {{.CreatedBy}} --no-trunc=true gaohongyu/jenkins-jnlp:v1|sed "s?/bin/sh\ -c\ \#(nop)\ ??g"|sed "s?/bin/sh\ -c?RUN?g" | tac

输出结果

ADD file:b9b24bd862a79bf6c6e79daf6babca27245063eb52a2f72ffc4fc3494ddd3d48 in /
 CMD ["bash"]
RUN apt-get update && apt-get install -y --no-install-recommends   ca-certificates   curl   netbase   wget  && rm -rf /var/lib/apt/lists/*
RUN set -ex;  if ! command -v gpg > /dev/null; then   apt-get update;   apt-get install -y --no-install-recommends    gnupg    dirmngr   ;   rm -rf /var/lib/apt/lists/*;  fi
RUN apt-get update && apt-get install -y --no-install-recommends   bzr   git   mercurial   openssh-client   subversion     procps  && rm -rf /var/lib/apt/lists/*
RUN set -eux;  apt-get update;  apt-get install -y --no-install-recommends   bzip2   unzip   xz-utils     ca-certificates p11-kit     fontconfig libfreetype6  ;  rm -rf /var/lib/apt/lists/*
 ENV LANG=C.UTF-8
 ENV JAVA_HOME=/usr/local/openjdk-8
 ENV PATH=/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN { echo '#/bin/sh'; echo 'echo "$JAVA_HOME"'; } > /usr/local/bin/docker-java-home && chmod +x /usr/local/bin/docker-java-home && [ "$JAVA_HOME" = "$(docker-java-home)" ]
 ENV JAVA_VERSION=8u222
 ENV JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u222-b10/OpenJDK8U-jdk_
 ENV JAVA_URL_VERSION=8u222b10
RUN set -eux;   dpkgArch="$(dpkg --print-architecture)";  case "$dpkgArch" in   amd64) upstreamArch='x64' ;;   arm64) upstreamArch='aarch64' ;;   *) echo >&2 "error: unsupported architecture: $dpkgArch" ;;  esac;   wget -O openjdk.tgz.asc "${JAVA_BASE_URL}${upstreamArch}_linux_${JAVA_URL_VERSION}.tar.gz.sign";  wget -O openjdk.tgz "${JAVA_BASE_URL}${upstreamArch}_linux_${JAVA_URL_VERSION}.tar.gz" --progress=dot:giga;   export GNUPGHOME="$(mktemp -d)";  gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys CA5F11C6CE22644D42C6AC4492EF8D39DC13168F;  gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys EAC843EBD3EFDB98CC772FADA5CD6035332FA671;  gpg --batch --list-sigs --keyid-format 0xLONG CA5F11C6CE22644D42C6AC4492EF8D39DC13168F | grep '0xA5CD6035332FA671' | grep 'Andrew Haley';  gpg --batch --verify openjdk.tgz.asc openjdk.tgz;  gpgconf --kill all;  rm -rf "$GNUPGHOME";   mkdir -p "$JAVA_HOME";  tar --extract   --file openjdk.tgz   --directory "$JAVA_HOME"   --strip-components 1   --no-same-owner  ;  rm openjdk.tgz*;    {   echo '#!/usr/bin/env bash';   echo 'set -Eeuo pipefail';   echo 'if ! [ -d "$JAVA_HOME" ]; then echo >&2 "error: missing JAVA_HOME environment variable"; exit 1; fi';   echo 'cacertsFile=; for f in "$JAVA_HOME/lib/security/cacerts" "$JAVA_HOME/jre/lib/security/cacerts"; do if [ -e "$f" ]; then cacertsFile="$f"; break; fi; done';   echo 'if [ -z "$cacertsFile" ] || ! [ -f "$cacertsFile" ]; then echo >&2 "error: failed to find cacerts file in $JAVA_HOME"; exit 1; fi';   echo 'trust extract --overwrite --format=java-cacerts --filter=ca-anchors --purpose=server-auth "$cacertsFile"';  } > /etc/ca-certificates/update.d/docker-openjdk;  chmod +x /etc/ca-certificates/update.d/docker-openjdk;  /etc/ca-certificates/update.d/docker-openjdk;   find "$JAVA_HOME/lib" -name '*.so' -exec dirname '{}' ';' | sort -u > /etc/ld.so.conf.d/docker-openjdk.conf;  ldconfig;   javac -version;  java -version
 MAINTAINER Oleg Nenashev <o.v.nenashev@gmail.com>
 ARG VERSION=3.9
 ARG user=jenkins
 ARG group=jenkins
 ARG uid=10000
 ARG gid=10000
 ENV HOME=/home/jenkins
|5 VERSION=3.9 gid=10000 group=jenkins uid=10000 user=jenkins RUN groupadd -g ${gid} ${group}
|5 VERSION=3.9 gid=10000 group=jenkins uid=10000 user=jenkins RUN useradd -c "Jenkins user" -d $HOME -u ${uid} -g ${gid} -m ${user}
 LABEL Description=This is a base image, which provides the Jenkins agent executable (slave.jar) Vendor=Jenkins project Version=3.9
 ARG AGENT_WORKDIR=/home/jenkins/agent
|6 AGENT_WORKDIR=/home/jenkins/agent VERSION=3.9 gid=10000 group=jenkins uid=10000 user=jenkins RUN curl --create-dirs -fsSLo /usr/share/jenkins/slave.jar https://repo.jenkins-ci.org/public/org/jenkins-ci/main/remoting/${VERSION}/remoting-${VERSION}.jar   && chmod 755 /usr/share/jenkins   && chmod 644 /usr/share/jenkins/slave.jar   && curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl   && chmod +x ./kubectl && mv kubectl /usr/local/sbin   && wget -P /tmp https://download.docker.com/linux/static/stable/x86_64/docker-18.09.3.tgz && tar -zxvf /tmp/docker-18.09.3.tgz -C /tmp   && mv /tmp/docker/* /usr/local/bin && rm -rf /tmp/*
 USER [jenkins]
 ENV AGENT_WORKDIR=/home/jenkins/agent
|5 VERSION=3.9 gid=10000 group=jenkins uid=10000 user=jenkins RUN mkdir /home/${user}/.jenkins && mkdir -p ${AGENT_WORKDIR}
 VOLUME [/home/jenkins/.jenkins]
 VOLUME [/home/jenkins/agent]
WORKDIR /home/jenkins
 USER [root]
COPY file:d7e06bf75d3a566a97f90fe56e4687b0b6889b23856455f84c9fdb19e071c54c in /usr/local/bin/jenkins-slave
COPY file:bbd6a87dc65f818fed97e0bf456e8ef7eb7c500eb97e33d2625da821551c088f in /etc/localtime
|5 VERSION=3.9 gid=10000 group=jenkins uid=10000 user=jenkins RUN chmod +x /usr/local/bin/*
 ENTRYPOINT ["jenkins-slave"]
posted @ 2022-11-20 12:53  高宏宇  阅读(885)  评论(0编辑  收藏  举报