如何从图像生成Dockerfile? [英] How to generate a Dockerfile from an image?

查看:327
本文介绍了如何从图像生成Dockerfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从图像生成Docker文件?我想知道两个原因:

Is it possible to generate a Dockerfile from an image? I want to know for two reasons:


  1. 我可以从存储库下载图像,但希望看到生成它们的配方

  1. I can download images from the repository but would like to see the recipe that generated them.

我喜欢保存快照的想法,但一旦完成,就可以使用结构化格式来查看所做的工作。 >

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.


推荐答案

要了解Docker映像的构建方式,请使用
docker history --no-trunc 命令。

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

您可以从图像中构建docker文件,但不会包含您想要完全了解图像生成的一切。您可以提取的内容是Docker文件的MAINTAINER,ENV,EXPOSE,VOLUME,WORKDIR,ENTRYPOINT,CMD和ONBUILD部分。

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 MAINTAINER, ENV, EXPOSE, VOLUME, WORKDIR, ENTRYPOINT, CMD, and ONBUILD parts of the dockerfile.

以下脚本适用于您:

#!/bin/bash
docker history --no-trunc "$1" | \
sed -n -e 's,.*/bin/sh -c #(nop) \(MAINTAINER .*[^ ]\) *0 B,\1,p' | \
head -1
docker inspect --format='{{range $e := .Config.Env}}
ENV {{$e}}
{{end}}{{range $e,$v := .Config.ExposedPorts}}
EXPOSE {{$e}}
{{end}}{{range $e,$v := .Config.Volumes}}
VOLUME {{$e}}
{{end}}{{with .Config.User}}USER {{.}}{{end}}
{{with .Config.WorkingDir}}WORKDIR {{.}}{{end}}
{{with .Config.Entrypoint}}ENTRYPOINT {{json .}}{{end}}
{{with .Config.Cmd}}CMD {{json .}}{{end}}
{{with .Config.OnBuild}}ONBUILD {{json .}}{{end}}' "$1"

我将其作为脚本的一部分重建运行容器作为图像:
https://github.com/docbill/docker-scripts/blob/master/docker- rebase

I use this as part of a script to rebuild running containers as images: https://github.com/docbill/docker-scripts/blob/master/docker-rebase

如果您想要重新打包图像,Dockerfile主要是有用的。

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

要记住的事情是,docker的图像实际上只能是一个真实的tar备份或虚拟机。我这样做了几个码头图像。即使构建历史显示我导入一个巨大的tar文件作为创建映像的第一步...

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...

这篇关于如何从图像生成Dockerfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆