Using the following Dockerfile:
FROM alpine:3.2
RUN apk update
RUN apk add gcc
RUN apk del --purge gcc
ENTRYPOINT ["sh"]I wanted to know about the final size of the containers. So I did the following:
$ docker build -t sizetest .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM alpine:3.2
---> 31f630c65071
Step 1 : RUN apk update
---> Running in af287be375ac
fetch http://dl-4.alpinelinux.org/alpine/v3.2/main/x86_64/APKINDEX.tar.gz
v3.2.2-57-g329b21b [http://dl-4.alpinelinux.org/alpine/v3.2/main]
OK: 5288 distinct packages available
---> f657af3b124b
Removing intermediate container af287be375ac
Step 2 : RUN apk add gcc
---> Running in 1ccb80265dc4
(1/11) Installing binutils-libs (2.25-r3)
(2/11) Installing binutils (2.25-r3)
(3/11) Installing libgomp (4.9.2-r5)
(4/11) Installing pkgconf (0.9.11-r0)
(5/11) Installing pkgconfig (0.25-r1)
(6/11) Installing libgcc (4.9.2-r5)
(7/11) Installing gmp (6.0.0a-r0)
(8/11) Installing mpfr3 (3.1.2-r0)
(9/11) Installing mpc1 (1.0.1-r0)
(10/11) Installing libstdc++ (4.9.2-r5)
(11/11) Installing gcc (4.9.2-r5)
Executing busybox-1.23.2-r0.trigger
OK: 67 MiB in 26 packages
---> cd95beacd1c0
Removing intermediate container 1ccb80265dc4
Step 3 : RUN apk del --purge gcc
---> Running in 3cb1840167ee
(1/11) Purging gcc (4.9.2-r5)
(2/11) Purging binutils (2.25-r3)
(3/11) Purging libgomp (4.9.2-r5)
(4/11) Purging pkgconfig (0.25-r1)
(5/11) Purging pkgconf (0.9.11-r0)
(6/11) Purging binutils-libs (2.25-r3)
(7/11) Purging libstdc++ (4.9.2-r5)
(8/11) Purging libgcc (4.9.2-r5)
(9/11) Purging mpc1 (1.0.1-r0)
(10/11) Purging mpfr3 (3.1.2-r0)
(11/11) Purging gmp (6.0.0a-r0)
Executing busybox-1.23.2-r0.trigger
OK: 6 MiB in 15 packages
---> 6b131d5d849c
Removing intermediate container 3cb1840167ee
Step 4 : ENTRYPOINT sh
---> Running in 63dadf375f44
---> 585797d958d4
Removing intermediate container 63dadf375f44
Successfully built 585797d958d4
After that, I went and check the size:
$ docker images sizetest
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
sizetest latest 585797d958d4 About a minute ago 70.98 MB
That's odd. The base [alpine:3.2][alpine-32] image is much smaller, and the final size should be close to that:
$ docker images alpine
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
alpine 3.2 31f630c65071 7 weeks ago 5.254 MB
alpine latest 31f630c65071 7 weeks ago 5.254 MB
After all, I'm just installing, removing and purging the same package. So I went and checked the history:
$ docker history sizetest
IMAGE CREATED CREATED BY SIZE COMMENT
585797d958d4 3 minutes ago /bin/sh -c #(nop) ENTRYPOINT &{["sh"]} 0 B
6b131d5d849c 3 minutes ago /bin/sh -c apk del --purge gcc 33.62 kB
cd95beacd1c0 3 minutes ago /bin/sh -c apk add gcc 65.01 MB
f657af3b124b 4 minutes ago /bin/sh -c apk update 686.9 kB
31f630c65071 7 weeks ago /bin/sh -c #(nop) ADD file:98d5decf83ee59e1dd 5.254 MB
Interesting. Let's try removing the intermediate images:
$ docker build --no-cache --force-rm --rm -t sizetest2 .
Sending build context to Docker daemon 2.56 kB
Sending build context to Docker daemon
Step 0 : FROM alpine:3.2
---> 31f630c65071
Step 1 : RUN apk update
---> Running in 9739479cb26c
fetch http://dl-4.alpinelinux.org/alpine/v3.2/main/x86_64/APKINDEX.tar.gz
v3.2.2-57-g329b21b [http://dl-4.alpinelinux.org/alpine/v3.2/main]
OK: 5288 distinct packages available
---> 7150d65a404c
Removing intermediate container 9739479cb26c
Step 2 : RUN apk add gcc
---> Running in d1f363df1740
(1/11) Installing binutils-libs (2.25-r3)
(2/11) Installing binutils (2.25-r3)
(3/11) Installing libgomp (4.9.2-r5)
(4/11) Installing pkgconf (0.9.11-r0)
(5/11) Installing pkgconfig (0.25-r1)
(6/11) Installing libgcc (4.9.2-r5)
(7/11) Installing gmp (6.0.0a-r0)
(8/11) Installing mpfr3 (3.1.2-r0)
(9/11) Installing mpc1 (1.0.1-r0)
(10/11) Installing libstdc++ (4.9.2-r5)
(11/11) Installing gcc (4.9.2-r5)
Executing busybox-1.23.2-r0.trigger
OK: 67 MiB in 26 packages
---> bed49b5219f5
Removing intermediate container d1f363df1740
Step 3 : RUN apk del --purge gcc
---> Running in e2ca2d5f1364
(1/11) Purging gcc (4.9.2-r5)
(2/11) Purging binutils (2.25-r3)
(3/11) Purging libgomp (4.9.2-r5)
(4/11) Purging pkgconfig (0.25-r1)
(5/11) Purging pkgconf (0.9.11-r0)
(6/11) Purging binutils-libs (2.25-r3)
(7/11) Purging libstdc++ (4.9.2-r5)
(8/11) Purging libgcc (4.9.2-r5)
(9/11) Purging mpc1 (1.0.1-r0)
(10/11) Purging mpfr3 (3.1.2-r0)
(11/11) Purging gmp (6.0.0a-r0)
Executing busybox-1.23.2-r0.trigger
OK: 6 MiB in 15 packages
---> 05ae9bd9dd86
Removing intermediate container e2ca2d5f1364
Step 4 : ENTRYPOINT sh
---> Running in a9b16fdfefd5
---> bc18e05eb80d
Removing intermediate container a9b16fdfefd5
Successfully built bc18e05eb80d
But the results were the same:
$ docker images sizetest2
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
sizetest2 latest bc18e05eb80d 18 seconds ago 70.98 MB
What can I do, then? Well, there's this little neat trick: if you export your container and import it again, it will only have one layer, but with the final size:
$ docker run --detach --name runsizetest sizetest
067e73132be59e6ec939a936f5e55bf458723eeb1d6845153057c5d5313096ab
$ docker export runsizetest | docker import - sizetest3
ccc68578724661e07963b27b0d9048058964243f9bf1eefeb30182ce4dc9a4b2
$ docker images sizetest3
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
sizetest3 latest ccc685787246 10 seconds ago 5.941 MB
Voilá! Now we're talking. Sadly, with this trick you'll loose your history:
$ docker history sizetest3
IMAGE CREATED CREATED BY SIZE COMMENT
ccc685787246 44 seconds ago 5.941 MB Imported from -
But if you use this for creating your artifacts for deployment, I think it's a neat trick to try.