2015年6月21日 星期日

[ 常見問題 ] How to generate a Dockerfile from an image?

Source From Here
Question
Is it possible to generate a Dockerfile from an image? I want to know for two reasons:
1. I can download images from the repository but would like to see the recipe that generated them.
2. I like the idea of saving snapshots, but once I am done it would be nice to have a structured format to review what was done.

How-To
To understand how a docker image was built, use the docker history command.

You can build a docker file from an image, but it will not contain everything you would want to fully understand how the image was generated. Reasonably what you can extract is the MAINTAINERENVEXPOSEVOLUMEWORKDIRENTRYPOINTCMDRUN, and ONBUILD parts of the dockerfile.

The following script should work for you:
  1. #!/bin/bash  
  2. docker history --no-trunc "$1" | \  
  3. sed -n -e 's,.*/bin/sh -c #(nop) \(MAINTAINER .*[^ ]\) *0 B,\1,p' | \  
  4. head -1  
  5. docker inspect --format='{{range $e := .Config.Env}}  
  6. ENV {{$e}}  
  7. {{end}}{{range $e,$v := .Config.ExposedPorts}}  
  8. EXPOSE {{$e}}  
  9. {{end}}{{range $e,$v := .Config.Volumes}}  
  10. VOLUME {{$e}}  
  11. {{end}}{{with .Config.User}}USER {{.}}{{end}}  
  12. {{with .Config.WorkingDir}}WORKDIR {{.}}{{end}}  
  13. {{with .Config.Entrypoint}}ENTRYPOINT {{json .}}{{end}}  
  14. {{with .Config.Cmd}}CMD {{json .}}{{end}}  
  15. {{with .Config.OnBuild}}ONBUILD {{json .}}{{end}}' "$1"  
I use this as part of a script to rebuild running containers as images:
https://github.com/docbill/docker-scripts/blob/master/docker-rebase

The Dockerfile is mainly useful if you want to be able to repackage an image.

The thing to keep in mind, is a docker image can actually just be the tar backup of a real or virtual machine. I have made several docker images this way. Even the build history shows me importing a huge tar file as the first step in creating the image...

Supplement
Docker Doc - Dockerfile reference
Docker Practice - Dockerfile
Docker Practice - 從映像檔產生 Dockerfile
CenturyLinkLabs 釋出 dockerfile-from-image 工具,以逆向工程建立出 Dockerfile 。 類似 docker history 指令,透過映像檔每一層的 metadata 來重建出那 Dockerfile ,即便沒有提供任何資訊。

This message was edited 10 times. Last update was at 22/06/2015 09:46:33

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...