I tried to add a file via ADD
command and then deleted it. But the size of docker images also shows that it includes that files! If I put *
in .dockerignore
, it will not work with ADD
.
Dockerfile:
from ubuntu:20.04
ADD myfile /tmp
RUN rm /tmp/*
Then I built it by
$ docker build -t testwf .
At the first stage it shows the below:
Sending build context to Docker daemon 34.21MB
The size of myfile
file is around 33MB
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
testwf latest 96543168ab34 16 minutes ago 107MB
ubuntu 20.04 ba6acccedd29 5 weeks ago 72.8MB
Actually, I was supposed to get an image with 72.8MB
the same size with ubuntu
not 107MB
which is roughly equal to 72.8MB
plus 33MB
! In other words, If I didn’t have that file with ADD
command, was there any way to access the file in the container as it was copied to Docker daemon
?
update
As HansKilian mentioned in the comments that file went in one of the layers where the final image is constructed on top of that. Is there any way to get rid of that layer in order to decrease the size of the final image?
$ docker history testwf:latest
IMAGE CREATED CREATED BY SIZE COMMENT
2af2733972ab 4 seconds ago /bin/sh -c rm /tmp/* 0B
40d13da4e0cc 4 seconds ago /bin/sh -c #(nop) ADD file:0ddf694d27b108b4a… 34.2MB
ba6acccedd29 5 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ADD file:5d68d27cc15a80653… 72.8MB
>Solution :
You can have a multistage build:
FROM alpine:latest
ADD myfile /tmp
from ubuntu:20.04
COPY --from=0 /tmp/myfile ./
RUN rm /tmp/*