Docker Beginner Tutorial with Commands and Examples

1. What is Docker?

Docker is a container management platform.

In simple words, Docker helps us create, start, stop, restart, pause, unpause, kill, and remove containers.

Just like VMware or AWS helps us manage virtual machines, Docker helps us manage containers.

VMware / AWS  → manage VMs
Docker        → manage containers

Docker is mainly used to run applications in a lightweight and fast way.


2. Why Do We Use Docker?

Docker helps us in three major ways:

1. Save cost

A container uses fewer resources than a virtual machine.

A VM normally needs:

Kernel + Operating System + Root Filesystem + Application

A Docker container uses:

Kernel from host + Root filesystem + Application

So containers consume less:

CPU
RAM
Disk
Operating system overhead

2. Save time

A virtual machine can take several minutes to start.

VM start time        → around 5 minutes
Container start time → around 1 second

Containers are much faster because they do not boot a full operating system.


3. Improve quality of work

Docker makes application environments consistent.

For example, if Jenkins runs inside a Docker container, the same Jenkins container can run on:

Developer laptop
Testing server
Production server
Cloud server

This reduces environment-related issues.


3. What is a Container?

A container is a lightweight application runtime environment.

Example:

Jenkins application
        ↓
JRE + Jenkins files
        ↓
Root filesystem
        ↓
Linux kernel from host

Container structure:

Host Kernel
    ↓
RootFS
    ↓
AppFS: JRE + Jenkins
    ↓
Running Container

The main goal of a container is:

Run applications with less CPU, RAM, disk, and OS cost.

4. Docker vs Virtual Machine

FeatureVirtual MachineDocker Container
Boot timeSlowVery fast
OS requiredFull OS requiredUses host kernel
Resource usageHighLow
SizeLargeSmall
Use caseFull machine virtualizationApplication runtime

Example:

VM Image:
Kernel + RootFS + Apps

Docker Image:
RootFS + Apps

Docker images do not contain a full operating system kernel.


5. Docker Architecture

Docker has multiple components.

Human
  ↓
Docker Client
  ↓
Docker Daemon / Docker Server
  ↓
containerd
  ↓
Linux Kernel
  ↓
Containers

Main components

ComponentMeaning
Docker ClientCommand-line tool used by humans
Docker DaemonDocker server that manages containers
containerdContainer runtime used internally by Docker
KernelLinux kernel that provides container features
ContainerRunning application environment

When you run a command like:

docker ps

You are using the Docker client.

The Docker client talks to the Docker daemon, and the Docker daemon manages containers.


6. Docker Workflow

Basic Docker workflow:

Step 1: Install Docker Engine
Step 2: Download Docker image
Step 3: Pull image from Docker registry
Step 4: Create container from image
Step 5: Start, stop, restart, pause, unpause, kill, or remove container

Docker registry example:

Docker Hub

Docker Hub stores Docker images such as:

httpd
nginx
ubuntu
mysql
jenkins
tomcat

7. Verify Docker Installation

First check whether Docker is installed.

docker version

Expected result:

Client: Docker Engine
Server: Docker Engine

You can also check detailed Docker information:

docker info

This shows details such as:

Number of containers
Number of images
Docker version
Storage driver
Operating system
CPU
Memory

8. Check Existing Docker Images

To list Docker images available on your system:

docker images

Example output:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

If no image is available, the list may be empty.


9. Download Docker Image

Now download the Apache HTTP Server image.

Docker image name:

httpd

Command:

docker pull httpd

This downloads the image from Docker Hub.

Now check images again:

docker images

Example output:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
httpd        latest    xxxxxxxxxxxx   few days ago   xxxMB

10. Create a Container

A container is created from an image.

Command:

docker create httpd

This creates a container but does not start it.

To see running containers:

docker ps

You may not see the container because it is not running yet.

To see all containers, including stopped containers:

docker ps -a

Example output:

CONTAINER ID   IMAGE   COMMAND              STATUS    PORTS   NAMES
943e7fcd9ce9   httpd   "httpd-foreground"   Created           nice_name

11. Start a Container

Use the container ID from docker ps -a.

Example:

docker start 943e7fcd9ce9

Now check again:

docker ps

Example output:

CONTAINER ID   IMAGE   STATUS         PORTS     NAMES
943e7fcd9ce9   httpd   Up few seconds  80/tcp    nice_name

12. Stop a Container

To stop a running container:

docker stop 943e7fcd9ce9

Check container status:

docker ps -a

Example:

STATUS
Exited

13. Restart a Container

To restart a container:

docker restart 943e7fcd9ce9

Check status:

docker ps -a

14. Pause a Container

Pause temporarily freezes the running container.

docker pause 943e7fcd9ce9

Check status:

docker ps -a

Example:

STATUS
Up ... Paused

15. Unpause a Container

To resume a paused container:

docker unpause 943e7fcd9ce9

Check again:

docker ps -a

16. Check Container Resource Usage

To see live CPU, memory, network, and disk usage:

docker stats

Example output:

CONTAINER ID   NAME       CPU %   MEM USAGE / LIMIT   NET I/O
943e7fcd9ce9   web_app    0.10%   20MiB / 1GiB        1kB / 1kB

To exit from docker stats, press:

CTRL + C

17. Kill a Container

docker stop gracefully stops a container.

docker kill forcefully stops it.

docker kill 943e7fcd9ce9

Check status:

docker ps -a

18. Remove a Container

A container must be stopped before removing it.

docker rm 943e7fcd9ce9

Check all containers:

docker ps -a

The removed container should not appear anymore.


19. Docker Container Lifecycle Commands

Here is the full lifecycle:

docker create httpd
docker ps
docker ps -a
docker start <container_id>
docker stop <container_id>
docker restart <container_id>
docker pause <container_id>
docker unpause <container_id>
docker stats
docker kill <container_id>
docker rm <container_id>

Example using container ID:

docker create httpd
docker ps -a
docker start 943e7fcd9ce9
docker stop 943e7fcd9ce9
docker start 943e7fcd9ce9
docker restart 943e7fcd9ce9
docker pause 943e7fcd9ce9
docker unpause 943e7fcd9ce9
docker kill 943e7fcd9ce9
docker rm 943e7fcd9ce9

20. Shortcut Command: docker run

Instead of doing this:

docker pull httpd
docker create httpd
docker start <container_id>

You can use:

docker run httpd

docker run performs:

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

So this command:

docker run httpd

means:

pull + create + start + attach

This runs the container in the foreground.


21. Run Container in Detached Mode

Detached mode means the container runs in the background.

Command:

docker run -d httpd

This means:

pull + create + start + do not attach

Check running containers:

docker ps

Example output:

CONTAINER ID   IMAGE   STATUS         PORTS   NAMES
6e8eb2b841ba   httpd   Up few seconds  80/tcp  random_name

22. Give a Name to a Container

It is easier to manage containers by name instead of ID.

Example:

docker run -d --name web1 httpd

Now check:

docker ps

You can stop it by name:

docker stop web1

Start it again:

docker start web1

Remove it:

docker rm web1

If it is running, stop it first:

docker stop web1
docker rm web1

23. Go Inside a Running Container

To enter a running container:

docker exec -it <container_id> /bin/bash

Example:

docker exec -it 6e8eb2b841ba /bin/bash

Now you are inside the container.

Example prompt:

root@6e8eb2b841ba:/usr/local/apache2#

You can run Linux commands inside the container:

ls
pwd
whoami
hostname

To exit from the container:

exit

Important:

docker exec does not create a new container.
It opens a shell inside an already running container.

24. Inspect a Container

To see full details of a container:

docker inspect <container_id>

Example:

docker inspect 6e8eb2b841ba

This shows details like:

Container ID
Image name
Container IP address
Network settings
Mounts
Environment variables
Port configuration

25. Find Container IP Address

You can inspect the container:

docker inspect 6e8eb2b841ba

Look for:

IPAddress

Example container IP:

172.17.0.5

To test the Apache web server from the Docker host:

curl http://172.17.0.5

Expected output:

<html><body><h1>It works!</h1></body></html>

This confirms that the Apache container is running.


26. Access Container from Outside

A container has its own internal IP.

Example:

Container IP: 172.17.0.5

But this IP is usually accessible only from the Docker host.

To access the container from outside, you should use port mapping.


27. Docker Port Mapping

Apache inside the container runs on port 80.

To access it from the host machine, map a host port to the container port.

Format:

docker run -d -p <host_port>:<container_port> <image_name>

Example:

docker run -d -p 80:80 httpd

Meaning:

Host port 80      → Container port 80

Now access from browser:

http://server-ip

Or using curl:

curl http://localhost

28. Run Apache on Host Port 81

If port 80 is already busy, use another host port.

Example:

docker run -d -p 81:80 httpd

Meaning:

Host port 81      → Container port 80

Now access:

curl http://localhost:81

From browser:

http://server-ip:81

Expected output:

It works!

29. Foreground vs Detached Port Mapping

Foreground mode:

docker run -p 80:80 httpd

This starts Apache and attaches your terminal to the container.

Detached mode:

docker run -d -p 80:80 httpd

This starts Apache in the background.

For servers like Apache, detached mode is usually better.


30. Complete Practical Lab

Lab Goal

Run Apache HTTP Server using Docker and access it from browser.


Step 1: Check Docker

docker version
docker info

Step 2: Check images

docker images

Step 3: Pull Apache image

docker pull httpd

Step 4: Verify image

docker images

Step 5: Create container

docker create httpd

Step 6: Check all containers

docker ps -a

Copy the container ID.

Example:

943e7fcd9ce9

Step 7: Start container

docker start 943e7fcd9ce9

Step 8: Check running container

docker ps

Step 9: Inspect container

docker inspect 943e7fcd9ce9

Find the container IP address.

Example:

172.17.0.5

Step 10: Test Apache using container IP

curl http://172.17.0.5

Expected output:

It works!

Step 11: Stop container

docker stop 943e7fcd9ce9

Step 12: Remove container

docker rm 943e7fcd9ce9

31. Practical Lab Using docker run

Now do the same thing using shortcut command.

docker run -d --name apache1 -p 81:80 httpd

Check container:

docker ps

Test:

curl http://localhost:81

Enter container:

docker exec -it apache1 /bin/bash

Inside container:

pwd
ls
hostname

Exit:

exit

Stop container:

docker stop apache1

Remove container:

docker rm apache1

32. Useful Docker Commands Summary

Docker information

docker version
docker info

Image commands

docker images
docker pull httpd

Container list commands

docker ps
docker ps -a

Container lifecycle commands

docker create httpd
docker start <container_id>
docker stop <container_id>
docker restart <container_id>
docker pause <container_id>
docker unpause <container_id>
docker kill <container_id>
docker rm <container_id>

Shortcut run commands

docker run httpd
docker run -d httpd
docker run -d --name web1 httpd
docker run -d -p 80:80 httpd
docker run -d -p 81:80 httpd

Access container shell

docker exec -it <container_id> /bin/bash

Inspect container

docker inspect <container_id>

Monitor container

docker stats

33. Common Mistakes and Fixes

Mistake 1: Using wrong command

Wrong:

docker versions

Correct:

docker version

Mistake 2: Container not showing in docker ps

docker ps only shows running containers.

Use:

docker ps -a

This shows all containers.


Mistake 3: Port already in use

If this fails:

docker run -d -p 80:80 httpd

Use another port:

docker run -d -p 81:80 httpd

Mistake 4: Removing running container

If this fails:

docker rm apache1

Stop it first:

docker stop apache1
docker rm apache1

Or force remove:

docker rm -f apache1

34. Final Practice Exercise

Run these commands yourself:

docker version
docker info
docker images
docker pull httpd
docker run -d --name myweb -p 8080:80 httpd
docker ps
curl http://localhost:8080
docker exec -it myweb /bin/bash
exit
docker inspect myweb
docker stats
docker stop myweb
docker rm myweb
docker ps -a

Expected result:

Apache HTTP Server container starts successfully.
Application is accessible on host port 8080.
Container can be stopped and removed successfully.

35. Key Takeaways

Docker is a container management platform.

Docker containers are lightweight application runtime environments.

Docker helps save:

CPU
RAM
Disk
Time
Cost

Docker image contains:

Root filesystem + Application

Docker container is:

Running instance of Docker image

Important Docker commands are:

docker pull
docker images
docker create
docker start
docker stop
docker restart
docker pause
docker unpause
docker kill
docker rm
docker run
docker exec
docker inspect
docker stats
docker ps

The most common shortcut command is:

docker run -d -p 81:80 httpd

This command downloads Apache if needed, creates a container, starts it in background, and maps host port 81 to container port 80.

Yes — add this References section at the end of the tutorial.

References

Docker Learning References

  1. Docker internal/container concepts
    https://www.devopsschool.xyz/course/section.php?id=767
  2. Docker command-line complete reference
    https://www.devopsschool.com/blog/the-docker-command-line-complete-referenece/
  3. Docker installation and configuration
    https://www.devopsschool.com/blog/docker-installation-and-configurations/
  4. How to install Docker in Linux
    https://www.devopsschool.com/blog/how-to-install-docker-in-linux/
  5. Lifecycle of Docker containers
    https://www.devopsschool.com/blog/lifecycle-of-docker-containers/
  6. Docker Hub registry
    https://hub.docker.com/

Related SSH References

These are useful when accessing Docker servers remotely.

  1. Secure Shell SSH tools
    https://www.devopsschool.com/blog/secure-shell-ssh-tools/
  2. Top SSH clients: features, pros, cons, comparison
    https://www.devopsschool.com/blog/top-10-ssh-clients-features-pros-cons-comparison/

Optional Official Docker References

You can also include these for students:

  1. Official Docker documentation
    https://docs.docker.com/
  2. Docker Engine installation guide
    https://docs.docker.com/engine/install/
  3. Docker CLI reference
    https://docs.docker.com/reference/cli/docker/
  4. Apache HTTPD image on Docker Hub
    https://hub.docker.com/_/httpd