Lesson 01 beginner

Introduction to Docker

What Docker is, the problem it solves, and your first containers: from hello-world to a small web app you build yourself.

Docker is a tool that packages an application together with everything it needs to run: the runtime, libraries, system tools and configuration. That package is called an image, and a running copy of an image is called a container. If it runs in a container on your laptop, it runs the same way on a colleague’s machine, in CI and on a production server.

The problem Docker solves

Before containers, shipping software meant shipping instructions: “install Node 20, then this version of OpenSSL, then set these environment variables”. Every server drifted a little from the others, and the classic answer to a bug report was “it works on my machine”.

Docker replaces the instructions with an artifact. You describe the environment once, in a file, and Docker builds an image from it. The image is immutable and versioned, so the thing you tested is exactly the thing you deploy.

Images and containers

The two words you will use the most are easy to mix up. An image is the blueprint, a container is the house built from it.

ImageContainer
What it isRead-only template with the app and its filesA running (or stopped) instance of an image
Created withdocker build or docker pulldocker run or docker create
ChangesNever: you build a new version insteadCan write files to its own thin layer
How manyOne image per version of your appAs many containers as you want from one image

Your first container

If Docker is installed (lesson 4 covers the setup), run the official test image:

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

That one command did four things: looked for the image locally, downloaded it from Docker Hub, created a container from it and ran it. The program printed a message and exited, so the container stopped.

Now something that keeps running. This starts the nginx web server in the background and publishes its port 80 on port 8080 of your machine:

$ docker run -d --name web -p 8080:80 nginx:1.27
4f2c9a1e7b3d8c6f0a5e2b9d7c1f4a8e3b6d0c9f2a7e5b1d4c8f3a6e9b2d7c0f
$ docker ps
CONTAINER ID   IMAGE        COMMAND                  STATUS         PORTS                  NAMES
4f2c9a1e7b3d   nginx:1.27   "/docker-entrypoint.…"   Up 5 seconds   0.0.0.0:8080->80/tcp   web
$ curl -s localhost:8080 | grep title
<title>Welcome to nginx!</title>

Clean up when you are done:

$ docker stop web
web
$ docker rm web
web

Tip: docker run --rm deletes the container automatically when it stops. Use it for anything you run once.

How Docker works

When you type docker run, several pieces cooperate:

  • Docker CLI: the docker command you type. It only sends requests.
  • Docker Engine (dockerd): the background service that builds images and manages containers, networks and volumes.
  • containerd and runc: the low-level runtime that actually starts the container processes using Linux kernel features.
  • Registry: a server that stores images, such as Docker Hub, GitHub Container Registry or your cloud provider’s registry.

A container is not a small virtual machine. It is a normal Linux process that the kernel isolates with namespaces (what the process can see) and cgroups (how much CPU and memory it can use). Lesson 3 compares this with virtual machines in detail.

Build your own image

Pulling images is useful, but the real power is packaging your own code. Create an empty folder with three files. First a tiny Node.js server, server.js:

const http = require("http");

const port = process.env.PORT || 3000;

http
  .createServer((req, res) => res.end(`Hello from a container! Path: ${req.url}\n`))
  .listen(port, () => console.log(`Listening on ${port}`));

Then a Dockerfile that describes the image:

FROM node:22-alpine
WORKDIR /app
COPY server.js .
ENV PORT=3000
EXPOSE 3000
CMD ["node", "server.js"]

And a .dockerignore so local clutter never ends up in the image:

node_modules
.git
*.log

Build the image, give it a name and a tag, and run it:

$ docker build -t hello-node:1.0 .
[+] Building 4.2s (8/8) FINISHED
 => [1/3] FROM docker.io/library/node:22-alpine
 => [2/3] WORKDIR /app
 => [3/3] COPY server.js .
 => exporting to image
 => => naming to docker.io/library/hello-node:1.0
$ docker run --rm -d --name hello -p 3000:3000 hello-node:1.0
$ curl localhost:3000/docker
Hello from a container! Path: /docker
$ docker stop hello
hello

You just did the full Docker loop: write a Dockerfile, build an image, run a container. Lesson 8 explains every Dockerfile instruction.

Exercise

You type docker run nginx. Which component actually creates and starts the container?

  1. The docker CLI itself
  2. Docker Engine (dockerd), with containerd and runc underneath
  3. Docker Hub
  4. The Dockerfile
Show answer

Answer: B. The CLI only sends a request to the Docker Engine API. dockerd prepares the container and hands it to containerd and runc, which start the isolated process.

Key takeaways

  • An image is an immutable package of your app and its environment; a container is a running instance of it.
  • docker run pulls the image if needed, creates a container and starts it.
  • Containers are isolated Linux processes, not virtual machines.
  • A Dockerfile turns your code into an image with docker build.

FAQ

Is Docker free to use?

Docker Engine and the CLI are open source and free. Docker Desktop, the app for macOS and Windows, is free for personal use, education and small companies, and needs a paid subscription in larger organizations.

Do containers only run on Linux?

Linux containers need a Linux kernel. On macOS and Windows, Docker Desktop runs a lightweight Linux virtual machine for you, so the same images work everywhere.

Is Docker the same thing as Kubernetes?

No. Docker builds and runs containers on one machine. Kubernetes schedules and manages containers across many machines. They work well together: you build images with Docker and run them at scale with Kubernetes.

↑ ↓ navigate Enter open Esc close