Skip to main content

Command Palette

Search for a command to run...

🚀 Docker from Zero to Hands-On A beginner-friendly guide to installing Docker, writing your first Dockerfile, and pushing images to Docker Hub

Updated
9 min read

📌 Introduction

If you’ve ever heard phrases like “It works on my machine” or struggled with setting up the same application on different systems, Docker was created to solve exactly this problem.

Docker allows you to package an application along with all its dependencies into a single unit called a container, ensuring that it runs the same way everywhere—on your laptop, a server, or the cloud.

In this blog, we won’t just talk about Docker concepts. We will actually do hands-on work:

By the end of this article, you will be able to:

  • Install Docker on a Linux system

  • Understand Docker architecture (Client, Daemon, Images, Containers)

  • Write your first Dockerfile from scratch

  • Build and run a Docker image

  • Push your image to Docker Hub

  • Understand how Docker daemon works behind the scenes

This blog is written assuming you are a beginner. No prior Docker or DevOps experience is required.


👥 Who Is This Blog For?

This guide is ideal for:

  • Beginners learning Docker for the first time

  • Students exploring DevOps

  • Developers who want a strong Docker foundation

  • Anyone preparing for interviews involving Docker basics

Prerequisites

You only need:

  • A Linux system (Ubuntu preferred)

  • Basic terminal knowledge

  • A GitHub account

  • A Docker Hub account


🐳 What Is Docker?

Docker is a containerization platform that allows you to build, ship, and run applications in isolated environments called containers.

Containers vs Virtual Machines (Simple Explanation)

Virtual MachineDocker Container
Includes full OSShares host OS
Heavy & slowLightweight & fast
High resource usageEfficient resource usage

👉 Containers start in seconds and are much more efficient than VMs.


🧩 Core Docker Components (Very Important)

Before running commands, let’s understand the key components:

  • Docker Client – The docker command you type in the terminal

  • Docker Daemon – The background service that does the real work

  • Docker Image – A blueprint/template for containers

  • Docker Container – A running instance of an image

  • Docker Registry – A place to store images (example: Docker Hub)

We’ll use all of these practically in this blog.


🏗 Docker Architecture (Beginner-Friendly)

https://almablog-media.s3.ap-south-1.amazonaws.com/Docker_Architecture_Diagram_23d43e4865.png

How Docker Works Internally

  1. You type a command like docker run

  2. Docker Client sends the request

  3. Docker Daemon processes it

  4. Images are pulled or built

  5. Containers are created and executed

📌 Key takeaway:
If the Docker daemon is not running, Docker commands will fail.


🔧 Hands-On 1: Installing Docker on Ubuntu (Local Machine or AWS EC2)

Step 1: Update your system

sudo apt update

Step 2: Install Docker

sudo apt install docker.io -y

Step 3: Verify Docker installation

docker --version

Step 4: Start and enable Docker service

sudo systemctl start docker
sudo systemctl enable docker

Step 5: Test Docker

sudo docker run hello-world

If you see a success message, Docker is working correctly 🎉


✅ Expected Successful Output (What to Look For)

You will see something similar to this:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from Docker Hub.
 3. The Docker daemon created a new container from that image.
 4. The Docker daemon ran the executable that produces the output you are currently reading.

📌 This is the success message.


🔎 Common Beginner Issue: Permission Denied

If the Docker daemon is running but Docker commands fail with a “permission denied” error, it usually means the current user does not have access to the Docker socket.

Docker runs as a root-level service and listens on a Unix socket:

/var/run/docker.sock

By default, only the root user and members of the docker group are allowed to communicate with this socket.
That’s why Docker commands may work with sudo but fail without it.

Fix: Grant Docker Access to the User

Add your user to the docker group:

sudo usermod -aG docker $USER

After running this command, log out and log back in (or reconnect to your EC2 instance).
Once the group permissions are refreshed, you should be able to run Docker commands without sudo.


🔎 Important Note for Beginners: Docker Daemon

Even though we did not explicitly start the Docker daemon during installation, Docker works immediately after installation because Ubuntu automatically starts the Docker service.

Docker commands do not execute containers directly.
They always communicate with the Docker daemon, which is responsible for building images, creating containers, managing networks, and handling Docker resources.

If the Docker daemon is not running, Docker commands will fail—even if Docker is installed correctly.


🧪 What Happens If the Docker Daemon Is Not Running?

You can verify this behavior using the following steps.

Step 1: Stop the Docker daemon

sudo systemctl stop docker

Step 2: Run a Docker command

docker ps

You will see an error similar to:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

This demonstrates that the Docker CLI cannot function independently and always depends on the Docker daemon.


▶ How to Start the Docker Daemon

To start the Docker daemon manually:

sudo systemctl start docker

To verify that it is running:

sudo systemctl status docker

Once the daemon is active, Docker commands will work normally again.

If you see a success message, Docker is working correctly 🎉


⚙ Understanding Docker Images and Containers

  • Image → Read-only blueprint

  • Container → Running instance of an image

When you run:

docker run hello-world

Docker does the following:

  1. Checks if the image exists locally

  2. Pulls it from Docker Hub if not present

  3. Creates a container

  4. Runs it

  5. Stops the container after execution


📝 Hands-On 2: Dockerizing a Simple Python Application

Now that we understand what Docker images and containers are, let’s build a real Docker image using a Python application.

This example shows how Docker can package an application along with its runtime.


Step 1: Create a Project Folder

mkdir python-docker-app
cd python-docker-app

Step 2: Create a Python Application

Create a simple Python file:

vim app.py

Add the following code:

print("Hello from Python Docker Container")

Save and exit.


Step 3: Create the Dockerfile

Inside the same directory, create a file named Dockerfile:

vim Dockerfile

Paste the following:

FROM python:3.10-slim

WORKDIR /app

COPY app.py .

CMD ["python", "app.py"]

📌 Important:
The file name must be exactly Dockerfile (no extension).


Dockerfile (Explained Line by Line)

FROM python:3.10-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]

Explanation:

  • FROM → Uses an official Python base image that already contains Python installed.

  • WORKDIR → Sets the working directory inside the container.

  • COPY → Copies the Python application into the image.

  • CMD → Runs the Python program when the container starts.

📌 Here, we explicitly specify the interpreter (python), so no shebang is required.


Step 4: Build the Docker Image

From the project directory, run:

docker build -t my-python-image .

This command:

  • Reads the Dockerfile

  • Pulls the Python base image if needed

  • Creates a Docker image named my-python-image


Step 5: Run the Docker Container

docker run my-python-image

You should see:

Hello from Python Docker Container

🎉 Your Python application is now running inside a Docker container.


📦 Hands-On 3: Push Docker Image to Docker Hub

So far, we have built and run a Docker image locally.
Now let’s take the next logical step: pushing the image to Docker Hub, so it can be pulled and run from any machine.

This is where Docker truly shines — build once, run anywhere.


🐳 What Is Docker Hub?

Docker Hub is a container registry where Docker images are stored and shared.

  • Public images can be pulled by anyone

  • Private images are accessible only to you or your team

  • Docker automatically pulls images from Docker Hub if not found locally

📌 Docker Hub plays the same role for Docker images that GitHub plays for source code.


Step 1: Create a Docker Hub Account

If you don’t already have one:

  1. Go to https://hub.docker.com

  2. Create a free account

  3. Note your Docker Hub username

  4. Verify your email address (important)

📌 Important Note
Docker Hub requires email verification before allowing image pushes.
Even if docker login succeeds, unverified accounts may receive an “access denied” error during docker push.


Step 2: Login to Docker Hub from Terminal

On a local machine with a browser, you can log in using:

docker login

Docker will open a browser-based authentication flow and confirm login securely.

On AWS EC2 or remote servers, where a browser is not available, use:

docker login -u <dockerhub-username>

You will be prompted to enter your Docker Hub password (or access token).

📌 Note:
Browser-based login may not work on remote servers accessed via SSH. In such cases, username-based login is the recommended approach.

After successful authentication, you should see:

Login Succeeded

Docker stores credentials locally so future pushes do not require re-authentication.


Step 3: Tag the Docker Image

Docker Hub follows a strict image naming convention:

<dockerhub-username>/<image-name>:<tag>

Tag your locally built image:

docker tag my-python-image <your-username>/my-python-image:latest

📌 The latest tag is simply a label—it does not imply versioning automatically.


Step 4: Push the Image to Docker Hub

Now push the image:

docker push <your-username>/my-python-image:latest

✅ Expected Successful Output

The push refers to repository [docker.io/<your-username>/my-python-image]
6c55f250879d: Pushed
0f084300c84d: Pushed
d8fe6d8e990b: Pushed
latest: digest: sha256:xxxxxxxx size: xxxx

This confirms that your image has been uploaded successfully.


Step 5: Verify the Image on Docker Hub

  1. Open Docker Hub in your browser

  2. Navigate to Repositories

  3. You should see my-python-image listed under your account


Step 6: Pull and Run the Image from Anywhere

On any machine with Docker installed, run:

docker run <your-username>/my-python-image

Docker will:

  1. Pull the image from Docker Hub

  2. Create a container from the image

  3. Run the Python application

If everything is correct, you will see:

Hello from Python Docker Container

This confirms that your Docker image is portable, reproducible, and environment-independent.


🔁 End-to-End Docker Flow (Big Picture)

Dockerfile
   ↓ docker build
Docker Image (local)
   ↓ docker run
Docker Container
   ↓ docker push
Docker Hub
   ↓ docker pull
Any Machine

What Each Step Represents

  • Dockerfile → Instructions to build the image

  • Image → Packaged application and runtime

  • Container → Running instance of the image

  • Docker Hub → Central registry for sharing images


✅ Conclusion

In this blog, you went from zero Docker knowledge to:

  • Installing Docker on Ubuntu / AWS EC2

  • Understanding Docker architecture and core components

  • Learning the difference between images and containers

  • Writing a Dockerfile

  • Dockerizing a Python application

  • Building and running Docker containers

  • Pushing Docker images to Docker Hub

  • Running containers from anywhere

You now have a solid, practical foundation in Docker, covering both concepts and real-world workflows.

More from this blog

codeops-labs.hashnode.dev

16 posts