DOCKER · DOCKER FROM SCRATCH · CHAPTER 1

Introduction to Docker

Why containers exist, what they isolate and how to install Docker on Ubuntu: the foundation of the whole route.

8 min readCourse: Docker from ScratchLevel: beginner

From point A to point B

Point A: you have heard about Docker and containers, maybe you have even copied a docker run or two, but you are not sure what problem they solve or what happens inside. Point B: by the end of this chapter you will be able to explain why containers exist, how they differ from a virtual machine and what an image is, and you will have Docker installed and running on your Linux machine.

Everything that comes later in this route, Kubernetes included, builds on this foundation. That is why Docker comes right after Linux in the route.

Why do you need Docker?

Picture an application with four parts: a web server, a database, a messaging system and an orchestration tool. Each part needs certain libraries and certain operating system versions. What one part requires, another one breaks.

Without containers, three problems show up that every team recognises:

  • Compatibility and dependencies. Upgrading a library for one component can take another one down. Every change means checking the whole stack again.
  • Setups that take days. Every new team member has to build their environment step by step, with long instructions and plenty of ways to get it wrong.
  • Different environments. Development, test and production are never identical. The classic “it works on my machine” is born here.

That web of incompatible combinations is known as the matrix from hell.

The matrix from hell vs containers Without containers, every component of the stack must be compatible with the libraries and operating system of each environment, and some combinations fail. With containers, each component travels with its own dependencies and runs the same on development, test and production. WITHOUT CONTAINERS WITH CONTAINERS DevTestProd Web serverDatabaseMessagingOrchestration ✓✓✗ ✓✗✓ ✗✓✓ ✓✓✗ libs · versions · OS every new combination can break Web+ libs DB+ libs Queue+ libs Orch.+ libs Docker Engine Any Linux host Dev ✓ Test ✓ Prod ✓ same package, same result everywhere

Without containers, any combination of component and environment can fail. With containers, each piece travels with its dependencies and behaves the same everywhere. Diagram: The House of Routing.

Docker solves this with a simple idea: package each component with everything it needs and run it in isolation. If the host has Docker, the package works.

What is a container?

A container is a completely isolated environment. It has its own processes, its own network and its own mounted filesystem, but it shares the kernel of the host operating system.

What a container isolates and what it shares Three containers run on one host. Each has its own processes, network and mounts, isolated with Linux namespaces, but all of them share the same operating system kernel. ONE HOST · THREE ISOLATED CONTAINERS web processes · PID 1: nginx network · 172.17.0.2 mounts · own filesystem db processes · PID 1: postgres network · 172.17.0.3 mounts · own filesystem queue processes · PID 1: redis network · 172.17.0.4 mounts · own filesystem Container runtime (Docker Engine) Shared Linux kernel · namespaces + cgroups isolated: processes, network, mounts · shared: the kernel

Each container sees only its own processes, network and mounts. Underneath, they all use the same Linux kernel. Diagram: The House of Routing.

That isolation is not magic: two Linux kernel features provide it. Namespaces decide what each container can see (processes, network, mounts, users). cgroups limit how much it can consume (CPU, memory, disk I/O).

Containers existed before Docker; LXC, for example, already offered them. But they were hard to set up. Docker, which relied on LXC in its early versions, made them easy to use with a single tool and a common image format.

Sharing the kernel has consequences

Ubuntu, Debian, Fedora and CentOS all use the same Linux kernel; what changes is the libraries and tools. That is why an Ubuntu host can run a container based on Debian or Fedora: the image brings its own libraries and uses the host's kernel.

Watch out for Windows. A Windows container needs a Windows kernel, so it does not run natively on a Linux host. When you use Docker on Windows or macOS to run Linux containers, there is a small Linux virtual machine underneath.

Containers vs virtual machines

A virtual machine virtualises hardware: a full guest operating system, with its own kernel, runs on top of a hypervisor. A container only isolates processes on top of the host's kernel.

Virtual machines vs containers Each virtual machine carries its own guest OS on top of a hypervisor; containers share the kernel of the host OS through a container runtime. VIRTUAL MACHINES CONTAINERS Hardware Host OS Hypervisor Guest OS Libs App A Guest OS Libs App B Guest OS Libs App C GB per VM · boots in minutes each VM carries a full OS Hardware Host OS · shared kernel Container runtime Libs App A Libs App B Libs App C MB per container · starts in seconds all containers share one kernel

Virtual machines carry an operating system per application; containers share the kernel. Diagram: The House of Routing.

Virtual machineContainer
Typical sizeGigabytesMegabytes
Start-upMinutesSeconds
KernelIts own, one per VMShared with the host
IsolationStronger: no shared kernelLighter, but enough for most cases
Can mix operating systemsYes, Windows and Linux on the same hostOnly those compatible with the host kernel

It is not one or the other. In the cloud it is very common to run containers inside virtual machines: the VM brings strong isolation and the container brings lightness and portability.

Image vs container

These two terms get mixed up a lot. The difference is simple:

  • Image: the blueprint. A read-only template with the application and everything it needs, ready to run.
  • Container: a running instance of that image. From one image you can start one container or a hundred, each isolated from the others.

Thousands of ready-made images live in public registries such as Docker Hub: operating systems, databases, web servers. You can also build your own.

docker pull nginx:alpine      # download the image
docker images                 # list local images
docker run -d nginx:alpine    # create and start a container from it
docker ps                     # list running containers

Docker in DevOps

In the past, development handed over the application with an installation guide and operations tried to reproduce the environment. With Docker, development hands over a Dockerfile and an image. That image works the same on any host with Docker.

From Dockerfile to running container A Dockerfile is built into an image, the image is pushed to a registry such as Docker Hub, and any host with Docker pulls it and runs it as a container. BUILD · SHIP · RUN build push pull · run Dockerfilerecipe Imageblueprint RegistryDocker Hub Containerrunning instance docker build docker push docker run developers build once · operations run it the same way on any host

Build once, publish to a registry and run anywhere. Diagram: The House of Routing.

  • Fast deployments: a container starts in seconds and is replaced just as quickly.
  • Small, independent pieces: each service gets its own container, which makes microservices easier to work with.
  • Portability: the same image moves from the laptop to test and to production unchanged.
  • A shared language: the Dockerfile documents how the application is built, and development and operations share it.

Docker editions today

For years people talked about two editions: Community, free, and Enterprise, paid and officially supported. The picture has changed, and it is worth being clear about it:

ProductWhat it isLicence
Docker EngineThe engine that runs on Linux: daemon, API and CLI. It is what you install on servers.Open source and free
Docker DesktopA graphical application for Windows, macOS and Linux. It bundles the engine, Compose and a light VM when needed.Free for personal use, education, non-commercial open source projects and companies with fewer than 250 employees and less than 10 million US dollars in annual revenue. Beyond that, it requires a paid subscription.
Former Docker EnterpriseIn November 2019, Mirantis bought Docker's Enterprise business, which continues today under the Mirantis brand.Commercial, from Mirantis
To learn: Docker Engine on an Ubuntu VM is all you need. It is free and it is the same thing you will find on real servers.

Installing Docker on Ubuntu

Here are two paths. For practice, the first one is the most direct.

Option 1 · Docker Engine with the official script

First, remove old or unofficial packages that could clash with the installation:

for pkg in docker.io docker-doc docker-compose docker-compose-v2 docker-buildx podman-docker containerd runc; do
  sudo apt-get remove -y $pkg
done

Then download the installation script. The --dry-run option shows you the steps without running them; review them before running it for real:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh ./get-docker.sh --dry-run   # only shows what it would do
sudo sh ./get-docker.sh             # installs Docker Engine

sudo docker run hello-world         # smoke test

If you see Hello from Docker!, the engine downloaded an image, created a container and ran it. It works.

Lab use only. Docker recommends this script only for test and development environments. On production servers, install from Docker's official repository with apt, as its documentation describes.

Using Docker without sudo, wisely

To avoid typing sudo before every command, add your user to the docker group:

sudo groupadd docker            # it may already exist
sudo usermod -aG docker $USER
newgrp docker                   # apply the change in this session
docker run hello-world
Security first. Membership in the docker group is equivalent to root privileges on that machine. Do it in your lab; on shared servers, think carefully about who gets that access.

Option 2 · Docker Desktop for Linux

If you prefer a graphical interface, download the .deb package from Docker's documentation and install it:

sudo apt-get update
sudo apt install ./docker-desktop-amd64.deb
systemctl --user start docker-desktop

At the end, apt may show an error about installing a downloaded package; the documentation says you can ignore it. Remember to check the Docker Desktop licence if you will use it at a company.

The golden rule: a container lives as long as its process

A container is not a virtual machine that stays switched on. It is designed to run one main process, and it exists as long as that process is alive. When the process ends, the container stops.

A container lives as long as its main process Running the ubuntu image alone starts bash with no terminal attached, so the process ends and the container exits at once. Running it with sleep 300 keeps the main process alive, so the container stays up for five minutes and then exits. MAIN PROCESS ALIVE = CONTAINER RUNNING 0 stime → docker run ubuntu bash has no terminal → ends at once → Exited (0) docker run ubuntu sleep 300 Up · PID 1 = sleep 300 Exited (0) process ends → container stops

The ubuntu image starts bash by default; with no terminal attached, bash ends right away and so does the container. Diagram: The House of Routing.

Try it yourself:

docker run ubuntu               # starts and ends instantly
docker ps                       # not listed: it is no longer running
docker ps -a                    # here it is: status Exited (0)

docker run -d ubuntu sleep 300  # the main process lasts 5 minutes
docker ps                       # now it is Up

docker run -it ubuntu bash      # or step in with an interactive terminal
exit                            # leaving bash stops the container

That is why an image like nginx keeps running: its main process is a web server that does not end.

Challenge

Answer before you open the solution. Getting it wrong here is the cheapest way to learn.

1. What do all containers running on the same host share?

The operating system kernel. Processes, network and mounts are isolated for each container; the kernel is common.

2. You have an nginx image. How many containers can you create from it?

As many as you need. The image is the read-only template; each container is an isolated instance created from it.

3. You run docker run ubuntu and the container does not show up in docker ps. What happened?

Its main process ended. bash started without a terminal and exited; the container is now in the Exited state. You can see it with docker ps -a.

4. Why should you not add just any user to the docker group?

Because that group grants privileges equivalent to root on the host. It is convenient in a lab, but a risk on shared servers.

What you take away

  • Docker was born to end dependency problems and “it works on my machine”.
  • A container isolates processes, network and mounts, and shares the host's kernel.
  • A VM virtualises hardware; a container, processes. They complement each other.
  • The image is the blueprint; the container, a running instance.
  • Docker Engine is free and open source; Docker Desktop has licence conditions.
  • A container lives as long as its main process lives.

In the next chapter we move to day-to-day practice: the basic Docker commands to create, list, stop and remove containers and images.

Official resources

Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's Docker for Absolute Beginners course (December 2024). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official Docker documentation.

Navigate