DEV Community

Cover image for Docker Images vs Containers — Understanding the Core Difference
DevOps Rise
DevOps Rise

Posted on

Docker Images vs Containers — Understanding the Core Difference

Images and containers are the two most fundamental concepts in Docker. Many beginners confuse them. Once you understand the difference, everything else in Docker makes sense.


The Simple Analogy

A Docker image is like a recipe (or a class in programming). It defines everything — the base OS, packages to install, files to copy, command to run.

A Docker container is like a dish cooked from that recipe (or an instance of a class). It is the running thing that actually exists and does work.

One recipe, many dishes. One image, many containers.


Docker Images

# Download an image from Docker Hub
docker pull ubuntu:22.04

# List images on your machine
docker images
# REPOSITORY   TAG      IMAGE ID       SIZE
# ubuntu       22.04    a6be9eb88d21   77.8MB
# nginx        latest   6efc10a0510f   142MB

# See each layer and its size
docker history nginx

# Remove an image
docker rmi ubuntu:22.04

# Remove all unused images
docker image prune
Enter fullscreen mode Exit fullscreen mode

Docker Containers

# Create and start a container
docker run -d --name web-server nginx

# List running containers
docker ps
# CONTAINER ID   IMAGE   STATUS          NAMES
# a1b2c3d4e5f6   nginx   Up 2 minutes    web-server

# List ALL containers (including stopped)
docker ps -a

# Stop it
docker stop web-server

# Start it again
docker start web-server

# Remove a container
docker rm web-server

# Remove a running container forcefully
docker rm -f web-server
Enter fullscreen mode Exit fullscreen mode

The Layer System

FROM ubuntu:22.04     # Layer 1: base Ubuntu (77MB)
RUN apt update        # Layer 2: package update
RUN apt install nginx # Layer 3: nginx installation
COPY index.html /var/www/html  # Layer 4: your file
Enter fullscreen mode Exit fullscreen mode

When you run a container, Docker adds a 5th writable layer on top.

Key insight: 10 containers from the same image all SHARE the read-only image layers. Each container only needs its own tiny writable layer.

This is why containers are so lightweight!


Container Lifecycle

# 1. Create (not running yet)
docker create --name myapp nginx

# 2. Start
docker start myapp

# 3. Or do both with run
docker run -d --name myapp2 nginx

# 4. Pause
docker pause myapp
docker unpause myapp

# 5. Stop
docker stop myapp

# 6. Remove
docker rm myapp

# Quick: stop and remove together
docker rm -f myapp

# Remove all stopped containers
docker container prune
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

Image = blueprint, read-only, stored on disk

Container = running instance, has writable layer

✅ Many containers can run from one image

✅ Container writable layer is lost when container is removed

✅ Use volumes for data that needs to persist


Practice 535+ real DevOps interview questions at devopsrise.vercel.app — completely free.

Top comments (0)