1. Container or virtual machine?
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 machine | Container | |
|---|---|---|
| Isolation | Strong (hardware level) | Good, but the kernel is shared |
| Size | Gigabytes | Megabytes |
| Start time | Tens of seconds to minutes | About a second |
| Packaging | Disk image | Layered image from a registry |
| Good for | Different operating systems, strong separation | Services, microservices, repeatable tools and labs |
Try it
2. The four words to learn first
| Word | Meaning | Analogy |
|---|---|---|
| Image | Read-only template: files plus the default command | A class, or a VM template |
| Container | A running (or stopped) instance of an image | An object, or a cloned VM |
| Registry | A server that stores images (Docker Hub, GitHub, Quay) | An app store |
| Tag | A label on an image version, nginx:1.27; no tag means latest | A 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
| Task | Command | Notes |
|---|---|---|
| Run in the foreground | docker run alpine echo hi | Pulls the image if missing, runs, exits |
| Run in the background | docker run -d --name web -p 8080:80 nginx | -d detach, --name, -p host:container publishes a port |
| Interactive shell | docker run -it --rm alpine sh | -it terminal, --rm delete on exit |
| List running | docker ps | -a includes stopped ones |
| Logs | docker logs -f web | -f follows, --tail 20 |
| Run inside a container | docker exec -it web sh | Starts a second process in the same container |
| Stop / start / restart | docker stop web, start, restart | stop sends SIGTERM, then SIGKILL after 10 s |
| Remove | docker rm web, docker rm -f web | -f kills a running one |
| List and remove images | docker images, docker rmi nginx | |
| Pull an image | docker pull nginx:1.27 | Pin a tag, avoid latest in real work |
| Inspect details | docker inspect web | Big JSON: IP, mounts, command, restart policy |
| Resource use | docker stats | Live 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
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 code | Usual meaning |
|---|---|
| 0 | The process finished normally |
| 1 or other small number | The application reported an error: read the logs |
| 125, 126, 127 | Docker could not start it, the command was not executable, or the command was not found |
| 137 | Killed (SIGKILL), often out of memory or docker kill |
| 143 | Stopped with SIGTERM (normal docker stop) |
Try it
Check yourself
Think first, type an answer if you like, then reveal.