Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Docker image sizes by using multistage build #2425

Open
OSMOSA44 opened this issue Dec 24, 2022 · 3 comments
Open

Improve Docker image sizes by using multistage build #2425

OSMOSA44 opened this issue Dec 24, 2022 · 3 comments
Labels
C: Containers Component: Containers (Docker, etc) T: Enhancement V: Apiman 3

Comments

@OSMOSA44
Copy link

Hi,

When you write

ADD $APIMAN_DISTRO /tmp/apiman-distro-vertx.zip

RUN bsdtar xvf /tmp/apiman-distro-vertx.zip --strip-components=1 -C /opt/apiman && \
    rm /tmp/apiman-distro-vertx.zip && \
    chmod +x /opt/apiman/apiman-gateway.sh

The rm /tmp/apiman-distro-vertx.zip is inefficient because ADD and RUN command generate two distinct image layers and a layer cannot modify another layer

To delete the zip file, you have to download, unzip and delete on the same layer

For example (with use of wget)

RUN wget -O /tmp/apiman-distro-vertx.zip $APIMAN_DISTRO && \
    bsdtar xvf /tmp/apiman-distro-vertx.zip --strip-components=1 -C /opt/apiman && \
    rm /tmp/apiman-distro-vertx.zip && \
    chmod +x /opt/apiman/apiman-gateway.sh

You can use this usefull tool dive to inspect the image efficiency

Regards

@msavy
Copy link
Member

msavy commented Dec 24, 2022

Thanks for your comment, @OSMOSA44!

This is something I did deliberately and have decided to keep.

The separate ADD step allows me to substitute the default remote file with a file copied from the Docker build directory (i.e. local file to build system). This is very useful when doing development, testing, and performing manual releases, working with snapshots, etc.

Unfortunately, this technique does not work with the curl approach, as curl can't transparently access the local filesystem in the same way the ADD command can (which has special capabilities).

e.g:

docker build --build-arg APIMAN_DISTRO=apiman-distro-wildfly-3.1.0-SNAPSHOT-overlay.zip .

Potentially, I could use a container compaction technique like docker build --squash or DockerSlim.

@OSMOSA44
Copy link
Author

You can use multi stage Docker build

See this Dockerfile

Testing commands below

docker buildx build --platform=linux/amd64 --build-arg APIMAN_DISTRO=apiman-distro-vertx-3.0.0.Final.zip -t apiman/vertx-gateway:3.0.0.Final.local .

or

docker buildx build --platform=linux/amd64 --build-arg APIMAN_VERSION=3.0.0.Final -t apiman/vertx-gateway:3.0.0.Final.remote .

Nota: To avoid warning and potentially errors, I have added the platform target parameter as i work on MacBook Pro M1

@msavy
Copy link
Member

msavy commented Dec 29, 2022

Thanks, that's a good idea. I'll chat with the team and we'll do some experiments.

@msavy msavy transferred this issue from apiman/apiman-docker Jan 4, 2023
@msavy msavy changed the title Improved Image Size Improve Docker image sizes by using multistage build Jan 4, 2023
@msavy msavy added C: Containers Component: Containers (Docker, etc) T: Enhancement V: Apiman 3 labels Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: Containers Component: Containers (Docker, etc) T: Enhancement V: Apiman 3
Projects
None yet
Development

No branches or pull requests

2 participants