DevLearningTools

LEARN · DOCKER

What Is Docker and Why Use It?

Docker packages an application with everything it needs so it runs the same way on every machine. Learn how the Docker client, daemon, image registry, and containers fit together, and how containers differ from virtual machines.

A program that works on your laptop often breaks on another machine, because the other machine has a different Python version, a missing library, or a different operating system setting. Docker solves this by packaging the program together with everything it depends on into a container. The container runs the same way anywhere Docker runs.

Learning Objectives

  • Explain the problem Docker solves: consistent environments across machines.
  • Name the four main parts of Docker: client, daemon, images, and registry.
  • Describe the difference between a container and a virtual machine.

How Docker's Parts Work Together

Think of Docker like a restaurant. You are the customer and you place an order. The waiter, which is the Docker client, takes the order to the kitchen. The kitchen, which is the Docker daemon, cooks the food. If the kitchen doesn't have an ingredient, it gets it from the supplier, which is the registry.

123
  1. 1You: You type a command, like docker run nginx.
  2. 2Docker Daemon: The worker. It builds images and starts containers, shown as the small boxes on top.
  3. 3Registry: A library of ready-made images, such as Docker Hub. The daemon downloads from here when it doesn't have an image.

You Run a Command

docker run nginx

Client Sends It to the Daemon

the docker CLI

Daemon Does the Work

builds, runs, and manages objects

Image Pulled If Needed

from a registry such as Docker Hub

Containers vs Virtual Machines

A virtual machine runs a complete guest operating system on top of virtualized hardware. A container shares the host's kernel and only packages the application and its libraries. That makes containers smaller and quicker to start.

Virtual machineContainer
What it includesA full guest operating systemThe application and its libraries, sharing the host kernel
SizeUsually gigabytesUsually megabytes, depending on the image
Start timeMinutesSeconds
IsolationStrong, separate kernelProcess-level, shared kernel
NOTE

Containers are not a replacement for virtual machines in every case. Isolation is weaker because containers share the host kernel. Use containers for packaging and running applications, and VMs when you need a different operating system kernel.

Learning Check

What does the Docker daemon do?

It builds images, runs and manages containers, and handles networks and volumes. The client only sends it commands.

Why is a container usually faster to start than a virtual machine?

It doesn't boot a whole guest operating system. It starts as a process on the host, using the host's kernel.

Step by Step: Check Docker Works

  • Install Docker Desktop (Windows or Mac) or Docker Engine (Linux).
  • Open a terminal and run docker --version to confirm the client is installed.
  • Run docker run hello-world. Docker pulls a tiny test image and prints a message, which proves the daemon works.

Advantages and Disadvantages

ADVANTAGES
  • + The same container runs the same way on a laptop, a test server, and production.
  • + Containers start in seconds and use far less memory than full virtual machines.
  • + Images can be shared through registries, so a teammate pulls one command and gets the same setup.
  • + Works well with CI tools, so automated tests run in a clean, repeatable environment.
DISADVANTAGES
  • − There's a learning curve: images, containers, volumes, and networks are new concepts.
  • − Containers share the host kernel, so isolation is weaker than with a virtual machine.
  • − On Windows and Mac, Docker runs a small Linux virtual machine behind the scenes.
  • − Data inside a container is lost when it's removed, unless you use volumes.

Common Mistakes

Thinking a container is just a small virtual machine

A container is a process on the host that is isolated from other processes. It doesn't boot its own operating system, so it starts faster and behaves differently from a VM.

Putting many unrelated apps in one container

Run one main process per container. Separate services, such as a web app and a database, should be separate containers that talk to each other.

Keeping important data inside the container

Data written inside a container disappears when the container is removed. Store important data in a volume.

Interview Questions

What is Docker in one sentence?

Docker packages an application and its dependencies into a container so it runs the same way on any machine that runs Docker.

How is a container different from a virtual machine?

A VM includes a full guest operating system on virtual hardware. A container shares the host's kernel and only packages the application and its libraries, so it's smaller and starts faster.

What does the Docker daemon do?

It builds images, runs and manages containers, and handles networks and volumes. The client only sends it commands.

Summary

Docker packages applications with their dependencies so they run the same way everywhere. The client sends commands to the daemon, which builds and runs containers from images, pulling images from a registry when needed.