Docker Images
Images are read-only templates for creating containers. Think of them as classes — containers are instances.
Pulling Images
# Pull an image from Docker Hub
docker image pull ubuntu:latest
docker image pull nginx:1.25
docker image pull python:3.11-slim
# List local images
docker image lsOutput columns:
| Column | Meaning |
|---|---|
| REPOSITORY | Image name |
| TAG | Version label (default: latest) |
| IMAGE ID | Unique hash identifier |
| SIZE | Disk space used |
Image Tags
Tags differentiate versions of the same image:
docker image pull python:3.11 # Full Python 3.11
docker image pull python:3.11-slim # Smaller variant
docker image pull python:3.11-alpine # Minimal variantRunning Containers
Basic Run
# Run and exit
docker container run hello-world
# Run interactively (-it)
docker container run -it ubuntu:latest bash
# You're now INSIDE the container
# Type 'exit' to leaveDetached Mode
Run containers in the background:
# Run nginx in background
docker container run -d --name my-web nginx:latest
# Check running containers
docker container lsNaming Containers
# Auto-generated names (adjective_scientist)
docker container run -d nginx
# Custom name
docker container run -d --name my-app nginxEnvironment Variables
Pass configuration to containers:
docker container run -d \
--name my-db \
-e MYSQL_ROOT_PASSWORD=secret123 \
-e MYSQL_DATABASE=webapp \
mysql:8.0Auto-Remove on Stop
docker container run --rm -it ubuntu bash
# Container is deleted when you exitContainer Lifecycle
# List running containers
docker container ls
# List ALL containers (including stopped)
docker container ls -a
# Stop a container
docker container stop my-web
# Start a stopped container
docker container start my-web
# Restart
docker container restart my-web
# Remove a stopped container
docker container rm my-web
# Force remove (even if running)
docker container rm -f my-webViewing Logs
# View container output
docker container logs my-web
# Follow logs in real-time
docker container logs -f my-web
# Last 50 lines
docker container logs --tail 50 my-webInspecting Containers
# Full JSON details
docker container inspect my-web
# Find IP address
docker container inspect my-web | grep IPAddress
# View resource usage
docker container statsExecuting Commands in Running Containers
# Run a command inside a running container
docker container exec my-web ls /usr/share/nginx/html
# Open a shell in a running container
docker container exec -it my-web bashCleanup Commands
# Remove all stopped containers
docker container prune
# Remove unused images
docker image prune
# Nuclear option: remove everything unused
docker system prune -aCommand Summary
| Command | Purpose |
|---|---|
docker image pull | Download an image |
docker image ls | List local images |
docker container run | Create and start a container |
docker container ls | List running containers |
docker container stop | Stop a container |
docker container rm | Remove a container |
docker container logs | View container output |
docker container exec | Run command in container |
docker container inspect | View container details |
Summary
- Images are templates; containers are running instances
-druns detached,-itruns interactive,--rmauto-removes-epasses environment variables,--namesets a custom namedocker container ls -ashows all containers including stoppeddocker container exec -it <name> bashopens a shell inside
Next Steps
Next, we'll cover container networking — port forwarding, Docker networks, and container-to-container communication.