Decrease Next.js Docker Image Size 15x

  Рет қаралды 32,191

Frontend Basics

Frontend Basics

Күн бұрын

Пікірлер: 64
@tsykin
@tsykin 4 ай бұрын
I used tips from your video and actually managed to decrease image size from 2GB to 139MB. Thanks a lot! Unfortunately some dockefile steps that you specified are stale, specifically: - CMD - ENV declaration it's an easy fix and I am still very grateful for provided info 👍
@jitxhere
@jitxhere 4 ай бұрын
If you don't mind can you please share what fix did you implement?
@javohirmirzo
@javohirmirzo 4 ай бұрын
​@heyjitendra do you use latest docker version? It should show warning messages for deprecated syntaxes
@haiffy
@haiffy 3 ай бұрын
Cant believe you said easy fix then left us hanging
@tsykin
@tsykin 3 ай бұрын
@@haiffy Wow, I have only now realised that my comment got that much attention. I can't share a link in the comments (YT is auto-deleting comments with links) I will be sharing this fixed Dockerfile in my upcoming Next.js starter kit free and open-source project, so make sure to subscribe to not miss out on that :)
@haiffy
@haiffy 3 ай бұрын
@@tsykin cool
@alexrecuenco
@alexrecuenco 2 ай бұрын
A few other tips 1. `COPY` instructions in general should have `--link` to speed up cache. and prevent a lot of other invalidation 1.5 Do not copy package.json and package-lock.json, instead use the bind instruction. 2. Remember to set the proper folder from npm as cache with --bind for the `npm ci`. (if you do `docker init` the image recommended by docker has this well set) 3. do `npm ci --only=production` and copy those deps. 4. Write a decent .dockerignore that removes files that are not needed NextJS standalone kind of does those things, but the tips here are for any build process, not just NextJS. And nevertheless, I believe like that you would get a result somewhat similar in size to what next js is achieving with the standalone option (but probably with better cache)
@aderivapodcast
@aderivapodcast 2 ай бұрын
Can you elaborate on 1.5?
@tdkrage
@tdkrage 3 ай бұрын
This video is amazing! Thank you so much, Would be great to see how to do this for a monorepo setup with multiple next apps and some shared resources.
@evmoh_it
@evmoh_it 3 ай бұрын
Thank's for video! Good practice to have one repository for production ready multistage build for Next JS in Docker
@frontendbasics
@frontendbasics 5 ай бұрын
If you liked the video, don't forget to subscribe. More videos will come
@Telmosampaio
@Telmosampaio 4 ай бұрын
Great tips, exactly what I was looking for 😊
@halitiince
@halitiince 3 ай бұрын
Thank you for the video friend❤ IMHO, you are Turkish right? Your edit & concept the best one I have ever seen 🎉
@frontendbasics
@frontendbasics 3 ай бұрын
Yes I am. Glad you liked it ❤️
@codjix
@codjix 3 ай бұрын
hey bro for standalone builds you still need to add .next/static and public folder do it in the builder stage like this after the build command: $ cp -r .next/static .next/standalone/.next $ cp -r public .next/standalone then copy the standalone folder later in the last stage as you do
@mzerone-g6m
@mzerone-g6m 3 ай бұрын
I am sorry but how is the person make this i do image like this just for developement but for production a 2 phase image one to build application and after that copy build and run using node
@zorkname
@zorkname 3 ай бұрын
I think you can make it even smaller with a `node:lts-bullseye-slim` image. It will always use LTS version, and this image feels the best as I've read.
@putrafajarh
@putrafajarh 3 ай бұрын
Does Alpine smaller?
@zorkname
@zorkname 3 ай бұрын
@@putrafajarh bullseye is smaller
@putrafajarh
@putrafajarh 3 ай бұрын
@@zorkname i dont know bro, but in my knowledge, bullseye its a version of Debian right? So it should have more bigger size rather than Alpine
@zorkname
@zorkname 3 ай бұрын
@@putrafajarh yeah, I confused some stuff a bit. I chose "bullseye-slim" some time ago to ensure everything runs well there (and works the same as locally), as alpine has some differences in compilation and utils.
@parthadebnath-n3t
@parthadebnath-n3t 4 ай бұрын
Awsome explanation. Thanks a lot man
@frontendbasics
@frontendbasics 4 ай бұрын
Thanks for the comment ❤️ I am still working on improving my tutorials.
@dzienisz
@dzienisz 3 ай бұрын
How stable this setup is? And why it’s not recommended?
@frontendbasics
@frontendbasics 3 ай бұрын
I think it is stable enough that Next.js recommends it in their documentation. You can check out these two: nextjs.org/docs/app/api-reference/next-config-js/output github.com/vercel/next.js/tree/canary/examples/with-docker
@mikami5799
@mikami5799 3 ай бұрын
Why separate dependency install and build into 2 stages?
@JohnUntivero
@JohnUntivero 3 ай бұрын
By separating the two stages, you can shorten build times. The first stage (install stage) will only be run whenever you add or remove packages from package.json. This means whenever you run docker build, it'll skip the install stage 99% of the time since it is cached, and go straight into building your application. This is great if you have slower internet speeds or have a large amount of dependencies, saving that 1+ minutes it would take to run "npm install"
@hvms6k
@hvms6k 3 ай бұрын
Great video! Thanks for sharing. Can I use this approach to develop nextjs web app locally? I mean are there any benefits to create docker image and run it instead of running npm run dev?
@frontendbasics
@frontendbasics 3 ай бұрын
No need for that. You can use npm run dev for local
@hamadrehman853
@hamadrehman853 2 ай бұрын
Containerization should be the last step because it makes debugging harder
@rezanaipospos3320
@rezanaipospos3320 Ай бұрын
I follow your tips, and my next js prisma decrease image size from 2 GB to 600 MB. Thanks
@abiiranathan
@abiiranathan 3 ай бұрын
What's with the zoom in and out?
@Emre-qp6fk
@Emre-qp6fk 3 ай бұрын
Kardeş, thank you very much, I needed it very much.
@frontendbasics
@frontendbasics 3 ай бұрын
Glad you liked it kanki ❤️
@yusufyalc1n
@yusufyalc1n 3 ай бұрын
@@frontendbasicshuh?
@reze_dev
@reze_dev 3 ай бұрын
Why we use this instead of vercel?
@frontendbasics
@frontendbasics 3 ай бұрын
If you don’t want to use their platform and host it in anywhere else, docker is the way to go
@reze_dev
@reze_dev 3 ай бұрын
@@frontendbasics is there a way to deploy container for free?
@SlickYeet
@SlickYeet 3 ай бұрын
Bro with an image size that small, you can run a web app anywhere. Even self host for no charge… Also most free hosting options would easily handle something like this.
@reze_dev
@reze_dev 3 ай бұрын
@@SlickYeet How? Aws only give 12 month for free tier
@SlickYeet
@SlickYeet 3 ай бұрын
@@reze_dev Oracle has a decent free tier iirc. Google cloud offers a free e2-micro instance which could run a web app. But I recommend getting into self hosting at home, can be complicated at times but you get complete freedom of how you set up your services. Pretty much any old hardware, like an old workstation or even a laptop can suffice to get started with a homelab/self hosted web app. There are plenty of good tutorials and free tools out there to make this easier too.
@J1ndod1nho
@J1ndod1nho 3 ай бұрын
Great tips, thanks a lot man 👍🏻
@furkangonulal1355
@furkangonulal1355 3 ай бұрын
Eyvallah, patronları sevindirecek haber :D
@zakir.nuriiev
@zakir.nuriiev 3 ай бұрын
Thank you for a such nice tip!
@ojfy1267
@ojfy1267 3 ай бұрын
hello what a great video to wathc thanks a lot. I have just one question about the public and static folder how can we upload them to CDN or S3, like whats the configurations that should be included to nextjs to make it work?
@frontendbasics
@frontendbasics 3 ай бұрын
You can check out assetPrefix option: nextjs.org/docs/app/api-reference/next-config-js/assetPrefix Basically create a structure where the prefix is your cdn link + the commit hash so that it syncs fine.
@SharunKumar
@SharunKumar 2 ай бұрын
Huh, standalone is the recommended way if you need to deploy it inside docker though
@RamkrishanYT
@RamkrishanYT 2 ай бұрын
You can't convince me that JavaScript is not a vulnerability created by RAM companies
@itznotabug
@itznotabug 3 ай бұрын
node-slim?
@avwie132
@avwie132 3 ай бұрын
People apparently never read any docs…
@mikemixsosa4264
@mikemixsosa4264 2 ай бұрын
🤣🤣🤣🤣
@fra4897
@fra4897 3 ай бұрын
holy shit! thanks a lot!
@igrb
@igrb 3 ай бұрын
amazing stuff
@frontendbasics
@frontendbasics 3 ай бұрын
Thanks for the comment ❤️
@zenobiusztasak8604
@zenobiusztasak8604 Ай бұрын
How it comes that your image weights only 135MB ? My node_modules + .next weights over 1GB. NVM This standalone guy did the job XD
@danxayota9933
@danxayota9933 3 ай бұрын
please could you make a video on dockerize a ready to run expo app image with all the necessary deps to run
@atovan3236
@atovan3236 3 ай бұрын
this video is helpful
@bobobo1673
@bobobo1673 2 ай бұрын
Amazinh
@princereyes5400
@princereyes5400 3 ай бұрын
how about docker ram usage?
@frontendbasics
@frontendbasics 3 ай бұрын
I haven't search that topic but I think it is highly dependent on what your application consists of and how many requests you are getting per second. Let me know if there are magic tricks for optimizing the ram usage as well.
@hugodsa89
@hugodsa89 3 ай бұрын
I love you
@okanbayulgen
@okanbayulgen 3 ай бұрын
türk müsün
@dantero_
@dantero_ 2 ай бұрын
evet adı onur
@turculaurentiu91
@turculaurentiu91 3 ай бұрын
I am pretty sure that you are supposed to build the project on your pipeline before building the docker container and only copy only the build in the docker image, don't build it inside the container.
3 ways to reduce the size of your docker images
17:20
Raghav Dua
Рет қаралды 23 М.
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
Is Next.js 15 any good? "use cache" API first look
8:16
Beyond Fireship
Рет қаралды 127 М.
Docker Image BEST Practices - From 1.2GB to 10MB
7:15
Better Stack
Рет қаралды 116 М.
JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue
12:35
Dockerize Next.js 14+ App in 2024
4:20
Frontend Basics
Рет қаралды 5 М.
Don't Use Websockets (Until You Try This…)
6:46
Code With Ryan
Рет қаралды 326 М.
The Better Way to Load Images
8:46
Josh tried coding
Рет қаралды 54 М.
How to self host a Next.js application (with Dockerfile overview)
13:13
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН