Introduction
Understanding the difference between Docker images and containers is fundamental. An image is a read-only template, while a container is a running instance of an image.
Working with Images
# List local images
docker images
# Pull a specific version
docker pull python:3.11-slim
# Remove an image
docker rmi nginx
# Build an image from Dockerfile
docker build -t myapp:1.0 .
# Tag an image
docker tag myapp:1.0 myapp:latest
Working with Containers
# Run with interactive mode
docker run -it ubuntu bash
# Run in detached mode
docker run -d --name web nginx
# Execute command in running container
docker exec -it web bash
# Copy files to/from container
cp file.txt web:/app/
cp web:/app/log.txt ./
# Inspect container details
docker inspect web
Container Lifecycle
# Start stopped container
docker start web
# Pause and unpause
docker pause web
docker unpause web
# View resource usage
docker stats
# Remove all stopped containers
docker container prune
Summary
Images are blueprints, containers are running instances. Master build, run, exec, and lifecycle commands to work effectively with Docker.
YouTip