Course contents · Lesson 2 of 22

What Is Docker? Containers Explained for Beginners

Containers 101 · Lesson 2 of 22 · 8 min Foundations

A classmate sends you their project: "just run it". On your laptop it crashes, because you have a different version of Python (a programming language) and a library is missing. "It works on my machine," they reply, the classic developer excuse. So what is Docker, and how do its images, containers and registries end that problem?

The "works on my machine" problem

Every app relies on things outside its own code, called dependencies: a language runtime (the program that runs your code, such as Python or Node.js), libraries (ready-made code your app calls), system tools and settings.

No two machines have exactly the same set. For example, your laptop has Python 3.12 and version 2 of a library, the server Python 3.9 and version 1. The code is identical, yet it fails on the server.

The fix: ship the dependencies with the app as one sealed package, so every machine runs the same files. That package is a container image, and Docker is the best-known tool for making one.

Packaging fixes what is inside the box, not outside it. A missing database or a blocked network still breaks the app, and a Linux image needs a Linux kernel and a matching processor type: amd64 (most Intel and AMD PCs) or arm64 (such as Apple silicon Macs).

Three nouns: image, container and registry

An image is the sealed package: your app's code, its libraries and system files, its language runtime, and settings such as the command that starts it. It is read-only: running it never changes it, so every copy stays identical. As the diagram shows, it holds no kernel.

A sealed tiffin box beside a read-only container image, with dashed lines joining matching parts: the note on the lid saying how to serve matches settings and start command, the main dish matches app code, sides and chutney match libraries and system files, and rice as the base matches the language runtime such as Python or Node.js. A banner says the kitchen, the host's kernel, is not in the box
Everything is packed and sealed except the kitchen: the host's kernel.

A container is a running instance of an image: its files plus one or a few walled-off processes, as you saw in Virtual Machines vs Containers. In the analogy, the image is the packed box and a container is the meal being eaten. Unlike a meal, an image copies exactly, so many containers can start from one image.

A registry is a service that stores images for other machines to download. Uploading an image is pushing; downloading it is pulling. Docker Hub is the public registry Docker uses by default; companies also run private ones. One image name can hold many versions, each labelled with a tag such as latest.

So what is Docker?

Docker is a set of tools, made by the company Docker, Inc., that builds images, runs containers from them and shares them through registries.

Its core, Docker Engine, is open source (its code is public and free to reuse) and has two main parts. The Docker daemon (dockerd) is a background program that does the work. The Docker client is the docker command you type, which passes each request to the daemon. Docker Desktop, the laptop app from lesson 1, bundles both with its hidden Linux virtual machine (VM).

Docker did not invent the walls around containers: they are lesson 1's Linux kernel features, namespaces (what a process can see) and control groups, or cgroups (how much it can use). Docker made them easy to use.

The image format is an open standard from the Open Container Initiative (OCI), an industry group Docker helped start in 2015. So images built with Docker also run in other tools, such as Podman, and on Kubernetes, which runs containers across many machines.

In the flowchart, you write a Dockerfile, a text file of build instructions (see Dockerfile Basics), and Docker builds an image from it. You push the image to a registry; another machine pulls an identical copy and runs containers from it. Notice that only the image travels.

Flowchart in four steps. On your laptop, step 1, docker build, turns a Dockerfile into a read-only image. Step 2, docker push, uploads the image to a registry such as Docker Hub. On another machine, step 3, docker pull, downloads an identical copy of the image, and step 4, docker run, starts several running containers from it. A note says only the image travels; containers start where they run
You build and share the image once; containers start fresh wherever it lands.

Everyday commands, in words

You type Docker commands in a terminal, the text window from lesson 1. Nothing to run today; here are the five most common.

  • docker pull nginx downloads the image of nginx, a popular web server. With no tag, Docker assumes latest.
  • docker run nginx creates a new container from the image and starts it, pulling the image first if needed.
  • docker ps lists running containers; add -a to include stopped ones. Each has an ID and a name.
  • docker stop plus a container's name or ID asks its main process to shut down, and forces it after a grace period (10 seconds by default).
  • docker rm plus a name or ID deletes a stopped container. The image stays.

The diagram strings these together for docker run hello-world, a tiny test image. The first time, Docker reports Unable to find image 'hello-world:latest' locally, pulls the image, and the container prints a message beginning Hello from Docker!.

Step-by-step flowchart of docker run hello-world. 1: you type the command. 2: is the image already on this machine? If not, Docker pulls it from Docker Hub, the first time only. 3: Docker creates a container from the image. 4: it starts the container's process, a tiny program. 5: the process prints Hello from Docker! 6: the process ends and the container exits, kept as Exited until docker rm. Notes: run it again and there is no download; every run creates a new container
One download, but every run makes a new container, kept after it exits.

Notice step 6: the container stops when its process ends, but it stays in docker ps -a as Exited until you remove it.

What Docker is not

Not a virtual machine. A container boots no guest operating system; it is processes sharing the host's kernel. On a Mac or Windows laptop, one hidden Linux VM hosts all your containers.

Not an orchestrator. An orchestrator runs containers across many machines and replaces them when a machine fails. Docker looks after one machine: it can restart a crashed container there if set to, but if the machine dies, nothing moves the work elsewhere. Kubernetes does that (see What Is Kubernetes?). Docker's built-in Swarm mode is a simpler orchestrator, not covered here.

Containers are disposable. You don't repair a container: you replace it with a fresh one, from a new image if the app changed. Anything written inside a container vanishes when it is removed, so a database's files belong in a volume, storage that outlives containers (see Docker Volumes and Networking).

Check yourself

You fix a typo inside a running container. A teammate then starts a new container from the same image. Do they get your fix?

No. The image is read-only, so your edit lives only in your container and vanishes when it is removed; to share the fix, build a new image.

You run docker run hello-world twice. How many downloads and containers do you get?

One download and two containers: the second run finds the image locally, but every docker run creates a new container.

The only server running your containers loses power. With Docker alone, do they restart on another server?

No. Docker manages containers on one machine; moving work between machines is the job of an orchestrator such as Kubernetes.