Skip to content

About

The primary purpose of this project is to address the issue of Docker images losing their original build files (Dockerfiles) during distribution.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DockerfileGen

Docker 镜像逆向工程工具 — 自动从镜像层和元数据中还原 Dockerfile。

功能

  • 自动解析镜像 History 和 Config,还原 FROM、RUN、COPY、ADD、ENV、EXPOSE、CMD、ENTRYPOINT 等指令
  • 支持远程 Registry、本地 tarball、OCI image layout 三种镜像来源,自动检测
  • 长 RUN 命令按 && 自动换行,输出可读的 Dockerfile
  • 支持合并连续 RUN 指令(--merge-runs)

安装

go install github.com/TDnorthgarden/DockerfileGen@latest

或从源码构建:

git clone https://github.com/TDnorthgarden/DockerfileGen.git
cd DockerfileGen
go build -o dockerfilegen .

使用

# 扫描远程镜像
dockerfilegen scan -i nginx:1.21-alpine

# 扫描本地 tarball(docker save 导出)
dockerfilegen scan -i ./nginx.tar

# 扫描 OCI image layout 目录
dockerfilegen scan -i ./oci-images/

# 输出到文件
dockerfilegen scan -i alpine:3.18 -o Dockerfile

# 合并连续 RUN 指令
dockerfilegen scan -i nginx:latest --merge-runs -o Dockerfile

镜像来源

来源 格式 示例
远程 Registry image:tag 或 registry/repo:tag nginx:latest、harbor.example.com/proj/img:v1
Docker tarball .tar / .tar.gz 文件 ./image.tar
OCI layout 包含 index.json 的目录 ./oci-dir/

来源类型根据路径格式自动检测。远程镜像支持 Docker Hub 和自建 Harbor 等任何兼容 OCI Distribution Spec 的 Registry,认证通过 ~/.docker/config.json(需提前 docker login)。

输出示例

# Generated by DockerfileGen
FROM nginx:1.21-alpine@sha256:abc123...

WORKDIR /

ENV NGINX_VERSION=1.21.0
LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"
EXPOSE 80/tcp

RUN set -x && \
    addgroup -S nginx && \
    adduser -S -G nginx nginx && \
    apk add --no-cache nginx

COPY docker-entrypoint.sh /

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]

命令参考

dockerfilegen scan [flags]

Flags:
  -i, --image string      镜像引用(必填)
  -o, --output string     输出文件路径(默认 stdout)
      --merge-runs        合并连续 RUN 指令
  -h, --help              帮助信息

技术栈

限制

  • COPY/ADD 源路径无法从 History 中恢复(显示层哈希)
  • 不支持多阶段构建还原(输出为单阶段 Dockerfile)
  • 构建时的 ARG 值不会保存在镜像中,无法恢复
  • BuildKit 特性(heredoc、--mount)可能无法正确解析

License

MIT

About

The primary purpose of this project is to address the issue of Docker images losing their original build files (Dockerfiles) during distribution.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages