In docker, the difference with commit and docker file

When I take docker images,

Is there any difference in creating images with the commit command and the build command with the docker file? I said the difference in image size and algorithm.

+5
source share
2 answers

Yes. There will be differences in size.

docker commit basically takes a “snapshot” of the current state of the “running” container and saves it as an image. This basically means that if your “running” container generates log files, updates packages, or makes changes to the file, they will be saved in the image. Each time you run docker commit , you will create a new image.

On the other hand, docker build creates an image by referencing a script (Docker file). Typically, docker build only creates a new image if it detects changes.

The image size for both methods depends on what you did in both scenarios, docker build will give you the consistency dimension, since it is based on what is specified in the Dockerfile . The size of the docker commit depends on the current state of the container. If you removed a bunch of things after starting the container, you will most likely find yourself with a smaller image, vice versa.

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

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

+2
source

The best way to find out is to try (or compare the code on github, but it went through my sleep time). There are slight differences in the resulting image. Basically, some links are missing, which are used to cache previous collections. And in my example, the command associated with the container is different. Honestly, I can’t say why the version of docker commit smaller, but both images are based on the same parent layer and are very similar in size. For ease of maintenance, the Dockerfile will be easier to manage.

version of dockerfile

 $ 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" ] } } ] 

Fixed 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" ] } } ] 
+1
source

Source: https://habr.com/ru/post/1261968/


All Articles