Docker Imageの履歴を見る

docker Imageのサイズが急に増えていたため、調べました。
docker history \<Image名\> で今までのImageに対する変更履歴が確認できます。

# 環境
docker history コマンドはImageがローカルにダウンロード済みである必要があります。
例としてpythonをpullしてみました。

```
$ docker pull python:alpine3.6
alpine3.6: Pulling from library/python
Digest: sha256:766a961bf699491995cc29e20958ef11fd63741ff41dcc70ec34355b39d52971
Status: Image is up to date for python:alpine3.6
```

```
$docker images
REPOSITORY        TAG             IMAGE ID            CREATED             SIZE
python            alpine3.6       37daba746bbe        7 months ago        86.9MB
```

# やってみる

```
$ docker history python:alpine3.6
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
37daba746bbe        7 months ago        /bin/sh -c #(nop)  CMD ["python3"]              0B                  
<missing>           7 months ago        /bin/sh -c set -ex;   apk add --no-cache --v…   6.5MB               
<missing>           7 months ago        /bin/sh -c #(nop)  ENV PYTHON_PIP_VERSION=10…   0B                  
<missing>           7 months ago        /bin/sh -c cd /usr/local/bin  && ln -s idle3…   32B                 
<missing>           7 months ago        /bin/sh -c set -ex  && apk add --no-cache --…   75.8MB              
<missing>           7 months ago        /bin/sh -c #(nop)  ENV PYTHON_VERSION=3.6.6     0B                  
<missing>           8 months ago        /bin/sh -c #(nop)  ENV GPG_KEY=0D96DF4D4110E…   0B                  
<missing>           8 months ago        /bin/sh -c apk add --no-cache ca-certificates   633kB               
<missing>           8 months ago        /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B                  
<missing>           8 months ago        /bin/sh -c #(nop)  ENV PATH=/usr/local/bin:/…   0B                  
<missing>           13 months ago       /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           13 months ago       /bin/sh -c #(nop) ADD file:6edc55fb54ec9fc36…   3.97MB
```

出力順序は上が新しく、下に行くほど古いレイヤーとなっているようです。

CREATED BYが長いと途中で切られます。(最大45文字まで表示されるようです。)
--no-trunc オプションをつけることで省略なしで表示できます。

また、--human オプションをつけると以下のように実際の日付で表示され、サイズはバイトで表示されます。

```
$ docker history python:alpine3.6 --human=false
IMAGE               CREATED AT                  CREATED BY                                      SIZE                COMMENT
37daba746bbe        2018-06-28T07:46:19+09:00   /bin/sh -c #(nop)  CMD ["python3"]              0                   
<missing>           2018-06-28T07:46:19+09:00   /bin/sh -c set -ex;   apk add --no-cache --v…   6500382             
<missing>           2018-06-28T07:46:11+09:00   /bin/sh -c #(nop)  ENV PYTHON_PIP_VERSION=10…   0                   
<missing>           2018-06-28T07:46:11+09:00   /bin/sh -c cd /usr/local/bin  && ln -s idle3…   32                  
<missing>           2018-06-28T07:46:03+09:00   /bin/sh -c set -ex  && apk add --no-cache --…   75802092            
<missing>           2018-06-28T07:44:13+09:00   /bin/sh -c #(nop)  ENV PYTHON_VERSION=3.6.6     0                   
<missing>           2018-06-06T12:02:33+09:00   /bin/sh -c #(nop)  ENV GPG_KEY=0D96DF4D4110E…   0                   
<missing>           2018-06-06T12:02:33+09:00   /bin/sh -c apk add --no-cache ca-certificates   633345              
<missing>           2018-06-06T12:02:32+09:00   /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0                   
<missing>           2018-06-06T09:59:04+09:00   /bin/sh -c #(nop)  ENV PATH=/usr/local/bin:/…   0                   
<missing>           2018-01-10T06:10:38+09:00   /bin/sh -c #(nop)  CMD ["/bin/sh"]              0                   
<missing>           2018-01-10T06:10:38+09:00   /bin/sh -c #(nop) ADD file:6edc55fb54ec9fc36…   3965926
```

# その他

コンテナの情報の確認はdocker inspectコマンドでできます。

```
$ docker inspect python:alpine3.6
[
    {
        "Id": "sha256:37daba746bbefde08a8b45a6a443f371a3ecf8031653ebba160c131c5be95741",
        "RepoTags": [
            "python:alpine3.6"
        ],
        "RepoDigests": [
            "python@sha256:766a961bf699491995cc29e20958ef11fd63741ff41dcc70ec34355b39d52971"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-06-27T22:46:19.330013996Z",
        "Container": "21dd0b514345dd67da6e8e10eb5fb908ff6d2449b88f6e09c25e82816ecd0b4a",
        "ContainerConfig": {
            "Hostname": "21dd0b514345",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D",
                "PYTHON_VERSION=3.6.6",
                "PYTHON_PIP_VERSION=10.0.1"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"python3\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:be4641bf4c0640d1f092b43a8c888ccb7cec044cd5993c609eef4a6e5f2fc5f7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {}
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D",
                "PYTHON_VERSION=3.6.6",
                "PYTHON_PIP_VERSION=10.0.1"
            ],
            "Cmd": [
                "python3"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:be4641bf4c0640d1f092b43a8c888ccb7cec044cd5993c609eef4a6e5f2fc5f7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 86901777,
        "VirtualSize": 86901777,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/58ec508567010fb449aa3343681f9396c1a564769969c0c858be488dab0dc071/diff:/var/lib/docker/overlay2/89ad2c7d6db3ab72e11be51c4a5bbaf3b2a3c24dfa39ca9b52f8a1ff38ab2ef3/diff:/var/lib/docker/overlay2/6f1f4c1ddd0c6ecbc46d062858e3ef6541824959e0bfa56abd1afada72ff4b17/diff:/var/lib/docker/overlay2/7d54e2b9d74fd7eeb57f35c050969ab6d8b7d2554db633d535aee6a1151a3b50/diff",
                "MergedDir": "/var/lib/docker/overlay2/ab16e3746f5c229b7c413000a096c2f801462013916f26d973de687cf6e48e49/merged",
                "UpperDir": "/var/lib/docker/overlay2/ab16e3746f5c229b7c413000a096c2f801462013916f26d973de687cf6e48e49/diff",
                "WorkDir": "/var/lib/docker/overlay2/ab16e3746f5c229b7c413000a096c2f801462013916f26d973de687cf6e48e49/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:9dfa40a0da3b1a8a7c34abc596d81ede2dba4ecd5c0a7211086d6685da1ce6ef",
                "sha256:8492333e0825c8b565ebb2f3c3b9834a23a0bd9d91a13381432208e3036efec2",
                "sha256:e5a9b27d4e38acc19cbc4124f49b36fb3431438bd8a064f69c5f846593eaec49",
                "sha256:14d7c702acff99cecef913a0a8adce8eca29d302b55ba29a9c730a093f4a8ff2",
                "sha256:a7c43324881cdea9ccdcbd6b799878c976f598d18a3d4ac9d88d3092c4ba0c47"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
```

↓環境変数だけ取り出す

```
$ docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' python:alpine3.6
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=C.UTF-8
GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D
PYTHON_VERSION=3.6.6
PYTHON_PIP_VERSION=10.0.1

```