What is Docker? And How to Use It to Boost Your Productivity

| 6 min read

Introduction

Greetings and welcome back to my humble blog, dear beautiful reader!!
Today’s article covers a very important technology, and I strongly believe every programmer should learn it — trust me, it will make your life a whole lot easier. Without any further introductions, today, we’ll talk about “Docker,” one of the best and most delightful technologies that have made life easier for me. In this article, I’ll give you a simplified summary of Docker, show you how to get started, and at the end, provide a practical example.
Let’s begin in the name of God…


What Does Virtualization Mean?

First and foremost, what does the term “container” mean? Before explaining that, let’s discuss another term — “Virtualization.” What is it, and how is it related to containers?
“Virtualization,” dear reader, refers to running a virtual system on top of your existing system. But how does this work?
To put things simply, you start with a host device — which could be any machine, such as your local computer, a server, etc. It has hardware components like CPU, RAM, and storage. Virtualization technology takes a part of this hardware and separates it into a virtual machine, within which you can run a complete operating system (Windows, Mac, Linux). These virtual machines rely on special programs to manage their lifecycle, such as “VMware,” “VirtualBox,” or “VirtManager.”


What Does Containerization Mean?

Now that we’ve discussed Virtualization, let’s explain the term “Containerization.”
Simply put, with container systems, you have a host computer — just like with virtual systems. Say you want to run a set of processes, but you want them to operate in an environment isolated from the rest of the system. Several methods exist to achieve this:

  1. The chroot command, which creates a new filesystem root for the processes so they live inside it without touching anything outside.
  2. The kernel feature rlimit, which limits the resources these processes can consume.

Using these methods, you can achieve “Containerization,” but it’s complicated and challenging. That’s why specialized programs exist to simplify the process and manage your containers’ lifecycle — and here’s where Docker steps in to save the day 💪.


What is Docker? How to Install and Get Started

So, after all this talk about containers and virtual machines, what exactly is Docker?
Simply put, Docker is a program that manages your containers’ lifecycle — modifying, running, and interacting with them.
Here’s how to get started with Docker:
Go to this link:
https://docs.docker.com/get-started/get-docker/
Choose your platform and follow the installation instructions there.
Once downloaded and installed, run this command with Docker to confirm it’s installed correctly:

docker run hello-world

Note: This command might only work if you have root privileges.

You’ll see several things happen. Don’t worry — I’ll explain it step by step, so stay with me.

  1. First, you’ll see this line:
    Unable to find image ‘hello-world:latest’ locally
    What does this mean? Basically, Docker couldn’t find the “hello-world” image locally, and “latest” is the image’s tag. So, Docker goes to fetch it from its “repository” — where Docker images are stored (somewhat like GitHub).
    You may be wondering, “What is an image?” An image is what runs Docker containers — I’ll explain this concept in more detail later, don’t worry.

  2. Then, you’ll see:
    latest: Pulling from library/hello-world
    Here, Docker is downloading the image from: https://hub.docker.com
    As mentioned, it works like GitHub, but for Docker images. You’ll find plenty of useful and awesome images there (such as ubuntu, archlinux, postgreSQL, and more).


What is a Dockerfile? Why Do I Need It?

First, what’s an image? And how does Docker know how to build images and run containers from them to begin with? If you guessed it’s thanks to the Dockerfile, you’re right! Everything revolves around the Dockerfile.

  • The Dockerfile, in essence, is a settings file for your container, where you define what base image to use, container properties, and the commands your container should execute.

Project Folder Structure

Any directory from which you want to build an image should follow this structure:

DIR
|__ Dockerfile
|__ /project
|__ /project/print-message.sh

Simple Practical Example

Here is a simple example of a Dockerfile:

FROM ubuntu:latest
RUN apt update && apt install -y figlet
COPY print-message.sh /print-message.sh
RUN chmod +x /print-message.sh
CMD ["/print-message.sh"]

Now let’s break down the file line by line:

  1. The first line specifies that you want to use the ubuntu image with the tag “latest” as the container’s base.
  2. The second line tells Docker to run a command on your container — since it operates on ubuntu (per the first line), the command updates package information and installs “figlet,” which the script will use.
  3. The third line instructs Docker to copy the “print-message.sh” script from your local folder into the container.
  4. The fourth line gives the script execution permissions.
  5. The fifth and last line sets the default command to run when the container starts — in this case, your script.

Here’s the script mentioned earlier:

#!/bin/bash

# Define an array of some words
PHRASES=("Hello There!" "I'm Watching YOU" "You're using LINUX!!!")

# Randomly select a word
RANDOM_INDEX=$(( RANDOM % ${#PHRASES[@]} ))
SELECTED_PHRASE=${PHRASES[$RANDOM_INDEX]}

# Print the message with figlet
figlet -w 200 "$SELECTED_PHRASE"

Building the Image

Now let’s use the Dockerfile above to build an image. You can name your image anything you like. I’ll call mine “print-ascii.”

You’re free to set any name or tag you prefer, but by default, Docker assigns the “latest” tag if you don’t specify one.

Keep in mind: an image is essentially the filesystem for your container, which it needs to operate. You can’t rebuild the exact same image. If you want to modify the file, you’ll need to rebuild the image with the same name but a different tag.

To build the image, use:

docker build -t "print-ascii" .

Let’s break down the command:

  1. The “-t” flag specifies the target image name.
  2. The “.” means the current directory.

In short, this command tells Docker to build an image named “print-ascii” from files in the current path.


Running the Image

  • You can run your built image with:
docker run print-ascii

Helpful Additional Commands

  • To display all images on your system:
docker images
  • As mentioned earlier, if you need to change anything in the image, you’ll need to rebuild it with a new tag:
docker build -t "print-ascii:new"

Conclusion

Finally, thank you so much, dear sweet reader, for making it to the end of this (admittedly long) blog post — it means a lot to me. I hope you enjoyed it, and until our next meeting, may you stay well and healthy! Goodbye, and have a wonderful day 🖤.