Reduce the Docker image size (5.24 GB as of now)
Hi,
When doing:
docker pull ghcr.io/gitroomhq/postiz-app
docker images ghcr.io/gitroomhq/postiz-app
It appears it takes more than 5GB on my computer. I think optimizing it could be great š
Thank you,
I am experiencing the same and not only that it's very big, but it also takes a really long time to build.
Especially because the package.json is not copied on its own, but as a part of the
COPY . .
command.
Copying the package.json itself and then the whole source code would at least decrease the build time when rebuilding.
Iād like to work on this issue. Could you please assign it to me ?
Digging into this a little bit, there are two main areas of the application which seem enormous to me.
/app/apps/frontend/.next/cache in the docker image is 1.7 GB in size. I don't have any experience with Next, but googling around suggests that this is only really needed if you are doing some sort of iterative development. It reduces build time between iterations of the code. If you deleted it/excluded it from the image, would the application still run.
The second area is, of course, node_modules at 2.9 GB in my current deployment. There doesn't seem to be one culprit here. Next and @next together weigh in at around 600 MB, but everything after that is just a long list of dependencies.
There has to be some way to get this number down though. At 5 GB I honestly was expecting to crack open the project and find an embedded LLM.
Hey @shreeMahadikGit Are you still up to this?
Hi, I'm using a Mac with a self-hosted version (clean basic installation without any data), in Docker Desktop it's even bigger, more than 7 GB. Can this be optimized somehow?
I think some of this could be fixed by adding a second build stage which copies the compiled application into a final layer that excludes the build dependencies. I'm not familiar enough with NextJS or this project to know where that final build would be, however.
Apologies if these notifications are becoming frustrating, but just trying to share what I'm finding as I'm finding it. If this ultimately results in a pull request from me, then great! If not, maybe someone else can use these notes to tackle the problem.
The frontend is, by far, the largest component of the application after compiling. The other components (backend, cron, and worker are all under 15 MB). This is, I think, in part because of how much caching Next does when building. Adding output: 'standalone' to apps/frontend/next.config.js produces a standalone build of the service with its own server.js file. There is a small amount of copying dependencies around that is documented on the linked page:
cp -r public .next/standalone/apps/frontend && .next/static .next/standalone/aps/frontend/.next
The end result is a standalone deployment of the frontend in .next/standalone/apps/frontend which can be launched by running node .next/standalone/apps/frontend/server.js and only takes up 100 Mb.
Following on from my suggestion in my previous post about adding a second build stage, only this folder from apps/frontend would need to be included in the final docker image, shaving a significant amount off the size of the final image.
I suspect node_modules in the root project directory can also be excluded from the docker image, but I have yet to test.
What is the best way to set up a test branch with some of these changes so people can collaborate?
Hi @munnellg , I can create a branch to collaborate into, but as GitHub restricts write access, I can only allow PRs onto it.
https://github.com/gitroomhq/postiz-app/tree/950-reduce-the-docker-image-size-524-gb-as-of-now
Sounds good. I've also created my own fork of the project here: https://github.com/munnellg/postiz-app
If people would like to collaborate with me directly on my approach to a solution, we can do so there and then merge pull requests from our main branch back into your new branch above. I'll try and take some time tonight and tomorrow morning to look at this.