0

I am quite new on docker and tried to build a postgres database image with preset database scheme created on. Thus created below dockerfile

#alpine linux version of the postgres image. uses a really small amount of disk
FROM postgres:alpine

#environment database name that postgres will get to create during the startup
ENV POSTGRES_DB=test

#Superuser name that will be created during the container creation
ENV POSTGRES_USER=test

#superuser password to be used to connect to the postgres instance
ENV POSTGRES_PASSWORD=test

#sql file to be run while container getting created
COPY auto-create-scheme.sql /docker-entrypoint-initdb.d/

but once the modified image created. I got below output;

docker build --no-cache -t test .
  Sending build context to Docker daemon  28.67kB
  Step 1/5 : FROM postgres:alpine
   ---> 47510d0ec468
  Step 2/5 : ENV POSTGRES_DB=test
   ---> Running in 7d0246b1cb20
  Removing intermediate container 7d0246b1cb20
   ---> 8be2b9662e86
  Step 3/5 : ENV POSTGRES_USER=test
   ---> Running in 51982d642f99
  Removing intermediate container 51982d642f99
   ---> 954931dc66a8
  Step 4/5 : ENV POSTGRES_PASSWORD=test
   ---> Running in a0ce506ccf78
  Removing intermediate container a0ce506ccf78
   ---> c97298682dac
  Step 5/5 : COPY create-scheme.sql /docker-entrypoint-initdb.d/
   ---> 95df9ff93fcd
  Successfully built 95df9ff93fcd
  Successfully tagged test:latest

following this; if I run docker images -a

I can see those image layers as repo and tag values none.

How can I create my build file to be able discard these image layers have only the final image test.

<none>                           <none>              c97298682dac        5 minutes ago       71.6MB
<none>                           <none>              8be2b9662e86        5 minutes ago       71.6MB
<none>                           <none>              954931dc66a8        5 minutes ago       71.6MB
test                             latest              95df9ff93fcd        5 minutes ago       71.7MB
1

Those images are the docker build cache. You need this to be able to quickly rebuild similar images, and to avoid rebuilding unchanged layers which would result in resending those to remote registry servers and forcing servers to download those unchanged layers again. One important point on these layers:

They do not consume any significant disk space

You can view the layers of an image by running the docker image history command on an image. Here's an example of a rather large image built on my machine:

$ docker image history 25f90c8ba1ca
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
25f90c8ba1ca        11 days ago         /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
8a190467427e        11 days ago         /bin/sh -c #(nop) COPY dir:36bb9e67a0f2844c2…   298MB
1f6f5474071d        11 days ago         /bin/sh -c #(nop) COPY dir:7c364877951bb2312…   249MB
007510a95d17        11 days ago         /bin/sh -c #(nop) COPY dir:9817b6e3d212385d0…   231MB
ee23b7a42488        11 days ago         /bin/sh -c #(nop) COPY dir:57e817f3d9d977f66…   277MB
e190d1bb3f66        11 days ago         /bin/sh -c #(nop) COPY dir:9ba997e629e74ca64…   205MB
abe0ec5c0ef9        11 days ago         /bin/sh -c #(nop) COPY dir:4dbbd5b2010590e28…   213MB
544af937d8a3        11 days ago         /bin/sh -c #(nop) COPY dir:0ac40edba2c7e1317…   178MB
b78085aba431        11 days ago         /bin/sh -c #(nop) COPY dir:2137a075c552d6783…   135MB
3ff8488b067b        11 days ago         /bin/sh -c #(nop) COPY dir:45da2005488e81e9c…   112MB
19b7cdd9d682        11 days ago         /bin/sh -c #(nop) COPY dir:2b745a4311740ddf7…   65.5MB
eb2cf84859fb        11 days ago         /bin/sh -c #(nop) COPY dir:cd7b639f2eee0e9da…   50.1MB
1ba1c21bb0a3        11 days ago         /bin/sh -c #(nop) COPY dir:594739607d1f2d593…   51.9MB
1da5d6bf03e9        11 days ago         /bin/sh -c #(nop) COPY dir:757daf68a395cfaae…   30MB
f6df8b695822        11 days ago         /bin/sh -c #(nop) COPY dir:449b6c2ff93606741…   18.5MB
72895d213dac        11 days ago         /bin/sh -c #(nop) COPY dir:2290eb8d11f15dbe6…   13.8MB
3b6ceadce77b        11 days ago         /bin/sh -c #(nop) COPY file:fcd71a6bfc87e9dd…   167B
52dd0e701be3        11 days ago         /bin/sh -c #(nop)  ENV TARGET=/usr/share/ngi…   0B
aae476eee77d        3 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B
<missing>           3 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0B
<missing>           3 weeks ago         /bin/sh -c #(nop)  EXPOSE 80/tcp                0B
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:1d1ac3b9a14c94a7…   1.09kB
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:af94db45bb7e4b8f…   643B
<missing>           3 weeks ago         /bin/sh -c GPG_KEYS=B0F4253373F8F6F510D42178…   13.3MB
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.15.5     0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B
<missing>           6 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           6 weeks ago         /bin/sh -c #(nop) ADD file:25c10b1d1b41d46a1…   4.41MB

$ docker image inspect --format '{{json .RootFS.Layers}}' 25f90c8ba1ca | jq
.
[
  "sha256:df64d3292fd6194b7865d7326af5255db6d81e9df29f48adde61a918fbd8c332",
  "sha256:39e8483b9882aa8e95fb00c14e1c5dbdf32e836af4aff4bf862608e0a35bc914",
  "sha256:431a5c7929dd5848b9734d84d5cc5be237dd84e9e5ab1cb84ad6975bdc171c0c",
  "sha256:a83dbde6ba05e8112bd2732375ffbd350d9872d9c9e7e088ba5127118cd6d99e",
  "sha256:65ec7af97662fd3f933dc8b0aefb9f2d9d25c0d78530142bcdab1323c6aee223",
  "sha256:25212046c4c3a7589e45b426085da35d7e5e5a7a834180977a43df533672c360",
  "sha256:508c1d799f8da8accdd8b4e92964c73d6f9b8c4e7381fd0e1241e894d01418d3",
  "sha256:a197e87da4cd4587c8965d81878fbb1d898c8ba7d2a05ff0e9862eb700d26053",
  "sha256:f66588501183b7000c7683808ec4653bb83dc3c07f77d67fc34c78e9d9e5cb33",
  "sha256:de2288e4aa944daa6059184813c549d994f5c2407bfae61c7f8b4e0f950e73b1",
  "sha256:77ae40a90a8d41d53751ee800d4008d7a3706ecae6364560b4606ba0167c2674",
  "sha256:1a4bae64e86919b1f40e61a6022e8f910748566b88c2628893c4e424a60c74a2",
  "sha256:ca61f354107fc14de3c01f5bb192ff8b673b6f84c7089a23d754b23a65751310",
  "sha256:d0da652f2c59d6fb6380c0b6940aaa9f5e5a4fc28ae0b82f1acb5ccf9a1884e1",
  "sha256:5f3a91149be048f4f0a4d2450eab6ff8e630bb416b5c508633014e645f57df06",
  "sha256:0c0762559bcfec46462749615ee06b0998e61673e8118cd1a43e20b38e27825a",
  "sha256:581ad1603f59463febb540052046bbdec82bdfc7c75fd4a94578bdca88b91fec",
  "sha256:ff599edf8583ba1dfd7d9c79c1b718f94641e1fb644a0be29ca5b45dcc1768b4",
  "sha256:6ce4a45f89645a2020cfdae51c83470d9f24424ea16f4bddaf1244aa08f2945e",
  "sha256:5fcfba1ba609a8fbfc3eccbc805eb18a78041897ec3f6dc168c9ed871baed950"
]

$ docker image inspect --format '{{json .RootFS.Layers}}' 8a190467427e | jq .
[
  "sha256:df64d3292fd6194b7865d7326af5255db6d81e9df29f48adde61a918fbd8c332",
  "sha256:39e8483b9882aa8e95fb00c14e1c5dbdf32e836af4aff4bf862608e0a35bc914",
  "sha256:431a5c7929dd5848b9734d84d5cc5be237dd84e9e5ab1cb84ad6975bdc171c0c",
  "sha256:a83dbde6ba05e8112bd2732375ffbd350d9872d9c9e7e088ba5127118cd6d99e",
  "sha256:65ec7af97662fd3f933dc8b0aefb9f2d9d25c0d78530142bcdab1323c6aee223",
  "sha256:25212046c4c3a7589e45b426085da35d7e5e5a7a834180977a43df533672c360",
  "sha256:508c1d799f8da8accdd8b4e92964c73d6f9b8c4e7381fd0e1241e894d01418d3",
  "sha256:a197e87da4cd4587c8965d81878fbb1d898c8ba7d2a05ff0e9862eb700d26053",
  "sha256:f66588501183b7000c7683808ec4653bb83dc3c07f77d67fc34c78e9d9e5cb33",
  "sha256:de2288e4aa944daa6059184813c549d994f5c2407bfae61c7f8b4e0f950e73b1",
  "sha256:77ae40a90a8d41d53751ee800d4008d7a3706ecae6364560b4606ba0167c2674",
  "sha256:1a4bae64e86919b1f40e61a6022e8f910748566b88c2628893c4e424a60c74a2",
  "sha256:ca61f354107fc14de3c01f5bb192ff8b673b6f84c7089a23d754b23a65751310",
  "sha256:d0da652f2c59d6fb6380c0b6940aaa9f5e5a4fc28ae0b82f1acb5ccf9a1884e1",
  "sha256:5f3a91149be048f4f0a4d2450eab6ff8e630bb416b5c508633014e645f57df06",
  "sha256:0c0762559bcfec46462749615ee06b0998e61673e8118cd1a43e20b38e27825a",
  "sha256:581ad1603f59463febb540052046bbdec82bdfc7c75fd4a94578bdca88b91fec",
  "sha256:ff599edf8583ba1dfd7d9c79c1b718f94641e1fb644a0be29ca5b45dcc1768b4",
  "sha256:6ce4a45f89645a2020cfdae51c83470d9f24424ea16f4bddaf1244aa08f2945e",
  "sha256:5fcfba1ba609a8fbfc3eccbc805eb18a78041897ec3f6dc168c9ed871baed950"
]

$ docker image inspect --format '{{json .RootFS.Layers}}' 1f6f5474071d | jq .
[
  "sha256:df64d3292fd6194b7865d7326af5255db6d81e9df29f48adde61a918fbd8c332",
  "sha256:39e8483b9882aa8e95fb00c14e1c5dbdf32e836af4aff4bf862608e0a35bc914",
  "sha256:431a5c7929dd5848b9734d84d5cc5be237dd84e9e5ab1cb84ad6975bdc171c0c",
  "sha256:a83dbde6ba05e8112bd2732375ffbd350d9872d9c9e7e088ba5127118cd6d99e",
  "sha256:65ec7af97662fd3f933dc8b0aefb9f2d9d25c0d78530142bcdab1323c6aee223",
  "sha256:25212046c4c3a7589e45b426085da35d7e5e5a7a834180977a43df533672c360",
  "sha256:508c1d799f8da8accdd8b4e92964c73d6f9b8c4e7381fd0e1241e894d01418d3",
  "sha256:a197e87da4cd4587c8965d81878fbb1d898c8ba7d2a05ff0e9862eb700d26053",
  "sha256:f66588501183b7000c7683808ec4653bb83dc3c07f77d67fc34c78e9d9e5cb33",
  "sha256:de2288e4aa944daa6059184813c549d994f5c2407bfae61c7f8b4e0f950e73b1",
  "sha256:77ae40a90a8d41d53751ee800d4008d7a3706ecae6364560b4606ba0167c2674",
  "sha256:1a4bae64e86919b1f40e61a6022e8f910748566b88c2628893c4e424a60c74a2",
  "sha256:ca61f354107fc14de3c01f5bb192ff8b673b6f84c7089a23d754b23a65751310",
  "sha256:d0da652f2c59d6fb6380c0b6940aaa9f5e5a4fc28ae0b82f1acb5ccf9a1884e1",
  "sha256:5f3a91149be048f4f0a4d2450eab6ff8e630bb416b5c508633014e645f57df06",
  "sha256:0c0762559bcfec46462749615ee06b0998e61673e8118cd1a43e20b38e27825a",
  "sha256:581ad1603f59463febb540052046bbdec82bdfc7c75fd4a94578bdca88b91fec",
  "sha256:ff599edf8583ba1dfd7d9c79c1b718f94641e1fb644a0be29ca5b45dcc1768b4",
  "sha256:6ce4a45f89645a2020cfdae51c83470d9f24424ea16f4bddaf1244aa08f2945e"
]

In each of those inspect commands you should see that each new image is just adding on an extra layer until you reach the final image. These layers are the same sha256 checksums, pointing to the same files on the disk. Docker does not copy these files per image, it doesn't even copy them when creating containers (excluding initializing named volumes), the layers are read-only and reused by every descendant image and container via a union filesystem. The disk usage that docker displays is the sum of all of the layers even when other images may share layers, while your actual disk usage is only of the unique layers.

If you rebuild images often on the server with the same build tag, you can prune dangling images with docker image prune. Otherwise, simply avoid displaying these build artifacts by running docker image ls without the -a option.

  • Thanks for the answer; I was at the same point on my head. Questioning "Why do you use -a then ?" Considering your answer; If I want to create another image with only one environment parameter; I simply just from .... by using that imageId ? which in my case; 8be2b9662e86 – Olgun Kaya Oct 25 '18 at 7:25
  • @OlgunKaya the layers will only ever branch apart, never merge. Once you create a new layer that hasn't existed before, the next layer is recreated. So you want to extend images from the final tagged image, from test: latest. With env variables, I try to set them at the end or just before they are used in the Dockerfile. Or don't set them in the image and use a docker-compose.yml to define them dynamically in the container. – BMitch Oct 25 '18 at 9:19
0

You can use the parameter --rm=true in the docker build command. Once the build has been successfully completed, the untagged images will not appear in your host. However, if the build was unsuccessful, they will remain for debugging purposes.

A minor consideration though is that keeping these intermediate images may allow you to build new images faster because not everything would have to be created from scratch.

  • --rm=true is the default, it removes intermediate containers, not the build cache images. You can see that in the build output. – BMitch Oct 25 '18 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.