Docker & Containers

Demystifying Images & Containers

Lesson 2 of 9 · 7 min · Beginner

To master Docker, you must understand its two fundamental building blocks: the static blueprints that define your application, and the dynamic, running environments they spawn.

Docker Images

A Docker image is like a blueprint. It contains absolutely everything needed to run an application without relying on the host system's configuration.

Read-Only & Layered

Images are completely read-only and built in stacked layers. This layered approach makes them fast to pull, incredibly efficient to store, and highly cacheable.

What's Inside?

An image packs OS libraries (like Debian/Alpine), your specific runtime (Java, Python, PHP), your application code, and your environment configurations.

Docker Containers

Container Lifecycle

  • start Spawns the container, attaching a read-write layer over the image.
  • stop Gracefully halts the running processes inside the container.
  • restart Stops and immediately starts the container back up.
  • delete Destroys the container and its temporary read-write data layer.

A container is a running instance of an image. When you start an image, Docker creates a container and adds a writable layer on top, bringing the application to life.

Essential CLI Operations

Understanding the difference between create, start, and run is crucial. run is simply a combination of create (making the container) followed immediately by start (executing it).

terminal

# Image Commands
$ docker image ls        → List all images
$ docker pull ubuntu     → Download an image from Hub
$ docker history ubuntu  → View build layers
$ docker rmi ubuntu      → Remove an image

# Container Run Commands
$ docker create ubuntu   → Creates, but does NOT start
$ docker start <id>      → Starts a created container
$ docker run -d nginx    → Create + Start in background

# Container Management
$ docker ps              → List running containers
$ docker ps -a           → List all (running + stopped)
$ docker logs <id>       → View container logs
$ docker exec -it <id> /bin/bash → Open interactive shell
$ docker stop <id>       → Stop a container
$ docker rm <id>         → Remove a container

The Key Differences

AttributeDocker Image Docker Container
StateStatic, read-onlyRunning, read-write layer added
PurposeBlueprint / TemplateActive Execution Environment
Real-Life AnalogyCake Recipe / App Installer FileBaked Cake / Installed App running
RelationshipYou need 1 Image......to spin up N Containers