Docker basics

Lesson 01 · 12 min

Container, image and first commands

How a container differs from a virtual machine, what an image is, and how to live with docker run, ps, logs, exec.

12 minLesson 1 of 5Practice includedAsk AI on any point

This course takes you from your first docker run to an image you are not ashamed to ship to production. It does not repeat the "Docker and deploy" course: there Docker is just one step on the way to a server, while here we dig into Docker itself: layers, cache, volumes, networks, Compose and image security.

AI

You can ask about this point«Lesson summary»

Course plan

  • Lesson 1. Container vs virtual machine, image vs container, basic commands.
  • Lesson 2. Dockerfile: layers, instruction order and cache, COPY vs ADD, CMD vs ENTRYPOINT.
  • Lesson 3. Volumes and networks: bind mount, named volume, DNS between containers, port publishing.
  • Lesson 4. Docker Compose: a web app, Postgres and Redis with healthchecks.
  • Lesson 5. Smaller and safer images: multi-stage, distroless or alpine, non-root, .dockerignore, scanning.
  • Coming later. Registries and image tags, Docker in CI, debugging containers that crash.
AI

You can ask about this point«Course plan»

A container is not a virtual machine

A virtual machine emulates a whole computer: it has its own OS kernel, takes a minute to boot and occupies gigabytes. A container is an ordinary process on the host that the Linux kernel isolates with namespaces (its own processes, network, file system) and limits with cgroups (CPU, memory). The kernel is shared with the host, so a container starts in a fraction of a second and weighs as much as its files.

AI

You can ask about this rule«The cost of a shared kernel»

AI

You can ask about this point«A container is not a virtual machine»

Image and container

An image is an immutable template: a file system made of layers plus metadata (which command to run, which environment variables). A container is a running instance of an image with a thin writable layer on top. You can start ten containers from one image, each with its own changes, and the image stays the same. A code analogy: the image is a class, the container is an object.

First commands
  • -d runs it in the background, --name gives a readable name instead of a random one.
  • -p 8080:80: port 8080 on the host leads to port 80 in the container. Host first, container second.
  • --rm removes the container after it stops, handy for one-off commands: docker run --rm -it node:22 node -v.
  • docker logs shows stdout and stderr of the main process. That is why an app in a container writes logs to the console, not to a file.
AI

You can ask about this rule«A container lives as long as its main process»

AI

You can ask about this point«Image and container»

Check

Lock it in before you move on

  1. 1. What fundamentally sets a container apart from a virtual machine?

  2. 2. How many containers can you start from one image?

  3. 3. What does -p 3000:80 mean?

  4. 4. Why does a container running npm start & stop right away?

PracticeSolve the lesson tasksA few short tasks in the editor. Check your code right away and see where it goes wrong.Start practice