← Learning Hub
Intermediate

Docker: Containers Explained

Docker packs your app with everything it needs - the right Node or Java version, libraries, settings - into an image. Running an image gives a container. It works the same on your laptop, a teammate's laptop and the server. No more "it works on my machine".

Docker packages an app with everything it needs, so it runs identically everywhere. This removes "works on my machine" problems, makes setting up databases for development trivial, and is the foundation of modern deployment.

Words to know

  • Image - a read-only package (like a recipe or a class)
  • Container - a running copy of an image (like a dish made from the recipe, or an object)
  • Dockerfile - the instructions to build an image
  • Registry - where images are stored and shared: Docker Hub, AWS ECR, GitHub Container Registry
  • Volume - storage that survives when a container is removed (for databases)

Install

Install Docker Desktop on Windows (it uses WSL) or macOS, or Docker Engine on Linux (docker.com has the steps for each system). Check it with docker --version.

Running containers

Example
docker run hello-world                   # downloads a tiny test image and runs it
docker run -d --name web -p 8080:80 nginx
#   -d       run in the background
#   --name   a name for the container
#   -p 8080:80   your computer's port 8080 -> the container's port 80
# now open http://localhost:8080

docker ps                    # running containers
docker logs web              # what it printed
docker exec -it web bash     # open a terminal inside it (type exit to leave)
docker stop web && docker rm web

A database in one command

Example
docker run -d --name shopdb -p 5432:5432 \
  -e POSTGRES_PASSWORD=dev-only-password \
  -v shopdata:/var/lib/postgresql/data \
  postgres:17

You get a real PostgreSQL database for development without installing it. The -v volume keeps the data even if you delete the container.

Containers share the host's Linux kernel, so they start in a second and use far less memory than a full virtual machine.
Key points
  • An image is a package; a container is a running image.
  • docker run starts containers; -p maps ports.
  • Volumes keep data when containers are removed.

📝 Test yourself

1. What is a container?
2. What does -p 8080:80 mean?
3. Fill in the command that lists running containers:
docker