You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Two concepts sit at the heart of Docker: images and containers. Understanding the relationship between them is essential before you can use Docker effectively.
A Docker image is a read-only template that contains everything needed to run an application: the operating system base layer, runtime, libraries, application code, and default configuration. Think of an image as a recipe or a class definition — it describes what a container should look like, but it is not itself running.
Images are composed of layers. Each instruction in a Dockerfile adds a new layer on top of the previous one. Layers are cached and shared between images, which is why downloading a second image that shares a base layer with one you already have is much faster than downloading the first.
# List images you have locally
docker images
# Pull an image without running it
docker pull nginx:latest
# Inspect image layers
docker history nginx:latest
# Remove an image
docker rmi nginx:latest
A container is a running instance of an image. When you run an image Docker creates a thin writable layer on top of the read-only image layers and starts the process defined in the image. Multiple containers can be started from the same image simultaneously; each gets its own writable layer and its own isolated process space.
The relationship is analogous to a class and its instances in object-oriented programming: the image is the class, containers are the instances.
# Run a container interactively and remove it when it exits
docker run --rm -it ubuntu:22.04 bash
# Run a container in the background (detached)
docker run -d --name my-nginx -p 8080:80 nginx
# List running containers
docker ps
# List all containers including stopped
docker ps -a
# Stop a running container
docker stop my-nginx
# Remove a stopped container
docker rm my-nginx
# Stop and remove in one command
docker rm -f my-nginx
A container moves through several states:
# Create without starting
docker create --name my-container nginx
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.