Docker Tutorial for Beginners

DevOps

YOUR COSMETIC CARE STARTS HERE

Find the Best Cosmetic Hospitals

Trusted โ€ข Curated โ€ข Easy

Looking for the right place for a cosmetic procedure? Explore top cosmetic hospitals in one place and choose with confidence.

โ€œSmall steps lead to big changes โ€” today is a perfect day to begin.โ€

Explore Cosmetic Hospitals Compare hospitals, services & options quickly.

โœ“ Shortlist providers โ€ข โœ“ Review options โ€ข โœ“ Take the next step with confidence

Docker Engine, Containers, Images, and Basic Container Management

1. Introduction

Modern applications often need many supporting components: web servers, app servers, databases, libraries, runtime environments, configuration files, and operating system dependencies.

Traditionally, setting up these environments manually takes time. A developer may spend hours installing packages, fixing dependency issues, configuring services, and making sure everything works correctly.

Docker helps solve this problem.

Docker allows us to package an application and its dependencies into a lightweight, portable unit called a container. Once packaged, the same application can run almost anywhere: on a developer laptop, a test server, a production server, or a cloud environment.


2. What is Docker?

Docker, also known as Docker Engine, is a container management platform.

It is used to create, run, manage, and remove containers.

Docker is:

  • A container management tool
  • Free to use in many cases
  • Written mainly in Go
  • Available in Community and Enterprise editions
  • Originally created by Solomon Hykes

In simple words:

Docker is a platform that helps us run applications inside containers.


3. Why Do We Use Docker?

Docker is popular because it saves time, reduces cost, improves quality, and makes application deployment easier.

3.1 Docker Saves Time

Without Docker, setting up an application environment may take a long time.

Example:

Manual setup:     1 hour
Using scripts:    5 minutes
Using Docker:     A few seconds

With Docker, we can start an application quickly using a pre-built image.

For example:

docker run -d -p 81:80 httpd
Code language: CSS (css)

This single command can download the image, create a container, start the web server, and expose it to the outside world.

3.2 Docker Saves Cost

Docker containers are lightweight compared to virtual machines.

A virtual machine usually needs its own operating system kernel, CPU, RAM, disk, and libraries.

A container shares the host operating system kernel and uses fewer resources.

This means we can run more applications on the same server.

3.3 Docker Improves Quality

Docker helps avoid the common problem:

It works on my machine, but not on your machine.

Because Docker packages the application with its dependencies, the same container can run consistently across different environments.


4. What is a Container?

A container is a lightweight application runtime environment.

An application runtime environment is the place where an application runs.

Examples of application runtime environments include:

Process
Virtual Machine
Container

A container provides an isolated environment for an application.

A container includes:

  • Application files
  • Required libraries
  • Configuration files
  • Root filesystem
  • Process isolation
  • Network isolation
  • Storage isolation

Containers use Linux kernel features such as:

  • Namespaces
  • Control groups, also called cgroups
  • Capabilities
  • Root filesystem

5. Process vs Virtual Machine vs Container

5.1 Process

A process is a running program on an operating system.

Example:

httpd
mysqld
java
nginx

A process is simple, but it does not provide strong isolation.

5.2 Virtual Machine

A virtual machine runs a full operating system.

A VM contains:

Application
Libraries
Root filesystem
Guest OS kernel
Virtual hardware

Virtual machines are powerful, but they are heavy.

They require more CPU, RAM, and disk space.

5.3 Container

A container is lighter than a virtual machine.

A container contains:

Application
Libraries
Root filesystem

But it shares the host machineโ€™s kernel.

That is why containers start faster and use fewer resources.


6. Operating System, Kernel, and Root Filesystem

To understand containers, we need to understand two important operating system parts:

6.1 Kernel

The kernel is the core part of the operating system.

It manages:

  • CPU
  • Memory
  • Disk
  • Network
  • Processes

6.2 Root Filesystem

The root filesystem contains operating system files, tools, commands, libraries, and directories.

Examples:

/bin
/etc
/usr
/var
/home
Code language: JavaScript (javascript)

6.3 Laptop OS vs VM OS vs Container

A normal laptop operating system has:

Kernel + root filesystem

A virtual machine also has:

Guest kernel + root filesystem

A container usually has:

Root filesystem only

The container shares the host machineโ€™s kernel.

That is one reason containers are lightweight.


7. Where Can Docker Be Used?

Docker can be used to run many types of applications.

Examples:

Web server  -> httpd, nginx
App server  -> tomcat
Database    -> mysql, postgres, mongodb
Cache       -> redis
Monitoring  -> prometheus, grafana

For example, instead of manually installing Apache HTTP Server, we can run it using Docker:

docker run -d -p 81:80 httpd
Code language: CSS (css)

8. Docker Architecture

Docker uses a client-server architecture.

The basic flow is:

Human
  |
  v
Docker Client
  |
  | REST API
  v
Docker Server / Docker Daemon
  |
  v
containerd
  |
  v
Linux Kernel

8.1 Docker Client

The Docker client is the command-line tool we use.

Example:

docker ps
docker images
docker run httpd

When we type Docker commands, we are using the Docker client.

8.2 Docker Server / Docker Daemon

The Docker daemon does the actual work.

It manages:

  • Images
  • Containers
  • Networks
  • Volumes
  • Builds

The daemon process is usually called:

dockerd

8.3 containerd

containerd is a container runtime used by Docker.

It helps manage the container lifecycle at a lower level.

8.4 Linux Kernel

Containers depend on kernel features such as namespaces and cgroups.

The kernel provides isolation and resource control.


9. Docker Image and Docker Container

Docker has two very important concepts:

Image
Container

9.1 What is a Docker Image?

A Docker image is a template or package used to create containers.

It contains:

  • Application code
  • Runtime
  • Libraries
  • Dependencies
  • Configuration
  • Filesystem

Example:

docker pull httpd

This downloads the Apache HTTP Server image.

You can compare a Docker image to a virtual machine image, but Docker images are usually smaller and more lightweight.

9.2 What is a Docker Container?

A container is a running instance of an image.

Example:

docker run httpd

This creates and starts a container from the httpd image.

Simple comparison:

Image      -> Template
Container  -> Running instance of that template

10. Installing Docker

Before using Docker, Docker Engine must be installed.

10.1 Install Docker on Linux

On Ubuntu, the basic installation flow is:

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg lsb-release
Code language: JavaScript (javascript)

Create the keyring directory:

sudo mkdir -p /etc/apt/keyrings

Add Dockerโ€™s GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Code language: JavaScript (javascript)

Add the Docker repository:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Code language: PHP (php)

Update package information:

sudo apt-get update
Code language: JavaScript (javascript)

Install Docker:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Code language: JavaScript (javascript)

10.2 Install Docker on Windows

On Windows, install Docker Desktop for Windows.

After installation, open a terminal and verify Docker:

docker version

10.3 Install Docker on Mac

On macOS, install Docker Desktop for Mac.

After installation, open Terminal and run:

docker version

11. Verify Docker Installation

After installing Docker, use these commands:

which docker
which dockerd
which containerd

Check Docker version:

docker version

Check Docker system information:

docker info

Check running containers:

docker ps

Check all containers:

docker ps -a

Check downloaded images:

docker images

12. Docker Hub

Docker Hub is a public registry where Docker images are stored.

From Docker Hub, we can download ready-made images such as:

ubuntu
httpd
nginx
mysql
tomcat
redis
postgres

To download an image, use:

docker pull httpd

To view downloaded images:

docker images

13. Basic Docker Commands

13.1 Pull an Image

docker pull httpd

This downloads the httpd image from Docker Hub.

13.2 List Images

docker images

Example output:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
httpd        latest    xxxxxxxxxxxx   2 weeks ago    145MB

13.3 Create a Container

docker create httpd

This creates a container but does not start it.

13.4 List All Containers

docker ps -a

This shows running and stopped containers.

13.5 Start a Container

docker start <container_id_or_name>
Code language: HTML, XML (xml)

Example:

docker start 8fa0f9d3de93

13.6 Stop a Container

docker stop <container_id_or_name>
Code language: HTML, XML (xml)

Example:

docker stop 8fa0f9d3de93

13.7 Restart a Container

docker restart <container_id_or_name>
Code language: HTML, XML (xml)

13.8 Pause a Container

docker pause <container_id_or_name>
Code language: HTML, XML (xml)

13.9 Unpause a Container

docker unpause <container_id_or_name>
Code language: HTML, XML (xml)

13.10 Kill a Container

docker kill <container_id_or_name>
Code language: HTML, XML (xml)

The kill command forcefully stops a container.

13.11 Remove a Container

docker rm <container_id_or_name>
Code language: HTML, XML (xml)

A container must be stopped before removing it.


14. Docker Container Lifecycle

A container can go through different states.

Created
Running
Paused
Stopped
Killed
Removed

Common lifecycle commands:

docker create httpd
docker start <container>
docker stop <container>
docker restart <container>
docker pause <container>
docker unpause <container>
docker kill <container>
docker rm <container>
Code language: HTML, XML (xml)

15. Understanding docker run

The docker run command is very important.

It performs multiple actions in one command.

docker run httpd

This command does the following:

Pull image if not available
Create container
Start container
Attach terminal to container

So this:

docker run httpd

is like combining:

docker pull httpd
docker create httpd
docker start <container>
docker attach <container>
Code language: HTML, XML (xml)

16. Understanding docker run -d

The -d option means detached mode.

Example:

docker run -d httpd

This command does the following:

Pull image if not available
Create container
Start container
Do not attach terminal to container
Run in background
Code language: PHP (php)

Detached mode is useful for web servers, databases, and background services.


17. Running Ubuntu Container

Run an Ubuntu container in detached interactive mode:

docker run -itd ubuntu

Check running containers:

docker ps

Example:

CONTAINER ID   IMAGE    COMMAND   STATUS
cf4f7c0a4cdb   ubuntu   bash      Up

18. Going Inside a Container

To enter a running container, use:

docker exec -it <container_id_or_name> /bin/bash
Code language: HTML, XML (xml)

Example:

docker exec -it cf4f7c0a4cdb /bin/bash

Now you are inside the container.

You can run Linux commands:

ls
pwd
cat /etc/os-release

To exit from the container:

exit
Code language: PHP (php)

19. Inspecting a Container

To see detailed information about a container, use:

docker inspect <container_id_or_name>
Code language: HTML, XML (xml)

Example:

docker inspect cf4f7c0a4cdb

This shows details such as:

  • Container ID
  • Image
  • Network settings
  • IP address
  • Mounts
  • Environment variables
  • Command
  • Status

20. Accessing a Container from Outside

Containers have internal IP addresses, but directly using container IP is not recommended for normal users.

Example:

docker inspect <container_id>
Code language: HTML, XML (xml)

You may see an IP address such as:

172.17.0.7
Code language: CSS (css)

Then you could test:

curl http://172.17.0.7
Code language: JavaScript (javascript)

However, the better method is port mapping.


21. Docker Port Mapping

A web server inside a container may run on port 80.

But to access it from the host machine, we need to publish the container port to a host port.

Syntax:

docker run -d -p <host_port>:<container_port> <image>
Code language: HTML, XML (xml)

Example:

docker run -d -p 81:80 httpd
Code language: CSS (css)

This means:

Host port 81      -> Container port 80

Now we can access the web server using the host machine on port 81.

Another example:

docker run -d -p 82:80 httpd
Code language: CSS (css)

This starts another Apache container.

Now we have:

Container 1: Host port 81 -> Container port 80
Container 2: Host port 82 -> Container port 80

This is useful because multiple containers can run the same application internally on port 80, while using different host ports externally.


22. Practical Lab: Run Apache HTTP Server Using Docker

Objective

In this lab, we will run an Apache web server using Docker.

Step 1: Pull the Image

docker pull httpd

Step 2: Check Images

docker images

You should see the httpd image.

Step 3: Run Apache Container

docker run -d -p 81:80 httpd
Code language: CSS (css)

Step 4: Check Running Containers

docker ps

Step 5: Test the Web Server

Use curl:

curl http://localhost:81
Code language: JavaScript (javascript)

You should see the default Apache response.

Step 6: Run Another Apache Container

docker run -d -p 82:80 httpd
Code language: CSS (css)

Step 7: Test the Second Container

curl http://localhost:82
Code language: JavaScript (javascript)

Now two Apache containers are running on the same machine.


23. Practical Lab: Container Lifecycle

Step 1: Create a Container

docker create httpd

Step 2: List All Containers

docker ps -a

Step 3: Start the Container

docker start <container_id>
Code language: HTML, XML (xml)

Step 4: Check Running Containers

docker ps

Step 5: Stop the Container

docker stop <container_id>
Code language: HTML, XML (xml)

Step 6: Restart the Container

docker restart <container_id>
Code language: HTML, XML (xml)

Step 7: Pause the Container

docker pause <container_id>
Code language: HTML, XML (xml)

Step 8: Unpause the Container

docker unpause <container_id>
Code language: HTML, XML (xml)

Step 9: Kill the Container

docker kill <container_id>
Code language: HTML, XML (xml)

Step 10: Remove the Container

docker rm <container_id>
Code language: HTML, XML (xml)

24. Practical Lab: Enter an Ubuntu Container

Step 1: Run Ubuntu Container

docker run -itd ubuntu

Step 2: Check Running Containers

docker ps

Step 3: Enter the Container

docker exec -it <container_id> /bin/bash
Code language: HTML, XML (xml)

Step 4: Run Commands Inside the Container

ls
pwd
cat /etc/os-release

Step 5: Exit the Container

exit
Code language: PHP (php)

25. Useful Docker Commands Summary

Docker System Commands

docker version
docker info

Docker Image Commands

docker images
docker pull httpd

Docker Container Commands

docker ps
docker ps -a
docker create httpd
docker start <container>
docker stop <container>
docker restart <container>
docker pause <container>
docker unpause <container>
docker kill <container>
docker rm <container>
Code language: HTML, XML (xml)

Docker Run Commands

docker run httpd
docker run -d httpd
docker run -itd ubuntu
docker run -d -p 81:80 httpd
docker run -d -p 82:80 httpd
Code language: CSS (css)

Docker Exec Command

docker exec -it <container> /bin/bash
Code language: HTML, XML (xml)

Docker Inspect Command

docker inspect <container>
Code language: HTML, XML (xml)

Docker Stats Command

docker stats

26. Developing Your Own Docker Images

So far, we used existing images such as:

httpd
ubuntu

But in real projects, we often need to create our own images.

There are two common ways to create Docker images:

docker commit
docker build

27. Method 1: Creating an Image Using docker commit

The docker commit command creates a new image from an existing container.

This is useful for learning, testing, and quick experiments.

Example

Run an Ubuntu container:

docker run -it ubuntu /bin/bash

Inside the container, install something:

apt-get update
apt-get install -y curl
Code language: JavaScript (javascript)

Exit the container:

exit
Code language: PHP (php)

Find the container ID:

docker ps -a

Create a new image from the container:

docker commit <container_id> myubuntu-curl
Code language: HTML, XML (xml)

Check the new image:

docker images

Run a container from your new image:

docker run -it myubuntu-curl /bin/bash

Now curl should already be available inside the container.

Important Note

docker commit is easy, but it is not the best method for production.

For real projects, we should use a Dockerfile and docker build.


28. Method 2: Creating an Image Using docker build

The docker build command creates an image using a file called Dockerfile.

A Dockerfile contains step-by-step instructions for building an image.

Example Dockerfile

Create a file named:

Dockerfile

Add this content:

FROM ubuntu

RUN apt-get update && apt-get install -y curl

CMD ["/bin/bash"]
Code language: JavaScript (javascript)

Build the image:

docker build -t myubuntu-curl .

Run the image:

docker run -it myubuntu-curl

Why Dockerfile is Better

Dockerfile is better because:

  • It is repeatable
  • It is easy to share
  • It can be stored in Git
  • It documents every step
  • It is suitable for automation and CI/CD pipelines

29. Docker Command Practice Exercise

Try the following commands one by one:

docker version
docker info
docker images
docker pull httpd
docker images
docker create httpd
docker ps -a
docker start <container_id>
docker ps
docker stop <container_id>
docker restart <container_id>
docker pause <container_id>
docker unpause <container_id>
docker kill <container_id>
docker rm <container_id>
docker run -d -p 81:80 httpd
docker run -d -p 82:80 httpd
docker ps
docker stats
docker inspect <container_id>
Code language: HTML, XML (xml)

30. Common Mistakes and Fixes

Mistake 1: Container is not visible in docker ps

Problem:

docker ps

shows only running containers.

Fix:

docker ps -a

Use docker ps -a to show all containers, including stopped containers.

Mistake 2: Port already in use

Problem:

docker run -d -p 81:80 httpd
Code language: CSS (css)

may fail if port 81 is already used.

Fix:

Use another host port:

docker run -d -p 82:80 httpd
Code language: CSS (css)

Mistake 3: Trying to remove a running container

Problem:

docker rm <container_id>
Code language: HTML, XML (xml)

may fail if the container is running.

Fix:

Stop it first:

docker stop <container_id>
docker rm <container_id>
Code language: HTML, XML (xml)

Or force remove:

docker rm -f <container_id>
Code language: HTML, XML (xml)

Mistake 4: Confusing image and container

Remember:

Image      -> Template
Container  -> Running instance

To see images:

docker images

To see containers:

docker ps -a

31. Docker Learning Path in 20 Minutes

Here is a simple learning path.

Minute 1-3: Understand Docker

Learn:

Docker
Image
Container
Docker Engine
Docker Hub

Minute 4-6: Install and Verify Docker

Run:

docker version
docker info

Minute 7-10: Pull and Run an Image

Run:

docker pull httpd
docker run -d -p 81:80 httpd
Code language: CSS (css)

Minute 11-14: Manage Containers

Practice:

docker ps
docker ps -a
docker stop
docker start
docker restart
docker rm

Minute 15-17: Enter a Container

Run:

docker run -itd ubuntu
docker exec -it <container_id> /bin/bash
Code language: HTML, XML (xml)

Minute 18-20: Create Your Own Image

Learn:

docker commit
docker build
Dockerfile

32. Introduction to Kubernetes

Docker is used to create and run containers.

But in production, we may need to run hundreds or thousands of containers.

Managing many containers manually becomes difficult.

This is where Kubernetes comes in.

Kubernetes is a container orchestration platform.

It helps manage:

  • Container deployment
  • Scaling
  • Networking
  • Load balancing
  • Self-healing
  • Rolling updates
  • Service discovery

Simple comparison:

Docker      -> Runs containers
Kubernetes  -> Manages many containers across many servers

Example:

One container       -> Docker is enough
Many containers     -> Kubernetes is useful
Production cluster  -> Kubernetes is commonly used

33. Docker vs Kubernetes

DockerKubernetes
Container platformContainer orchestration platform
Runs containersManages containers
Works on one machineWorks across many machines
Good for development and simple deploymentsGood for production-scale deployments
Uses images and containersUses pods, deployments, services, and clusters

34. Summary

Docker is a powerful platform for running applications inside containers.

In this tutorial, we learned:

  • What Docker is
  • Why Docker is useful
  • What containers are
  • Difference between process, VM, and container
  • Docker architecture
  • Docker images and containers
  • How to install Docker
  • How to pull images
  • How to create, start, stop, restart, pause, unpause, kill, and remove containers
  • How to run containers in foreground and detached mode
  • How to access containers from inside and outside
  • How to use port mapping
  • How to create custom images using docker commit and docker build
  • Basic introduction to Kubernetes

Docker is the first step toward modern container-based application deployment. Once Docker basics are clear, Kubernetes becomes easier to understand.

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x