Drastically reduce the size of your DOCKER images with MULTISTAGE builds
Vložit
- čas přidán 2. 05. 2018
- I'm always surprised how few people know about Docker Multistage builds. I've even see people manually try and replace a Multistage build with two regular docker builds. When you start up your first Kubernetes cluster for the first time you probably realize just how expensive cloud resources can be. So if you can reduce the size of your Docker images and containers by 85% with just 4 lines of code ... you might want to go for it!
Background in Thumbnail image from Dimound via pngtree.com - Věda a technologie
Thanks. I reduced size of my container from 12GB to 6MB. :)
It sounds to me like an entire operating system reduced to 6MB though =)
@, actually I used an image with Haskell compiler GHC and a ton of preloaded libraries.
best way to explain something - hands on and simple. good work.
I was actually looking for a tutorial on how to do CI/CD with docker containers. This video explained everything I didnt find out yet eventhough it was meant for something else. Really great video! Please do more on docker images and building and deployment!
Dude you are a God! Was racking my brain on how to reduce my image size because they were in Giga size! This video is gold!
Excellent insights. Nicely done!
Cool features. Thanks for the video 😊
Direct to the point. Thanks.
Thanks for creating this video.
Great video! Thanks.
Great video mate, I have a question in my mind. If I put three FROM in docker file it's not running from the second FROM line ? The idea is atlast in Third FROM I have to use my base image.
This is awesome, thank you.
Good Vid! in production you should use distroless images though. In staging for debugging a normal distro image is completely fine though !
simple and to the point, thank you :)
Very clear explanation :)
what about the memory footprints if you are utilizing the lower layers somewhere else as well? are we saving by this way also some?
Great!.
Thanks
Great explainer 👍
cool, which editor do you use btw!!
Thank you for this video! :)
very clear !
How did you run the go file in Alpine without golang?
Thanks really clear
What is the best way to do something like this with a container from a Dockerhub?
really awesome!! very helpful!!
Thanks!
Thanks a lot
Nice video
super Docker Image Reduction from 600 MB to 10 MB, Cool features.
I got confused in minute 3:40. It looks like he is using COPY --from=builder /go/src/app/ /app/. Is the image going two containers in one dockerfile? If he is, Is he using the same golang:alpine image in the second base image?
Can you upload basic videos of docker and kubernetes
How can I use the multistage if I am installing python with more than 20 Wheelfiles? I need to copy everything from 1st container which will not reduce the docker image size? Could somebody please help me?
Thanks for the video. I have Django, Apache, React. How do I do this. It's little unclear about the video. First image has go lang libraries. Second one you are starting with alpine and where are go lang libraries. you are copying your app file only.
so the conclusion is we cannot run the image built with base image "scracth"?, or we can actually run is like usual, buat we just can execute linux/OS bash command ?
Can you please tell me how this reduced the size? To my understanding, you created new container with alpine but you also copied the entire previous folder too. What is the difference in old and new container to be precise?
go is a compiled language, so after its compiled you don't need all of its dependencies or debugging tools, justo a place where you can run the app. So, you make a big container for it to compile and test, and a small one to deploy
@@jacobitosuperstar you deserve a golden medal. I was not able to realize this by myself, and I was curious why tf is my image still same size - I was copying already compiled go.
Just to add an example in favor of @jacobitosuperstar answer, if you are using .net or java you will need .net framework or java virtual machine in both containers, you wont be able to reduce on it.
I have to try it tomorrow with my nodejs app)
Coool nice tip. :)
Error: error preparing container a005d60bbe11f0d23810156 for attach: crun: executable file `./webserver` not found in $PATH: No such file or directory: OCI runtime attempted to invoke a command that was not found
Tried the same and received when try to do podman run
=> ERROR [builder 4/4] RUN go build -o webserver .
> [builder 4/4] RUN go build -o webserver .:
#0 0.649 go: go.mod file not found in current directory or any parent directory; see 'go help modules'
------
ERROR: failed to solve: executor failed running [/bin/sh -c go build -o webserver .]: ex
why iam error? can help me to explain this?
But anyway why is he building the application inside the docker container ? What about doing the building outside and push the final build artifacts into a tiny base image?
because he need CI/CD, developers push a new commit with a production tag and he need to compile and deploy....
go: cannot find main module; see 'go help modules'
The command '/bin/sh -c GOOS=linux go build -a -installsuffix cgo -o helloworld .' returned a non-zero code: 1
getting this problem when i do changes in dockerfile FROM golang:alpine
just add "RUN go mod init your-project-name" before "RUN go build -o main ."
Do you really need a workdir or can you just go to town at the root?
It's the best practice to have a working directory
You have copied the application folder but what about the required binaries of golang?
i think it was already built, so he copied the app's binary. idk :)
@@sasg87962 correct, go produces static binary meaning it already has all the required dependencies in the executable, that's why the executable size is big
so after the build you just need the executable which has been compiled so it will run similarly on the compiled architecture, compile for linux and it will run on linux
I am using a RHEL image for a docker container which comes out to ~700MB, mainly for security reasons. What is the advantage of reducing the size of the docker image? Can we get the same security advantage as RHEL compared to Alpine?
Hey I'm replying to a 3 yrs old comment but general rule is,
that as the more target gets simpler and stupider the less attack vector it gets!
Say, one gotta be *really* smart to hack into a `scratch` image, without bash, no open ports etc. lol you get the point
Oh and of course the host OS should be up to date before everything so that the client OS could be.. "KISS"ed (Keep It Simple & Stupid)
can you make the same for Nodejs please?
I'm not really sure what that would look like for a non-compiled language like NodeJS. In Golang the big savings are because you can use a very minimal image in the final step because the compiled binary needs next to nothing other than a minimal OS (like alpine). However, the finished node app still needs at least a NodeJS environment. You can of course remove devDependencies that you don't need at runtime and you can minify etc if you want. But I don't think you'll ever get the same massive saving that you would on a compiled language. Nevertheless the process is exactly the same regardless of the language, so feel free to apply the knowledge from this video to different languages like NodeJS.
Your "build" image is still 700M big. "Webserver" image needs to be rebuilt for every new code changes on the app. They could live on individual Dockerfiles
Interesting point. What would be the best flow of re-building and separate Dockerfiles work given code changes?
the "build" image is discarded right after the alpine image is created? and "build" already takes care of rebuilding, if the code is not changed then cache is used else "build" is re-build and so is alpine image
Great video! Thanks!