The example of the Dockerfile we used above is unoptimized:
$ vi Dockerfile
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install curl
RUN apt-get -y install nginx
RUN groupadd -r mtitek --gid=1001
RUN useradd -r -g mtitek --uid=1001 mtitek
Let's inspect the layers of the ubuntu image (
ubuntu:latest):
$ docker image inspect ubuntu:latest --format '{{json .RootFS.Layers}}' | jq
[
"sha256:8901a649dd5a9284fa6206a08f3ba3b5a12fddbfd2f82c880e68cdb699d98bfb"
]
Let's inspect the layers of the new image (
ubuntu-nginx:latest):
$ docker image inspect ubuntu-nginx:latest --format '{{json .RootFS.Layers}}' | jq
[
"sha256:8901a649dd5a9284fa6206a08f3ba3b5a12fddbfd2f82c880e68cdb699d98bfb",
"sha256:3f8c7084c7bbc943bd6afed212591874b303398bc7e0ea95c43e4033c9fd2d3a",
"sha256:7bf6682df91a9076c66f394cac225db5b878b96de5041c494adc8e4acd5c0f16",
"sha256:798623fc5905c87438699e37cc35fec5bff9bc5bb20d2d8e9752b47cad36ce8d",
"sha256:f48e14b4afc200c8022b3e36d9f29924afaae221aeb1a1e9eb3d996a8587213c",
"sha256:6468f3e1a65da2cab178429ce66b9c057e2a9ec3faeeeb1f199b772beb3321b5"
]
Note that the image '
ubuntu-nginx:latest' inherit all the layers of the parent image '
ubuntu:latest'.
The five new layers are the ones created by executing the build steps in the Dockerfile.
Let's check the size of the layers of the new image (
ubuntu-nginx:latest):
$ docker image history ubuntu-nginx:latest
IMAGE CREATED CREATED BY SIZE COMMENT
aa946f62cb3c 21 minutes ago /bin/sh -c useradd -r -g mtitek --uid=1001 m… 24.6kB
4aebef1f834f 21 minutes ago /bin/sh -c groupadd -r mtitek --gid=1001 24.6kB
6b9093ec8233 21 minutes ago /bin/sh -c apt-get -y install nginx 7.82MB
157c3399ec8a 21 minutes ago /bin/sh -c apt-get -y install curl 10.9MB
6751ea0c7fc3 21 minutes ago /bin/sh -c apt-get -y update 48.9MB
6015f66923d7 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ADD file:ad85a9d7b0a74c214… 87.6MB
<missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.opencontainers.… 0B
<missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.opencontainers.… 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ARG LAUNCHPAD_BUILD_ARCH 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ARG RELEASE 0B
Check the image size:
$ docker image ls ubuntu-nginx:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-nginx latest aa946f62cb3c 3 minutes ago 226MB
Note that deleting temporary files in later layers won't reduce the image size. Let's demonstrate this by updating the Dockerfile above:
$ vi Dockerfile
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install curl
RUN apt-get -y install nginx
RUN groupadd -r mtitek --gid=1001
RUN useradd -r -g mtitek --uid=1001 mtitek
RUN rm -rf /var/lib/apt/lists/*
Let's build the new Dockerfile:
$ DOCKER_BUILDKIT=0 docker build -t ubuntu-nginx:latest .
Sending build context to Docker daemon 10.75kB
Step 1/7 : FROM ubuntu:latest
---> 6015f66923d7
Step 2/7 : RUN apt-get -y update
---> Using cache
---> 6751ea0c7fc3
Step 3/7 : RUN apt-get -y install curl
---> Using cache
---> 157c3399ec8a
Step 4/7 : RUN apt-get -y install nginx
---> Using cache
---> 6b9093ec8233
Step 5/7 : RUN groupadd -r mtitek --gid=1001
---> Using cache
---> 4aebef1f834f
Step 6/7 : RUN useradd -r -g mtitek --uid=1001 mtitek
---> Using cache
---> aa946f62cb3c
Step 7/7 : RUN rm -rf /var/lib/apt/lists/*
---> Using cache
---> 93bccff9a921
Successfully built 93bccff9a921
Successfully tagged ubuntu-nginx:latest
Let's inspect the layers of the new image (
ubuntu-nginx:latest):
$ docker image inspect ubuntu-nginx:latest --format '{{json .RootFS.Layers}}' | jq
[
"sha256:8901a649dd5a9284fa6206a08f3ba3b5a12fddbfd2f82c880e68cdb699d98bfb",
"sha256:3f8c7084c7bbc943bd6afed212591874b303398bc7e0ea95c43e4033c9fd2d3a",
"sha256:7bf6682df91a9076c66f394cac225db5b878b96de5041c494adc8e4acd5c0f16",
"sha256:798623fc5905c87438699e37cc35fec5bff9bc5bb20d2d8e9752b47cad36ce8d",
"sha256:f48e14b4afc200c8022b3e36d9f29924afaae221aeb1a1e9eb3d996a8587213c",
"sha256:6468f3e1a65da2cab178429ce66b9c057e2a9ec3faeeeb1f199b772beb3321b5",
"sha256:deef6270e5c6912ae672d7f30d4e90c5160efcfa0ef43423474535e3bef125a9"
]
You can notice that a new layer (
deef6270e...) was added for the command
rm -rf /var/lib/apt/lists/*
Let's check the size of the layers of the new image (
ubuntu-nginx:latest):
$ docker image history ubuntu-nginx:latest
IMAGE CREATED CREATED BY SIZE COMMENT
93bccff9a921 16 minutes ago /bin/sh -c rm -rf /var/lib/apt/lists/* 20.5kB
aa946f62cb3c 24 minutes ago /bin/sh -c useradd -r -g mtitek --uid=1001 m… 24.6kB
4aebef1f834f 24 minutes ago /bin/sh -c groupadd -r mtitek --gid=1001 24.6kB
6b9093ec8233 24 minutes ago /bin/sh -c apt-get -y install nginx 7.82MB
157c3399ec8a 24 minutes ago /bin/sh -c apt-get -y install curl 10.9MB
6751ea0c7fc3 24 minutes ago /bin/sh -c apt-get -y update 48.9MB
6015f66923d7 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ADD file:ad85a9d7b0a74c214… 87.6MB
<missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.opencontainers.… 0B
<missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.opencontainers.… 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ARG LAUNCHPAD_BUILD_ARCH 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ARG RELEASE 0B
Let's check the size of the new image:
$ docker image ls ubuntu-nginx:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-nginx latest 93bccff9a921 2 minutes ago 226MB
The image size remains the same because the sizes of the filesystem layers are additive.