Skip to content
Happy Programming Guide
Start learning
Web Development

What Is Docker and Why Do We Need It?

Docker packages an application with everything it needs to run, so it runs the same on every machine. What a container actually is, the problem it solves, and your first one in five commands.

Servers mounted in a data centre rack

Docker is a tool for packaging an application together with everything it needs — the runtime, the libraries, the configuration — into a single unit called a container that runs identically on a laptop, a colleague’s machine and a server. It exists because “works on my machine” was the most expensive phrase in software, and it solves that by making the machine part of the package. Around seven in ten professional developers now use it. Here is what it is and why.

The problem it solves#

An application depends on a specific runtime version, specific libraries, environment variables, a database of a particular version, and often system packages. Getting all of that identical on every developer’s machine, the test server and production is hard, and every difference is a place for a bug to hide. Onboarding a new developer used to be a day of installing things. Deploying used to be a document of manual steps.

Docker’s answer: describe the environment once, in a file, build it into an image, and run that image everywhere. The application never sees a machine that differs from the one it was built for.

The three words#

  • Image: a read-only snapshot of a file system with your app and its dependencies installed. Built once, shared through a registry, never changed.
  • Container: a running instance of an image, isolated from the host and from other containers. Start one, stop it, throw it away, start another from the same image and it is identical.
  • Dockerfile: the recipe for building an image, a short text file of steps.
Output
FROM python:3.13-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Read it top to bottom: start from an official Python image, set a working folder, copy the dependency list in and install it, copy the code in, and say what to run. That file, checked into the repository, is the environment.

Containers vs virtual machines#

A virtual machine runs a whole operating system on emulated hardware, which costs gigabytes and minutes to start. A container shares the host’s operating-system kernel and isolates only the process, its file system and its network. It costs megabytes and starts in under a second. You can run dozens on a laptop.

Container Virtual machine
Starts in milliseconds to seconds minutes
Size tens to hundreds of MB gigabytes
Isolation process-level full OS
Best for packaging and running applications running different operating systems, strong isolation

On Windows and macOS, Docker Desktop runs a small Linux VM in the background and puts containers inside it, which is why it is heavier there than on Linux.

Your first container#

Terminal
docker run --rm hello-world                      # 1. proves the install works
docker run --rm -it python:3.13 python           # 2. a Python prompt, no Python installed on the host
docker run --rm -d -p 8080:80 --name web nginx   # 3. a web server, in the background, on port 8080
docker ps                                        # 4. see it running
docker stop web                                  # 5. and stop it

Line 2 is the moment it clicks for most people: a complete Python, downloaded and running in seconds, gone without trace when you exit. Line 3 maps port 80 inside the container to 8080 on your machine; open http://localhost:8080.

Building your own#

Terminal
docker build -t myapp .
docker run --rm -p 8000:8000 myapp

With the Dockerfile above in the folder, that builds an image called myapp and runs it. Anyone with the repository can do the same and get exactly your environment.

Several services: Compose#

Real applications need a database, maybe a cache, maybe a queue. Compose describes them together:

Output
# compose.yaml
services:
  web:
    build: .
    ports: ["8000:8000"]
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/app
    depends_on: [db]
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app
    volumes:
      - dbdata:/var/lib/postgresql/data

volumes:
  dbdata:
Terminal
docker compose up

One command starts the application and a PostgreSQL of the exact version, networked so web reaches the database at the hostname db. New team member, day one: clone, docker compose up, done. That is the onboarding story Docker was built for.

Where it earns its keep#

  • Any project with a database or other service: no more installing PostgreSQL locally.
  • Teams: identical environments without a setup document.
  • Deployment: the image that passed tests is the image that runs in production.
  • Trying things: a tool or a language for an afternoon, then docker rm.
  • CI: builds run in a clean, defined environment every time.

Where it is more trouble than it is worth#

  • A single script with no dependencies. A virtual environment is enough.
  • Desktop and mobile applications with graphical interfaces.
  • The first weeks of learning a language. Learn the language; add Docker when a project needs a service.
  • Very tight resource limits, since Docker Desktop on Windows and macOS uses a few gigabytes of memory.

Questions people ask#

Is Docker free?

Docker Engine and the command-line tools are open source. Docker Desktop, the Windows and macOS application, is free for personal use and small businesses and licensed for larger companies; check the current terms.

Is Docker the same as Kubernetes?

No. Docker builds and runs containers. Kubernetes schedules many containers across many machines. You need Docker long before you need Kubernetes, and most projects never need the latter.

Do containers run on Windows?

Linux containers run on Windows through WSL 2, which Docker Desktop manages. Windows containers also exist for Windows-only applications and are a separate, rarer thing.

Where do images come from?

Docker Hub and other registries hold official images for languages, databases and tools. Prefer official images and pin versions, as in python:3.13-slim.

Where to go next#

Docker Desktop for beginnersRead next

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *