• Stars
    star
    574
  • Rank 77,739 (Top 2 %)
  • Language
    Ruby
  • Created over 10 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

NOTE

This repo is no longer being maintained. Users are welcome to fork it, but we make no warranty of its functionality.

dockerfile-from-image

Reverse-engineers a Dockerfile from a Docker image.

Similar to how the docker history command works, the dockerfile-from-image script is able to re-create the Dockerfile (approximately) that was used to generate an image using the metadata that Docker stores alongside each image layer.

Usage

The Ruby dockerfile-from-image script is itself packaged as a Docker image so it can easily be executed with the Docker run command:

docker run -v /var/run/docker.sock:/var/run/docker.sock \
  centurylink/dockerfile-from-image <IMAGE_TAG_OR_ID>

The <IMAGE_TAG_OR_ID> parameter can be either an image tag (e.g. ruby) or an image ID (either the truncated form or the complete image ID).

Since the script interacts with the Docker API in order to query the metadata for the various image layers it needs access to the Docker API socket. The -v flag shown above makes the Docker socket available inside the container running the script.

Note that the script only works against images that exist in your local image repository (the stuff you see when you type docker images). If you want to generate a Dockerfile for an image that doesn't exist in your local repo you'll first need to docker pull it.

Example

Here's an example that shows the official Docker ruby image being pulled and the Dockerfile for that image being generated.

$ docker pull ruby
Pulling repository ruby

$ docker run --rm -v /run/docker.sock:/run/docker.sock centurylink/dockerfile-from-image
Usage: dockerfile-from-image.rb [options] <image_id>
    -f, --full-tree                  Generate Dockerfile for all parent layers
    -h, --help                       Show this message

$ docker run --rm -v /run/docker.sock:/run/docker.sock centurylink/dockerfile-from-image ruby
FROM buildpack-deps:latest
RUN useradd -g users user
RUN apt-get update && apt-get install -y bison procps
RUN apt-get update && apt-get install -y ruby
ADD dir:03090a5fdc5feb8b4f1d6a69214c37b5f6d653f5185cddb6bf7fd71e6ded561c in /usr/src/ruby
WORKDIR /usr/src/ruby
RUN chown -R user:users .
USER user
RUN autoconf && ./configure --disable-install-doc
RUN make -j"$(nproc)"
RUN make check
USER root
RUN apt-get purge -y ruby
RUN make install
RUN echo 'gem: --no-rdoc --no-ri' >> /.gemrc
RUN gem install bundler
ONBUILD ADD . /usr/src/app
ONBUILD WORKDIR /usr/src/app
ONBUILD RUN [ ! -e Gemfile ] || bundle install --system

Run it as local command

$ docker pull centurylink/dockerfile-from-image
$ alias dfimage="docker run --rm -v /var/run/docker.sock:/var/run/docker.sock centurylink/dockerfile-from-image"
$ dfimage --help
Usage: dockerfile-from-image.rb [options] <image_id>
    -f, --full-tree                  Generate Dockerfile for all parent layers
    -h, --help                       Show this message
$ dfimage ruby

How Does It Work?

When an image is constructed from a Dockerfile, each instruction in the Dockerfile results in a new layer. You can see all of the image layers by using the docker images command with the (soon-to-deprecated) --tree flag.

$ docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
└─511136ea3c5a Virtual Size: 0 B Tags: scratch:latest
  └─1e8abad02296 Virtual Size: 121.8 MB
    └─f106b5d7508a Virtual Size: 121.8 MB
      └─0ae4b97648db Virtual Size: 690.2 MB
        └─a2df34bb17f4 Virtual Size: 808.3 MB Tags: buildpack-deps:latest
          └─86258af941f7 Virtual Size: 808.6 MB
            └─1dc22fbdefef Virtual Size: 846.7 MB
              └─00227c86ea87 Virtual Size: 863.7 MB
                └─564e6df9f1e2 Virtual Size: 1.009 GB
                  └─55a2d383d743 Virtual Size: 1.009 GB
                    └─367e535883e4 Virtual Size: 1.154 GB
                      └─a47bb557ed2a Virtual Size: 1.154 GB
                        └─0d4496202bc0 Virtual Size: 1.157 GB
                          └─5db44b586412 Virtual Size: 1.446 GB
                            └─bef6f00c8d6d Virtual Size: 1.451 GB
                              └─5f9bee597a47 Virtual Size: 1.451 GB
                                └─bb98b84e0658 Virtual Size: 1.452 GB
                                  └─6556c531b6c1 Virtual Size: 1.552 GB
                                    └─569e14fd7575 Virtual Size: 1.552 GB
                                      └─fc3a205ba3de Virtual Size: 1.555 GB
                                        └─5fd3b530d269 Virtual Size: 1.555 GB
                                          └─6bdb3289ca8b Virtual Size: 1.555 GB
                                            └─011aa33ba92b Virtual Size: 1.555 GB Tags: ruby:2, ruby:2.1, ruby:2.1.1, ruby:latest

Each one of these layers is the result of executing an instruction in a Dockerfile. In fact, if you do a docker inspect on any one of these layers you can see the instruction that was used to generate that layer.

$ docker inspect 011aa33ba92b
[{
  . . .
  "ContainerConfig": {
    "Cmd": [
        "/bin/sh",
        "-c",
        "#(nop) ONBUILD RUN [ ! -e Gemfile ] || bundle install --system"
    ],
    . . .
}]

The output above has been truncated, but nested within the ContainerConfig data you'll find the Dockerfile command that generated this layer (in this case it was an ONBUILD instruction).

The dockerfile-from-image script works by simply walking backward through the layer tree and collecting the commands stored with each layer. When the script reaches the first tagged layer (or the root of the tree) it stops and displays the (reversed) list of commands. If you want to generate the commands going all the way back to the root image layer you can use the -f flag to walk the entire tree.

Limitations

As the dockerfile-from-image script walks the list of layers contained in the image it stops when it reaches the first tagged layer. It is assumed that a layer which has been tagged represents a distinct image with its own Dockerfile so the script will output a FROM directive with the tag name.

In the example above, the ruby image contained a layer in the local image repository which had been tagged with buildpack-deps (though it wasn't shown in the example, this likely means that buildpack-deps:latest was also pulled at some point). If the buildpack-deps layer had not been tagged, the dockerfile-from-image script would have continued outputing Dockerfile directives until it reached the root layer.

Also note that the output generated by the script won't match exactly the original Dockerfile if either the COPY or ADD directives (like the example above) are used. Since we no longer have access to the build context that was present when the original docker build command was executed all we can see is that some directory or file was copied to the image's filesystem (you'll see the file/directory checksum and the destination it was copied to).

More Repositories

1

panamax-ui

The Web GUI for Panamax
JavaScript
1,439
star
2

golang-builder

Containerized build environment for compiling an executable Golang package and packaging it in a light-weight Docker container.
Shell
398
star
3

dray

An engine for managing the execution of container-based workflows.
Go
383
star
4

building

Build a Docker container for any app using Heroku Buildpacks
Ruby
230
star
5

zodiac

A lightweight tool for easy deployment and rollback of dockerized applications.
Go
196
star
6

fig2coreos

Convert fig.yml to CoreOS formatted systemd configuration files
Ruby
174
star
7

docker-image-graph

HTML
114
star
8

panamax-coreos

panamax-coreos installs the Panamax application, which is made up of the panamax-ui and panamax-api codebases.
Shell
84
star
9

panamax-api

The Local Panamax Agent
Ruby
82
star
10

ca-certs-base-image

Minimal Docker base image with standard certificate authority root certificates
Dockerfile
68
star
11

lorry-ui

Docker Compose YAML Editor
JavaScript
64
star
12

panamax-public-templates

Ruby
63
star
13

alpine-rails

A lightweight Rails image based on Alpine Linux
Dockerfile
45
star
14

panamax-contest-templates

Ruby
43
star
15

lorry

The API portion of Lorry UI a Docker Compose YAML Editor
Ruby
34
star
16

fleet-api

Ruby
28
star
17

docker-mysql

Shell
21
star
18

docker-ngrok

Dockerfile
19
star
19

docker-serf

Docker image for serf
Dockerfile
19
star
20

flatcar

Rails development environment manager
Ruby
17
star
21

ctlc-docker-wordpress

PHP
15
star
22

ctlc-docker-dropbox

A Dropbox Docker container
Dockerfile
14
star
23

nginx-ssl-proxy

Shell
14
star
24

docker-reg-client

Go Wrapper for the Docker Registry v1 API
Go
13
star
25

ctlc-docker-ambassador

Dockerfile
13
star
26

panamax-kubernetes-adapter-go

The Kubernetes adapter in combination with the Panamax remote agent enables the deployment of a Panamax template to a Kubernetes cluster.
Go
13
star
27

ctl-base-ui

A library of UI components for CTL labs web projects
CSS
12
star
28

ctlc-docker-haproxy-serf

Serf-aware Haproxy Docker Container
Shell
11
star
29

ctlc-docker-haproxy

Serf enabled haproxy docker container
Shell
11
star
30

openssl

Dockerfile
10
star
31

docker-sphinx

Docker image of Sphinx search
Dockerfile
9
star
32

docker-wordpress

PHP
8
star
33

panamax-remote-agent

The remote agent for managing Panamax deployments
Ruby
8
star
34

haproxy-etcd

Shell
8
star
35

docker-gitlab

Docker image for use with linked containers
Shell
8
star
36

docker-drupal

Dockerfile
8
star
37

panamax-kubernetes-adapter

The Kubernetes adapter in combination with the Panamax remote agent enables the deployment of a Panamax template to a Kubernetes cluster.
Ruby
8
star
38

docker-apache-php

Dockerfile
7
star
39

docker-wetty-cli

Dockerfile
7
star
40

panamax-ruby-base

Base image for Ruby-based container services
Ruby
6
star
41

ctlc-docker-amb-serf

Docker Ambassador with Serf registration built-in
Shell
6
star
42

ruby-base-image

Dockerfile
6
star
43

ctlc-docker-amb-etcd

Docker Ambassador with Etcd registration built-in
Shell
6
star
44

ctlc-docker-wordpress-serf

WordPress Docker Container with Serf built-in
Dockerfile
6
star
45

panamax-template-validator

Ruby
5
star
46

panamax-fleet-adapter

Ruby
5
star
47

panamax-marathon-adapter

The Marathon adapter in combination with the Panamax remote agent enables the deployment of a Panamax template to a Mesos cluster.
Go
5
star
48

buildpack-runner

Shell
5
star
49

prettycli

A generic library of helpers for pretty text output, intended for TUI applications.
Go
5
star
50

kube-install

Shell
5
star
51

docker-coreos-cli

Dockerfile
4
star
52

pmx-runner

Ruby
4
star
53

ctlc-docker-mysql-serf

Serf-aware MySQL Docker Container
Shell
4
star
54

panamaxcli

A Panamax Remote Agent command-line application
Go
4
star
55

docket

Python
4
star
56

alpine-nginx

Base Docker image containing Nginx running in Alpine Linux version 3.1.
Dockerfile
4
star
57

remote-agent-setup

Shell
3
star
58

ubuntu-rails

A lightweight Rails image based on Ubuntu
Dockerfile
3
star
59

lighttpd

Dockerfile
3
star
60

stackedit

Dockerfile for an image to run StackEdit https://stackedit.io
Shell
3
star
61

docker-postgresql

Shell
3
star
62

panamax-contrib

Shell
3
star
63

dot-files

Team mandated dot files
Vim Script
3
star
64

ctlc-docker-serf-members

Connect to a serf agent and print the active members
Dockerfile
3
star
65

docker-openstack-cli

Dockerfile
2
star
66

sample-go-adapter

Go
2
star
67

chinook

Ruby
2
star
68

panamax-remote-agent-go

Go
2
star
69

ctlc-docker-mysql

Shell
2
star
70

docker-data-container

Dockerfile
2
star
71

clcgo

A Go client for the CenturyLinkCloud API
Go
2
star
72

coreos-kubernetes

Shell
2
star
73

panamax-image-packager

Shell
1
star
74

kube-cluster-deploy

Go
1
star
75

docker-deis-cli

Dockerfile
1
star
76

docker-events

Ruby
1
star
77

socialize-etl

Ruby
1
star
78

ctlc-docker-nginx

Serf enabled nginx docker container
1
star
79

badge-monitor

Ruby
1
star
80

debian-rails

An optimized Debian-based Rails image
Dockerfile
1
star
81

dray-demo

Dray demo images
1
star
82

kwalify

Ruby
1
star
83

docker-aws-cli

Dockerfile
1
star
84

pmxadapter

Go
1
star
85

socialize-api

where the magic happens
Ruby
1
star
86

chimera

Shell
1
star
87

redis

Redis Docker image based on busybox
Dockerfile
1
star
88

socialize-ui

game changer
JavaScript
1
star
89

draycluster

Go
1
star
90

panamax-remote-agent-installer

Shell
1
star
91

testmux

Go
1
star
92

agent-server-deploy

Go
1
star
93

panamax-integration-suite

Ruby
1
star