Working with Containers & Images

30 minLesson 2 of 5

Learning Objectives

  • Pull and manage Docker images
  • Run containers in interactive and detached modes
  • Start, stop, restart, and remove containers
  • Pass environment variables and name containers
  • Inspect container and image properties

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 ls

Output columns:

ColumnMeaning
REPOSITORYImage name
TAGVersion label (default: latest)
IMAGE IDUnique hash identifier
SIZEDisk 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 variant

Running 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 leave

Detached Mode

Run containers in the background:

# Run nginx in background
docker container run -d --name my-web nginx:latest
 
# Check running containers
docker container ls

Naming Containers

# Auto-generated names (adjective_scientist)
docker container run -d nginx
 
# Custom name
docker container run -d --name my-app nginx

Environment Variables

Pass configuration to containers:

docker container run -d \
  --name my-db \
  -e MYSQL_ROOT_PASSWORD=secret123 \
  -e MYSQL_DATABASE=webapp \
  mysql:8.0

Auto-Remove on Stop

docker container run --rm -it ubuntu bash
# Container is deleted when you exit

Container 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-web

Viewing 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-web

Inspecting Containers

# Full JSON details
docker container inspect my-web
 
# Find IP address
docker container inspect my-web | grep IPAddress
 
# View resource usage
docker container stats

Executing 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 bash

Cleanup Commands

# Remove all stopped containers
docker container prune
 
# Remove unused images
docker image prune
 
# Nuclear option: remove everything unused
docker system prune -a

Command Summary

CommandPurpose
docker image pullDownload an image
docker image lsList local images
docker container runCreate and start a container
docker container lsList running containers
docker container stopStop a container
docker container rmRemove a container
docker container logsView container output
docker container execRun command in container
docker container inspectView container details

Summary

  • Images are templates; containers are running instances
  • -d runs detached, -it runs interactive, --rm auto-removes
  • -e passes environment variables, --name sets a custom name
  • docker container ls -a shows all containers including stopped
  • docker container exec -it <name> bash opens a shell inside

Next Steps

Next, we'll cover container networking — port forwarding, Docker networks, and container-to-container communication.