Search code examples
dockernext.jsjenkins-pipelinecicdharbor

How to Optimize Docker Image Size and Reduce Push Time to Harbor?


I am currently using Harbor as a Docker image configuration management tool in my company. Before running the Next.js Docker image on the Linux server, I push the Docker image to Harbor and then execute it on the server.

However, I am facing a problem where both the size of the node_modules file in the Next.js Docker image and the overall size of the Docker image are large. As a result, it takes a long time to push the image to Harbor, averaging more than 8 minutes.

To reduce the image size, I have tried reducing the running steps and optimizing the project using techniques like using a builder, running 'npm install --production', and removing the '.next/cache' directory. However, these efforts have not significantly resolved the issue.

The team keeps insisting on reducing the image size due to the lengthy build time. However, based on my current knowledge, I believe that this level of optimization is the best I can achieve.

If anyone has encountered a similar problem, I would appreciate any hints or workarounds they can provide.

Thank you.

ps: I don't speak English well, so I translated it to gpt. There may be incorrect grammar.

I want to speed up image builds and reduce time to push to harbor.


Solution

  • These are 5 steps you need to perform:

    1. use minimal base images(read https://snyk.io/blog/choosing-the-best-node-js-docker-image/)
    2. you should use multistagebuilds
    3. use .Dockerignore
    4. Double-check the dependencies
    5. Make image layer as small as possible

    You should definetly try Dive(https://github.com/wagoodman/dive),which serves for exploring a docker image, layer contents, and discovering ways to shrink the size of your Docker/OCI image.

    After checking these steps it does not matter if your are using Harbor or any other image repository.