Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In docker, difference with commit and dockerfile

Tags:

docker

When I make docker images,

Is there any difference in making images by commit command and build command with docker file? I said difference in terms of image sizes and algorithm.

like image 314
Haksung Kim Avatar asked Dec 28 '16 01:12

Haksung Kim


People also ask

Why Dockerfile is better than commit command?

It is also easier to share a Dockerfile with your team, as opposed to sharing a list of commands they need to run. Although docker commit works well for some uses, it is more practical in the long run to use a Dockerfile. A Dockerfile contains a set of instructions for building a new image.

Do you commit the Dockerfile?

Dockerfiles are a tool that is used to create images. The result of running docker build . is an image with a commit so it's not possible to use a Dockerfile with out creating a commit.

What is the difference between Dockerfile and docker image?

A Dockerfile is a recipe for creating Docker images. A Docker image gets built by running a Docker command (which uses that Dockerfile ) A Docker container is a running instance of a Docker image.


2 Answers

Yes. There will be differences in size.

docker commit is basically taking a "snapshot" of the current state of the "running" container and save it as an image. This basically means if your "running" container are generating logs files, update packages, or making file changes, they will be saved into the image. Every-time you run docker commit, you will create a new image.

On the other hand, docker build create the image by referring to a script (Dockerfile). Generally, docker build will only create a new image if it detects changes.

Image size for both methods depends on what did you do in both scenarios, docker build will give you consistence image size since it's built based on what specified in the Dockerfile. And docker commit image size depends on the current state of the container. If you deleted bunch on stuff after a container run, you likely end up with smaller image, vise-versa.

In short, they are similar but serve difference purpose. Then end product of docker build is essentially a commit.

To put it in a different analogy docker build is like git clone, while docker commit is like git commit.

like image 185
Andy Avatar answered Sep 23 '22 13:09

Andy


Best way to find out is to give it a try (or compare the code on github, but it's past my bedtime). There are minor differences in the resulting image. Mainly some references aren't there that are used to cache previous builds. And in my example, the command associated with the container is different. Honestly, I can't say why the docker commit version is smaller, but both images are based on the same parent layer and very similar in size. For maintainability, the Dockerfile is going to be easier to manage.

Dockerfile version

$ cat df.apt-get-git
FROM debian:latest

RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y \
      git \
      vim \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

$ docker build -t test-git:dockerfile -f df.apt-get-git .
Sending build context to Docker daemon 248.3 kB
Step 1 : FROM debian:latest
 ---> 7b0a06c805e8
Step 2 : RUN apt-get update  && DEBIAN_FRONTEND=noninteractive apt-get install -y       git       vim  && apt-get clean  && rm -rf /var/lib/apt/lists/*
 ---> Running in 44588d9cdef4
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
Ign http://deb.debian.org jessie InRelease
....
Updating certificates in /etc/ssl/certs... 174 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
Processing triggers for systemd (215-17+deb8u5) ...
 ---> 01cb5ddcddd1
Removing intermediate container 44588d9cdef4
Successfully built 01cb5ddcddd1

$ docker inspect test-git:dockerfile
[
    {
        "Id": "sha256:01cb5ddcddd101e498ff9e09e4cb4efad85f49a3b87c5792c807ebccedc83977",
        "RepoTags": [
            "test-git:dockerfile"
        ],
        "RepoDigests": [],
        "Parent": "sha256:7b0a06c805e8f23807fb8856621c60851727e85c7bcb751012c813f122734c8d",
        "Comment": "",
        "Created": "2016-12-28T02:55:23.950610688Z",
        "Container": "44588d9cdef49728a012a5a19657ac2b97b6de191ece375607a22043ae993043",
        "ContainerConfig": {
            "Hostname": "397f80c505a4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "apt-get update  \u0026\u0026 DEBIAN_FRONTEND=noninteractive apt-get install -y       git       vim  \u0026\u0026 apt-get clean  \u0026\u0026 rm -rf /var/lib/apt/lists/*"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:7b0a06c805e8f23807fb8856621c60851727e85c7bcb751012c813f122734c8d",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {}
        },
        "DockerVersion": "1.12.3",
        "Author": "",
        "Config": {
            "Hostname": "397f80c505a4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:7b0a06c805e8f23807fb8856621c60851727e85c7bcb751012c813f122734c8d",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {}
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 245152070,
        "VirtualSize": 245152070,
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:f96222d75c5563900bc4dd852179b720a0885de8f7a0619ba0ac76e92542bbc8",
                "sha256:e839e7442c5bbd0a3843822997bcd6830f88fc03417ad6dfd4cc9cb9c6ce0dfa"
            ]
        }
    }
]

Commit version

$ docker run --name test-git-commit debian:latest /bin/sh -c 'apt-get update \
>   &&  DEBIAN_FRONTEND=noninteractive apt-get install -y \
>         git \
>         vim \
>   &&  apt-get clean \
>   && rm -rf /var/lib/apt/lists/*'
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
...
Updating certificates in /etc/ssl/certs... 174 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
Processing triggers for systemd (215-17+deb8u5) ...

$ docker commit test-git-commit test-git:commit
sha256:141c140e3bb0b8a865e58cfd1674f9dac70623c6537f362b15a0ec0a8edbfd0c

$ docker inspect test-git:commit
[
    {
        "Id": "sha256:141c140e3bb0b8a865e58cfd1674f9dac70623c6537f362b15a0ec0a8edbfd0c",
        "RepoTags": [
            "test-git:commit"
        ],
        "RepoDigests": [],
        "Parent": "sha256:7b0a06c805e8f23807fb8856621c60851727e85c7bcb751012c813f122734c8d",
        "Comment": "",
        "Created": "2016-12-28T02:57:50.962700517Z",
        "Container": "5160a31123f3594255a03c42ab72a41ec1fbe72a394923608c8e5654e5d3027b",
        "ContainerConfig": {
            "Hostname": "5160a31123f3",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "apt-get update \\\n  \u0026\u0026  DEBIAN_FRONTEND=noninteractive apt-get install -y \\\n        git \\\n        vim \\\n  \u0026\u0026  apt-get clean \\\n  \u0026\u0026 rm -rf /var/lib/apt/lists/*"
            ],
            "Image": "debian:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "1.12.3",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "apt-get update \\\n  \u0026\u0026  DEBIAN_FRONTEND=noninteractive apt-get install -y \\\n        git \\\n        vim \\\n  \u0026\u0026  apt-get clean \\\n  \u0026\u0026 rm -rf /var/lib/apt/lists/*"
            ],
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 236898630,
        "VirtualSize": 236898630,
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:f96222d75c5563900bc4dd852179b720a0885de8f7a0619ba0ac76e92542bbc8",
                "sha256:c1fd858a68b921981cd15793cbf673a7794d950bb5cc70769ba250df7b7439f9"
            ]
        }
    }
]
like image 31
BMitch Avatar answered Sep 21 '22 13:09

BMitch