apt-get install
my dependencies. Therefore, although I envy Adrianβs tiny image, I need support from a wider range of applications, which makes his approach impractical. FROM debian:wheezy RUN mkdir /tmp/foo RUN fallocate -l 1G /tmp/foo/bar
debian:wheezy
as the base image, create the /tmp/foo
, and in it select 1 GB of space for the bar
file. $ docker build -t sample . Sending build context to Docker daemon 2.56 kB Sending build context to Docker daemon Step 0 : FROM debian:wheezy ---> e8d37d9e3476 Step 1 : RUN mkdir /tmp/foo ---> Running in 3d5d8b288cc2 ---> 9876aa270471 Removing intermediate container 3d5d8b288cc2 Step 2 : RUN fallocate -l 1G /tmp/foo/bar ---> Running in 6c797329ee43 ---> 3ebe08b36733 Removing intermediate container 6c797329ee43 Successfully built 3ebe08b36733
docker build
, you can see what exactly Docker does to build our image:FROM
instruction, Docker launches a container based on a debian:wheezy
image (container ID: 3d5d8b288cc2
)mkdir /tmp/foo
command.9876aa270471
) and then deleted6c797329ee43
)fallocate -l 1G /tmp/foo/bar
3ebe08b36733
) and then deleteddocker images --tree
(unfortunately, the --tree
flag --tree
obsolete and is likely to be removed in future releases): $ docker images --tree Warning: '--tree' is deprecated, it will be removed soon. See usage. ββ511136ea3c5a Virtual Size: 0 B Tags: scratch:latest ββ59e359cb35ef Virtual Size: 85.18 MB ββe8d37d9e3476 Virtual Size: 85.18 MB Tags: debian:wheezy ββ9876aa270471 Virtual Size: 85.18 MB ββ3ebe08b36733 Virtual Size: 1.159 GB Tags: sample:latest
debian:wheezy
, followed by the two containers mentioned earlier (one for each instruction in the Dockerfile). docker run -it sample:latest /bin/bash
We can easily run one of the unnamed layers: docker run -it 9876aa270471 /bin/bash
docker history
: $ docker history sample IMAGE CREATED CREATED BY SIZE 3ebe08b36733 3 minutes ago /bin/sh -c fallocate -l 1G /tmp/foo/bar 1.074 GB 9876aa270471 3 minutes ago /bin/sh -c mkdir /tmp/foo 0 B e8d37d9e3476 4 days ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B 59e359cb35ef 4 days ago /bin/sh -c #(nop) ADD file:1e2ba3d9379f 85.18 MB 511136ea3c5a 13 months ago 0 B
sample
image along with the commands that led to their creation and their size (note that the order of the layers in docker history
is the same as the order displayed in docker images --tree
).ADD
instruction (inherited from debian:wheezy
) and our fallocate
command. $ docker save sample > sample.tar $ ls -lh sample.tar -rw-r--r-- 1 core core 1.1G Jul 26 02:35 sample.tar
FROM debian:wheezy RUN mkdir /tmp/foo RUN fallocate -l 1G /tmp/foo/bar RUN rm /tmp/foo/bar
docker build
for the updated Dockerfile and look at the story again, we will see the following: $ docker history sample IMAGE CREATED CREATED BY SIZE 9d9bdb929b00 8 seconds ago /bin/sh -c rm /tmp/foo/bar 0 B 3ebe08b36733 24 minutes ago /bin/sh -c fallocate -l 1G /tmp/foo/bar 1.074 GB 9876aa270471 24 minutes ago /bin/sh -c mkdir /tmp/foo 0 B e8d37d9e3476 4 days ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B 59e359cb35ef 4 days ago /bin/sh -c #(nop) ADD file:1e2ba3d9379f 85.18 MB 511136ea3c5a 13 months ago 0 B
rm
call has added a new layer (to 0 bytes), but everything else remains the same. If we save our updated image, we should see that the size has practically not changed (there will be a slight difference due to the metadata of the added layer): $ docker save sample > sample.tar $ ls -lh sample.tar -rw-r--r-- 1 core core 1.1G Jul 26 02:55 sample.tar
docker run
for this image and looked into the /tmp/foo
, we would find it empty (ultimately, the file was deleted). However, since our Dockerfile generated a layer containing a 1 GB file, it has become an integral part of the image. $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE scratch latest 511136ea3c5a 13 months ago 0 B busybox latest a9eb17255234 7 weeks ago 2.433 MB debian latest e8d37d9e3476 4 days ago 85.18 MB ubuntu latest ba5877dc9bec 4 days ago 192.7 MB centos latest 1a7dc42f78ba 2 weeks ago 236.4 MB fedora latest 88b42ffd1f7c 10 days ago 373.7 MB
ubuntu
as a basis for the team - mostly because most of us already knew it. However, after playing a little with debian
, we concluded that it fully satisfies our needs and at the same time retains 100+ MB of space.debian:wheezy
as a basis: $ docker images --tree Warning: '--tree' is deprecated, it will be removed soon. See usage. ββ511136ea3c5a Virtual Size: 0 B Tags: scratch:latest ββe8d37d9e3476 Virtual Size: 85.18 MB Tags: debian:wheezy ββ22a0de5ea279 Virtual Size: 85.18 MB β ββ057ac524d834 Virtual Size: 85.18 MB β ββbd30825f7522 Virtual Size: 106.2 MB Tags: creeper:latest ββd689af903018 Virtual Size: 85.18 MB β ββbcf6f6a90302 Virtual Size: 85.18 MB β ββffab3863d257 Virtual Size: 95.67 MB Tags: enderman:latest ββ9876aa270471 Virtual Size: 85.18 MB ββ3ebe08b36733 Virtual Size: 1.159 GB ββ9d9bdb929b00 Virtual Size: 1.159 GB Tags: sample:latest
debian:wheezy
, but these are not three copies of Debian. Instead of copying, each image contains a link to an instance of the Debian layer (one of the reasons I like docker images --tree
, is that it clearly demonstrates the connections between different layers).debian:wheezy
, you no longer have to drag these layers again, and every bit of it used in the images will take place only once. FROM debian:wheezy WORKDIR /tmp RUN wget -nv RUN tar -xvf someutility-v1.0.0.tar.gz RUN mv /tmp/someutility-v1.0.0/someutil /usr/bin/someutil RUN rm -rf /tmp/someutility-v1.0.0 RUN rm /tmp/someutility-v1.0.0.tar.gz
$ docker history some utility IMAGE CREATED CREATED BY SIZE 33f4a99 16 seconds ago /bin/sh -c rm /tmp/someutility-v1.0.0.tar.gz 0 B fec7b5e 17 seconds ago /bin/sh -c rm -rf /tmp/someutility-v1.0.0 0 B 0851974 18 seconds ago /bin/sh -c mv /tmp/someutility-v1.0.0/someuti 12.21 MB 5b6b996 19 seconds ago /bin/sh -c tar -xvf someutility-v1.0.0.tar.gz 99.91 MB 0eebad5 20 seconds ago /bin/sh -c wget -nv http://centurylinklabs.com 55.34 MB d6798fc 8 minutes ago /bin/sh -c #(nop) WORKDIR /tmp 0 B e8d37d9 5 days ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B 59e359c 5 days ago /bin/sh -c #(nop) ADD file:1e2ba3d9379f7685a1 85.18 MB 511136e 13 months ago 0 B
wget
results in a 55 MB layer, and unpacking the archive to a 99 MB layer. We do not need these files, which means we just waste 150+ MB in vain. FROM debian:wheezy WORKDIR /tmp RUN wget -nv && \ tar -xvf someutility-v1.0.0.tar.gz && \ mv /tmp/someutility-v1.0.0/someutil /usr/bin/someutil && \ rm -rf /tmp/someutility-v1.0.0 && \ rm /tmp/someutility-v1.0.0.tar.gz
RUN
instruction, we grouped them using the &&
operator. And although the Dockerfile becomes a bit less readable, it allows us to remove the tarball and extracted directory before the layer is committed. $ docker history some utility IMAGE CREATED CREATED BY SIZE 8216b5f 7 seconds ago /bin/sh -c wget -nv http://centurylinklabs.com 12.21 MB d6798fc 17 minutes ago /bin/sh -c #(nop) WORKDIR /tmp 0 B e8d37d9 5 days ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B 59e359c 5 days ago /bin/sh -c #(nop) ADD file:1e2ba3d9379f7685a1 85.18 MB 511136e 13 months ago 0 B
sample
image (the one with fallocate
and rm
) and run it: $ docker run -d sample 7423d238b754e6a2c5294aab7b185f80be2457ee36de22795685b19ff1cf03ec
-d
flag just to display the container ID).docker import
, we can turn it back into an image: $ docker export 7423d238b | docker import - sample:flat 3995a1f00b91efb016250ca6acc31aaf5d621c6adaf84664a66b7a4594f695eb $ docker history sample:flat IMAGE CREATED CREATED BY SIZE 3995a1f00b91 12 seconds ago 85.18 MB
sample:flat
shows only one layer weighing 85 MB, - the layer containing the gigabyte file is missing.sample:flat
image now contains a built-in debian:wheezy
copy.Source: https://habr.com/ru/post/234829/
All Articles