DEV Community

Cover image for Why Pull Base Images When You Can Build Your Own?
vaibhavlokhande3000
vaibhavlokhande3000

Posted on

Why Pull Base Images When You Can Build Your Own?

Ever wondered why you’re pulling base images like everyone else when you could just… make your own? Revolutionary idea, right?

No more relying on pre-made images, no more "is this secure?" overthinking - it’s time to roll up your sleeves and DIY your container base. And guess what? It’s easier than you think. Let me take you through the process with this simple, step-by-step guide.

(Spoiler: it’ll feel like cooking your own pizza from scratch instead of ordering one online. Bonus satisfaction guaranteed.)


🛠️ What You Need:

  • Buildah: Your go-to container-building tool.

  • Ubuntu AMI Server: A clean slate to start with.

  • Debootstrap: For creating your Ubuntu filesystem.

  • Container Registry: To store your shiny new creation.


Before diving in, let’s break down Buildah and Debootstrap—the dynamic duo of this process:

🔹 Buildah: A powerful command-line tool for building, modifying, and managing container images, independent of a container runtime. It gives you more flexibility and control.

🔹 Debootstrap: A utility that helps you create a minimal Ubuntu/Debian filesystem, perfect for a lightweight and clean base image.

Together, these tools make building custom container images efficient and streamlined. 🚀


📋 Steps to Create Your Base Image

1. Lets set up our machine first:

For this guide, I’m using an AWS EC2 instance with the Ubuntu AMI.

First, update your machine:

sudo -i
apt update 
Enter fullscreen mode Exit fullscreen mode

2. Installing requirements:
Installing Buildah:

apt install buildah
Enter fullscreen mode Exit fullscreen mode

Installing debootstrap:

 apt install debootstrap
Enter fullscreen mode Exit fullscreen mode

3. Launching an empty container:

Now, let’s spin up a completely empty container

buildah from scratch
Enter fullscreen mode Exit fullscreen mode

Container running successfully:
(This is an empty container)

Image description

cat /etc/os-release #Check version of your ubuntu os 
Enter fullscreen mode Exit fullscreen mode

Mine noble here
Image description


4. Mount the container filesystem
We need to access the container like a regular directory. Use the command below to mount the filesystem and store the mount point in scratchmnt:

scratchmnt=$(buildah mount working-container) 
Enter fullscreen mode Exit fullscreen mode

5. Creating Ubuntu filesystem in the container directory
Here comes the fun part—use Debootstrap to create a minimal Ubuntu ‘Noble’ filesystem inside the mounted container directory:

debootstrap noble $scratchmnt
Enter fullscreen mode Exit fullscreen mode

Once that's done, you’ll see all the OS files magically appear in the container!

Image description


6. Building an image from container:
Now let’s commit your work and package it as a custom image:

 buildah commit working-container vaibhavs_ubuntu_base_image:0.0
Enter fullscreen mode Exit fullscreen mode

Image description

Boom! You’ve successfully built your very own Ubuntu image 🎉—give yourself a high-five, you genius!

Check your images:

buildah images    #list images
Enter fullscreen mode Exit fullscreen mode

Image description


Time to show off your masterpiece—let’s push that image to the repo and make it official! 🚀

Here are some best options to show off:


Pull My Image and Get Started

You can grab my image from here—go ahead, it’s all yours!

docker pull quay.io/vlokhande3000/ubuntu_image
Enter fullscreen mode Exit fullscreen mode

Image description


And there you have it—your very own custom Ubuntu base image!


Benefits of Building Your Own Container Base Image

  • Greater Control
    By building your own base image, you have full control over the environment. You can ensure that only the software and dependencies you need are included, making it more lightweight and secure compared to relying on pre-made images.

  • Enhanced Security
    Custom base images allow you to eliminate unnecessary components and reduce the attack surface. You can ensure the image is up-to-date and free of vulnerabilities by manually installing and configuring only trusted software.

  • Tailored Environment
    You can create a container with exactly the configurations, tools, and libraries you need, optimizing it for your specific use case. This results in a more efficient and purpose-built image compared to a generic one.

  • No Dependency on External Images
    By avoiding pre-built images, you eliminate the need to trust external sources. You can be confident that your image is built exactly the way you want, and there’s no risk of pulling a vulnerable or outdated base image from public repositories.

  • Improved Performance
    Custom base images can be optimized to minimize the size of the container, reducing overhead. This helps improve the performance of your containerized applications, as smaller images take less time to pull and deploy.

  • Flexibility for Future Modifications
    Once you have created your custom base image, you can easily modify it in the future to include additional software, configuration changes, or security updates. It also gives you the flexibility to scale and adapt as your projects evolve.

  • Satisfaction of Creating from Scratch
    Just like cooking your own pizza 🍕, there's something incredibly satisfying about creating a custom container base image from scratch, knowing it’s optimized and built exactly the way you want it.


Thanks!!

Linkedin

Top comments (0)