SwitchNRoutesDocker and containers lessonsAll interactive lessonsBlogGet a quote
DOCKER | INTERACTIVE LESSON

Module 1: Containers and the first commands

What a container really is, how it differs from a virtual machine, and the dozen commands you will use every day.

Why it matters: Docker (or Podman) runs almost every modern app, CI pipeline and lab tool. Network gear now ships as containers too (Containerlab, Cisco containers, NetBox, Grafana).
All Docker and containers lessons
Progress is saved in this browser

1. Container or virtual machine?

Virtual machines Containers AppLibrariesFull guest OSAppLibrariesFull guest OSAppLibrariesFull guest OS Hypervisor Host operating system and hardware AppLibrariesAppLibrariesAppLibraries Container engine (Docker / Podman) Host kernel shared by all containers GBs each, minutes to boot, own kernel MBs each, seconds to start, share the host kernel
A container is a normal Linux process with a private view of the system, not a small computer.

A container is built from kernel features: namespaces give it its own process list, network, mount points and host name; cgroups limit its CPU and memory. That is why it starts in a second and why it cannot run a different kernel from the host (a Linux container on Windows or macOS really runs inside a hidden Linux VM).

Virtual machineContainer
IsolationStrong (hardware level)Good, but the kernel is shared
SizeGigabytesMegabytes
Start timeTens of seconds to minutesAbout a second
PackagingDisk imageLayered image from a registry
Good forDifferent operating systems, strong separationServices, microservices, repeatable tools and labs

Try it

2. The four words to learn first

From recipe to running serviceDockerfile(the recipe)Image(the frozenpackage)Container(a runningcopy)Registry(where imagesare stored)
build makes an image from a Dockerfile; run starts a container from an image; push and pull move images to and from a registry such as Docker Hub.
WordMeaningAnalogy
ImageRead-only template: files plus the default commandA class, or a VM template
ContainerA running (or stopped) instance of an imageAn object, or a cloned VM
RegistryA server that stores images (Docker Hub, GitHub, Quay)An app store
TagA label on an image version, nginx:1.27; no tag means latestA version number

Try it

3. Install and check

# Docker Engine on Ubuntu: follow docs.docker.com/engine/install ; or Docker Desktop on Windows/Mac
# Podman on Rocky/RHEL (docker-compatible commands):
sudo dnf install -y podman podman-docker
docker version                         # client and server details (Podman prints its own)
docker info                            # storage driver, cgroup, number of images and containers
docker run --rm hello-world            # downloads a tiny image, prints a welcome, removes itself
RememberEverything below works the same with podman in place of docker. Podman needs no background daemon and can run without root, which is why Red Hat prefers it.

4. The dozen commands

TaskCommandNotes
Run in the foregrounddocker run alpine echo hiPulls the image if missing, runs, exits
Run in the backgrounddocker run -d --name web -p 8080:80 nginx-d detach, --name, -p host:container publishes a port
Interactive shelldocker run -it --rm alpine sh-it terminal, --rm delete on exit
List runningdocker ps-a includes stopped ones
Logsdocker logs -f web-f follows, --tail 20
Run inside a containerdocker exec -it web shStarts a second process in the same container
Stop / start / restartdocker stop web, start, restartstop sends SIGTERM, then SIGKILL after 10 s
Removedocker rm web, docker rm -f web-f kills a running one
List and remove imagesdocker images, docker rmi nginx
Pull an imagedocker pull nginx:1.27Pin a tag, avoid latest in real work
Inspect detailsdocker inspect webBig JSON: IP, mounts, command, restart policy
Resource usedocker statsLive CPU, memory, network
$ docker run -d --name web -p 8080:80 nginx
$ docker ps
CONTAINER ID  IMAGE   COMMAND                 STATUS         PORTS                 NAMES
a1b2c3d4e5f6  nginx   "/docker-entrypoint..." Up 5 seconds   0.0.0.0:8080->80/tcp  web
$ curl -s localhost:8080 | head -3
<!DOCTYPE html>
<html>
$ docker logs web | tail -2
$ docker exec web nginx -v
$ docker rm -f web

Try it

5. The container lifecycle

StatesCreatedRunningPaused orStopped(exited)Removed
A stopped container still exists with its writable layer until you <code>docker rm</code> it. <code>docker ps -a</code> shows them.
Watch outContainers are disposable. Anything written inside the container's own filesystem disappears when it is removed. Data that must survive goes in a volume (Module 3).

Try it

6. Foreground, PID 1 and why a container exits

A container lives as long as its main process (PID 1) lives. docker run alpine echo hi exits immediately because echo finishes. A web server stays up because it keeps running. If a container 'keeps stopping', check docker logs and the exit code in docker ps -a.

Exit codeUsual meaning
0The process finished normally
1 or other small numberThe application reported an error: read the logs
125, 126, 127Docker could not start it, the command was not executable, or the command was not found
137Killed (SIGKILL), often out of memory or docker kill
143Stopped with SIGTERM (normal docker stop)

Try it

Check yourself

Think first, type an answer if you like, then reveal.