"🚢Mastering Docker: Build and Share Your First Container Image on DockerHub"

How To Build a Docker Container Image and Push It to DockerHub

Welcome, future Docker pro! 🐳
Have you ever wondered how developers share their applications in lightweight, portable containers? Or how you can deploy the exact same environment from your laptop to a server in seconds? The answer lies in Docker images.

In this guide, I’ll walk you through:
Building your own Docker image from a Dockerfile
Pushing it to DockerHub (so you—or anyone—can use it anywhere!)
Modifying Your Dockerfile and updating the image on Dockerhub

By the end, you’ll have your very own container image hosted on DockerHub. Let’s get started!

We’ll begin with a quick understanding of some basic concepts.

What Is a Container?

Think of a container as a tiny, self-contained box that holds everything your app needs to run:

  • OS-level virtualization (but not the whole OS—just the essentials!)

  • Isolated libraries & dependencies (no more "it works on my machine" issues!)

  • Lightweight & fast (uses fewer resources than VMs)

Containers have revolutionized software development by making apps:
🚀 Portable – Run the same way on any machine
Scalable – Spin up multiple instances in seconds
🔒 Consistent – No more environment mismatches

Common Use Cases For Containers

  • Running Microservices – Breaking apps into smaller, manageable pieces

  • CI/CD Pipelines – Automating testing & deployment

  • Ensuring Development/Production Parity – Same environment everywhere.

  • Migrating infrastructure easily

  • Building scalable and elastic architectures

  • Creating self-healing systems

in summary, a container is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software — including the code, runtime, libraries, and dependencies. Containers use OS-level virtualization, making them fast, efficient, and portable.

Docker is one of the most popular platforms for creating and managing containers. It allows developers to package applications and ship them in a consistent format across different environments.


What Is a Docker Image?

A Docker image is a read-only template used to create containers. It contains all the files, libraries, dependencies, and configuration settings needed to run an application. Unlike virtual machines that virtualize the entire hardware, containers virtualize the operating system — which makes them much more lightweight and efficient.

A Docker image is like a blueprint for containers. It’s a read-only template containing:
📦 Your application code
📦 Required libraries & dependencies
📦 Configuration files

Popular base images (starting points) include:

  • ubuntu (minimal Linux OS)

  • nginx (web server)

  • python (for Python apps)

  • redis


What Is DockerHub?

DockerHub is a cloud-based registry where Docker users can store and share container images. Once your image is uploaded to DockerHub, it can be accessed and used from anywhere.

Other examples of container registries include:

  • Azure Container Registry

  • Google Container Registry

  • Amazon ECR

Prerequisites

Before we start, make sure you have the following:

  1. Docker installed on your Linux system
    👉 Install Docker on Ubuntu

  2. A DockerHub account
    👉 Sign up on DockerHub


Step-by-Step: Build and Push a Docker Image

Step 1: Switch to Root & Update Packages

First, let’s ensure we have the latest updates:

Step 2: Create a Project Directory

Organize your work in a dedicated folder:

Step 3: Create a Dockerfile

A Dockerfile is a set of instructions Docker uses to build your image.

Let’s create one:

Press i to enter insert mode, then paste:

dockerfile

Save & exit:

  1. Press ESC

  2. Type :wq (write & quit)

Step 4: Build Your Docker Image

Now, let’s turn this Dockerfile into an actual image:

  • -t mydocker → Tags (names) your image

  • . → Looks for Dockerfile in the current directory

Verify it’s built:

Run: “docker image ls” This lists all the images on your system and if your dockerfile is built, you will see it in the list.

To verify, run: ‘docker run —rm <yourdockerfilename>

Step 5: Create a Repository on DockerHub

Go to DockerHub, log in, and click on "Create Repository".

Name your new repository

  • Add a short description

  • Set visibility to Public

  • Click "Create"

You’ve now created a space where your image will be stored!

Step 6: Push to DockerHub

Step 6.1: Log in to DockerHub

(Enter your password when prompted.)

Step 6.2: Tag Your Image

DockerHub requires images to follow the format:

  • bettym72 → Your repository name

  • testing01 → Version tag (optional but recommended)

Step 5.3: Push to DockerHub

Once completed, go back to your DockerHub repository — you should now see your uploaded image listed

🎉 Success! Your image is now live on DockerHub.

🛠️ Steps to Modify a Dockerfile and Update the Image on DockerHub

To modify a Dockerfile that's already been pushed to Docker Hub and ensure the changes are reflected, follow these steps:

✅ 1. Navigate to the Project Directory

Go to the folder containing your original Dockerfile.

cd ~/path/to/your/project

Or pull the Existing Image

If you don’t have the latest version locally: docker pull myuser/myapp:latest


✏️2. Modify the Dockerfile

Make your desired changes to the Dockerfile (e.g., updating packages, adding new files, changing configurations).

Changes made:

RUN apt update && apt install -y curl : Added curl

CMD ["echo", "Welcome to docker, Here you learn about containerization in a fun way"]: Changed the default command


🏗️ 3. Rebuild the Image With a New Tag

Give it a new version or tag name (to differentiate from the old one).

Rebuild the image with an updated tag (e.g., v2, latest, or a new version number):

📤 4. Push the Updated Image to DockerHub4.

docker push yourusername/reponame:newversion

🔎 5. Verify the Update on DockerHub

  1. Go to hub.docker.com

  2. Visit your repository

  3. You should see your new version/tag (v2) listed with updated metadata

And there you have it!

Pushed it to DockerHub (making it globally accessible)

You’ve just learned how to:

  • Build your own Docker image

  • Create a DockerHub repository

  • Push your image to DockerHub for global access

  • Modify your dockerfile and update the image on Dockerhub

    Now you can:

    • Share your image with teammates

    • Deploy it anywhere (cloud, servers, CI/CD pipelines)

    • Build more complex images (Node.js, Python, databases, etc.)

Next Steps

  • Try pulling the image from another system using docker pull

  • Explore multi-stage builds for smaller images

  • Learn about Docker Compose for multi-container apps

  • Automate builds with GitHub Actions

🎉 Conclusion

  • In conclusion, mastering Docker empowers you to build, share, and deploy applications with ease and consistency. By creating and pushing Docker images to DockerHub, you gain the ability to share your work globally, ensuring that your applications run seamlessly across different environments. This skill is essential in modern DevOps and cloud-native workflows, enabling faster app deployment, automated processes, and consistent development environments.

    As you continue to explore Docker, consider experimenting with multi-stage builds, Docker Compose, and automation tools like GitHub Actions to further enhance your containerization skills.

  • Got questions? Drop them below—I’d love to help! 🚀

    Happy Dockering! 🐋✨


0
Subscribe to my newsletter

Read articles from Funmilola Elizabet Opeyemi Musari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Funmilola Elizabet Opeyemi Musari
Funmilola Elizabet Opeyemi Musari

I am a Food Technologist & Sales Strategist turned DevOps Explorer | Leveraging problem-solving and client-centric skills to build resilient cloud systems. Currently mastering CI/CD pipelines, Azure, and Infrastructure as Code (Terraform) to automate deployments like a well-oiled production line. Passionate about merging operational efficiency with technical innovation—because great systems, like great recipes, require precision and scalability.