Docker-images: Use Docker multi-stage build to reduce the final image size

Created on 25 Jul 2018  路  5Comments  路  Source: oracle/docker-images

Since Docker 17.05 it has been possible to use multi stage builds to reduce the size of the final docker image, ref https://docs.docker.com/develop/develop-images/multistage-build/.

The Docker image for Oracle Database 18.3.0-se2, as built by this repository, is 18GB in size. A multi-stage build can reduce this to about half.

database question

Most helpful comment

You can copy the files with predefined owner, so no chown is needed afterwards.
i.e :
COPY --from=build-stage /usr/ /usr
COPY --from=build-stage --chown=oracle:oinstall /home/oracle /home/oracle
COPY --from=build-stage --chown=oracle:oinstall /etc/oratab /etc/oratab
COPY --from=build-stage --chown=oracle:oinstall /opt/oracle /opt/oracle

This reduces the image size (for 12.2.0.1-se2 which I'm using now) to 6.3GB.

The --squash option is experimental and is up for discussion to be removed (although this has been going on since 2017 :) : https://github.com/moby/moby/issues/34565

[edit] Sorry, I see you already are implementing this. Worked great for me.

All 5 comments

Actually, it can't. Multi-stage builds copy all binaries over as root and upon changing the ownership of these files the image will get bloated again.

What does help is the usage of the --squash option, which is still in experimental status since introduced but works just fine. If the image is built with --squash you save about 10GB of the image.

All of this is written up here: https://geraldonit.com/2017/11/13/how-to-create-small-docker-images/

I highly recommend turning on experimental and using --squash. You can just pass the flag on to ./buildDockerImage.sh -s -o --squash

Leaving this open for now until the PR got validated.

You can copy the files with predefined owner, so no chown is needed afterwards.
i.e :
COPY --from=build-stage /usr/ /usr
COPY --from=build-stage --chown=oracle:oinstall /home/oracle /home/oracle
COPY --from=build-stage --chown=oracle:oinstall /etc/oratab /etc/oratab
COPY --from=build-stage --chown=oracle:oinstall /opt/oracle /opt/oracle

This reduces the image size (for 12.2.0.1-se2 which I'm using now) to 6.3GB.

The --squash option is experimental and is up for discussion to be removed (although this has been going on since 2017 :) : https://github.com/moby/moby/issues/34565

[edit] Sorry, I see you already are implementing this. Worked great for me.

PR #979 got submitted.

And PR #979 got merged. @elygre, are you fine with closing this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajeshpasupathy picture rajeshpasupathy  路  5Comments

nfornag picture nfornag  路  3Comments

vafaronaghi picture vafaronaghi  路  5Comments

bfayz picture bfayz  路  5Comments

maheoasic picture maheoasic  路  5Comments