How to make smaller Docker containers (including multi-stage build demo!)

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ม.ค. 2025

ความคิดเห็น • 41

  • @BigWobblyElephant
    @BigWobblyElephant 4 ปีที่แล้ว +7

    Simple video, explaining the benefits of multi stage docker files. Great job

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว

      Thanks Wesley, I'm glad it was helpful!
      In my next video I'm going to share some tips for increasing container security, and multi-stage dockerfiles are useful there as well! (they make it possible to use a distroles image to reduce the attack surface area! github.com/GoogleContainerTools/distroless)

  • @DevOpsDirective
    @DevOpsDirective  4 ปีที่แล้ว +1

    Also, hello to all of the HN folks who found my channel through this post! news.ycombinator.com/item?id=23952024
    Welcome to the channel! 👋👋👋

  • @rextechworld
    @rextechworld 2 ปีที่แล้ว +1

    Hey bro, will u please debug this
    bitbucket alpine image failed to copy files: failed to copy directory: Error processing tar file(exit status 1): Container ID 166536 cannot be mapped to a host ID

  • @datamike7457
    @datamike7457 4 ปีที่แล้ว +2

    We tend to use kitchen sink style docker containers that are large, take a long time to build, and are hard to move around. Good tips, I'm inspired to be more selective of our images!

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว

      There is certainly a balance to strike.
      For a data science I see where it can be nice to have lots of libraries/packages pre-installed. This is kind of the approach that jupyter has taken with their docker stacks images:
      For example the jupyter/datascience-notebook (jupyter-docker-stacks.readthedocs.io/en/latest/using/selecting.html#jupyter-datascience-notebook) is nearly 4GB!
      It all comes down to the use case. If you hardly ever need to rebuild/push/pull (e.g. add a new library or bump a version #) then its not too bad to have large images.
      As always... "It depends" 😁

  • @KieCodes
    @KieCodes 4 ปีที่แล้ว +1

    Amazing video. Never tried using multi stage docker builds! I need to try this tomorrow. Thanks!

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว +2

      Oh man -- multi-stage dockerfiles are a game changer! 👌
      If you hit any snags, post them here -- happy to help debug!

  • @dalmirogranas9990
    @dalmirogranas9990 3 ปีที่แล้ว

    Great now o want to go back to all my previous projects to implement these techniques.
    Thanks for the tips!

  • @friedrichwilhelmhufnagel3577
    @friedrichwilhelmhufnagel3577 2 ปีที่แล้ว +1

    Could you please explain a bit more what youre actually doing in the multi stage approach and what step it is that reduces the size? Also, what editor are you using? Thanks

  • @abura3d2012
    @abura3d2012 ปีที่แล้ว

    can we use multi-stage with ruby on rail app ?

  • @benshapiro9731
    @benshapiro9731 ปีที่แล้ว

    Wuuuut??? I just learned about this hold up, how did u reduce the size like that???

  • @bobylikescars
    @bobylikescars 4 ปีที่แล้ว +2

    We can also use from scratch to make it even smaller, but we have to install the compilers as well

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว +3

      Yeah! For a language that can be compiled into static binary, using a multi-stage dockerfile where the first stage has all of the build dependencies and then the second stage starts from SCRATCH and simply copies the build binary and runs it is a great way to go for the ultimate minimal image!

  • @sahil9471
    @sahil9471 3 ปีที่แล้ว

    I am building docker image for Shcrodinger Suite, which has lot of dependent tar file installations.
    I have used multistage build but still need to optimize size more....
    How can I compress the dependencies which are being installed?

    • @DevOpsDirective
      @DevOpsDirective  3 ปีที่แล้ว

      Do you have a link to a repo and/or Dockerfile you can share? Also, TH-cam comments are clunky for technical discussions, if you want to hop into the channel discord it would probably make for a more productive discussion! discord.gg/ExrENts5CT

  • @venkataramanareddy7735
    @venkataramanareddy7735 3 ปีที่แล้ว

    Thank you very much its short and sweety. why couldn't we use alpine image in the first FROM itself, here we are copying /app to alpine don't we require the binaries at run time.

    • @DevOpsDirective
      @DevOpsDirective  3 ปีที่แล้ว +2

      I think you are talking about the Golang example. We needed to use the golang:1.14-alpine image in the first stage because it contains additional dependencies required to build the application.
      We can then use the smaller alpine:3.12 image in the final stage because we dont need them at runtime!
      Does that answer the question?

  • @madaarimultitasks5945
    @madaarimultitasks5945 3 ปีที่แล้ว

    It was one of my interview question and I messed up already. Hope I would be able to answer it in my next if asked. Haha

  • @thebluechimera
    @thebluechimera 4 ปีที่แล้ว +1

    Awesome video!
    I don't know a whole lot about writing Docker files, but why would someone ever not want to start with the slim base image in the first place?

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว +4

      Great question!
      In general, it is just a matter of convenience. The full image will often have utilities installed that are helpful when building/configuring your application that the slim container doesn't
      It will vary from one image to the next, but a few examples for the node base images are curl, wget, and git which are pre-installed in the full image but not in the slim image. If you needed to use them in the slim image you would need to first `apt update` then `apt install` them in the Dockerfile.
      --
      This is why multi-stage builds are so great!
      You get all the convenience when writing your Dockerfile along with the smaller size, improved workflows (see my previous video for tips on using multi-stage images for debugging), and added security (my next video will include info about this 🙂)!
      Sorry for the super long answer -- hopefully that makes sense!

    • @thebluechimera
      @thebluechimera 4 ปีที่แล้ว

      @@DevOpsDirective It does indeed make sense! Thank you :)

  • @EagerEggplant
    @EagerEggplant 3 ปีที่แล้ว

    I wish you applied the multi stage for the node too

  • @dirkneuhauser8213
    @dirkneuhauser8213 3 ปีที่แล้ว

    Great tutorial!

  • @BenjaminWheeler0510
    @BenjaminWheeler0510 4 ปีที่แล้ว +2

    That's great! Unfortunately I'm using Python so I am not able to use a multistage build (as python isn't compiled)

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว +1

      True that python is interpreted, but there are often cases where it is much easier to configure things in a full-sized image (with a bunch of utilities preinstalled) than working with a lightweight image from the start. For example, if you need to clone a git repo or download a binary it can be nice to have git and curl preinstalled 😁
      Also, if your code is leveraging components written in other languages (wiki.python.org/moin/IntegratingPythonWithOtherLanguages)... then you might have a build stage for the C++ (or other) code and copy the built executables into your final python-based stage!

  • @Adarsh8750
    @Adarsh8750 3 ปีที่แล้ว

    Thanks! Simple

  • @NullLabs
    @NullLabs 4 ปีที่แล้ว

    Nice! Awesome video!

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว

      Thanks Marek -- Trying out a new style where I recorded the audio first and then did a screen record to match it!

    • @NullLabs
      @NullLabs 4 ปีที่แล้ว

      @@DevOpsDirective Well done. I always do it live with my talking because matching the speed I find hard really hard! you did it quite well! It flowed well. Also good content :)

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว

      My trick is playing it back extra slow when doing the screen recording. It's annoying to do, but makes the edit much easier!

  • @JT-iw2cw
    @JT-iw2cw 3 ปีที่แล้ว

    Great stuff, thank you.

    • @DevOpsDirective
      @DevOpsDirective  3 ปีที่แล้ว

      Thanks @JT -- I'm actually working on a follow up to this one in which I build a fully functional web server container image in just 6.3 kB. Stay tuned! 😁

  • @Photograaf11
    @Photograaf11 4 ปีที่แล้ว

    Thank you so much!!! i reduced my docker image from 900+MB to only 11MB for my discord bot!!!
    EDIT: after cleaning the cache it didnt work anymore so i updates some things and now the size is 67MB instead of 11MB

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว +1

      Woohoo! Pinning this comment 😀
      What language are you using/which techniques did you use to shrink the image?

    • @Photograaf11
      @Photograaf11 4 ปีที่แล้ว

      @@DevOpsDirective i used nodejs:12 but now im using alphine with npm+nodejs12, after a lot of crashes
      (i didnt cleaned the cache, so it looked it worked with 11mb but it didnt after an hour when all caches are cleaned)
      its now 67MB but thats still 7.25% of that it was before and for me still worth it!

    • @DevOpsDirective
      @DevOpsDirective  4 ปีที่แล้ว

      Yeah, from 900 --> 70MB is still pretty good!