How to Update Your Docker Image Without Starting From Scratch

Hi, it’s me here again! 👋

Ever been in this situation? You have a Docker image that you’ve been using for a while, and suddenly you need to add some new softwares. Building everything from scratch again? Nobody has time for that! 😅

Let me share a neat trick I use to save time.

Docker

Let’s say I have an image called my-image (based on Ubuntu). After using it for a while, I realize I need to install some extra packages. Sure, I could modify the original Dockerfile and rebuild everything, but that would take forever! 🐌

Here’s a faster way to do it:

  1. Create a new Dockerfile (let’s call it Dockerfile.update):

    1
    2
    3
    4
    
    FROM my-image
    USER root
    RUN apt update && apt install -y your-new-package
    # Add more commands as needed
    
  2. Build a new image:

    1
    
    docker build -t my-image-new -f Dockerfile.update .
    
  3. Play the name-swapping game:

    1
    2
    3
    4
    5
    
    # Backup old image
    docker tag my-image my-image-backup
    
    # Make new image the main one
    docker tag my-image-new my-image
    
  4. Test it out:

    1
    
    docker run my-image some-command
    
  5. If everything works, clean up:

    1
    
    docker rmi my-image-backup my-image-new
    

And that’s it! Your image is updated, and your disk space is clean. 🎉

  • Much faster than rebuilding from scratch
  • Safe (you have a backup if something goes wrong)
  • Saves disk space after cleanup

Remember: Always test your updated image before removing the backup! Better safe than sorry! 😉

Happy Dockering! 🐳

Related Content