DEV Community

nitishbali24
nitishbali24

Posted on

Gatsby SSR docker image size is too large

Hi all, I am facing issue while deploying Gatsby SSR code to GCP using docker as an image container.
The image size is coming more than 3.0GB which is heavy for handling load and autoscaling in GCP.

Right now using single stage build in Docker
Where building the application first using Gatsby build and then serving it on a port using Gatsby serve command.

Because node modules are also part of the image so size is very high.

Tried nginx multi-stage build as well but that didn't work because it is not able to find the index.html in the root of the build folder.

This is another issue index.html is not getting generated after running Gatsby build in my Gatsby SSR application

Please help me reduce the image size at most priority.

Latest comments (1)

Collapse
 
ajeetraina profile image
Ajeet Singh Raina • Edited

Here are some things you can do to reduce the image size of your Gatsby SSR application:

  • Use a multi-stage build. This will allow you to build the application in a separate image and then copy the static files to a smaller image that will be used to serve the application.
  • Use a smaller base image. There are many different base images available, some of which are much smaller than others. Choose a base image that is appropriate for your needs.
  • Exclude node modules from the image. Node modules are not needed when serving the application, so you can exclude them from the image.
  • Use a caching layer. A caching layer can help to reduce the size of the image by caching the static files.

Here are some additional things you can do to troubleshoot the issue with the index.html file not being generated:

  • Make sure that you are running the gatsby build command in the root directory of your project.
  • Make sure that you have all of the necessary dependencies installed.
  • Try running the gatsby build command in a clean environment.

If you still have further queries, do post it in forums.docker.com
you can also refer to docker.com/blog/9-tips-for-contain...