Docker Containers
- A Docker container is a running instance of a Docker image. It is an isolated process (or group of processes) running on the host kernel, with its own filesystem, network, and process tree.
- Containers are ephemeral by default - all data written inside the container’s filesystem is lost when the container is removed. Use volumes or bind mounts to persist data.
- Multiple containers can be created from the same image simultaneously - each gets its own isolated writable layer.
Container Lifecycle
Section titled “Container Lifecycle”docker pull → docker create → docker start → [running] → docker stop → docker rm ↑ docker restart# Pull an image and run a container (combined)docker run nginx # Foreground, blocks terminaldocker run -d nginx # Detached (background)docker run -d --name my-nginx nginx # Named containerdocker run --rm nginx # Auto-remove container on exit (keeps things clean)
# Run interactively (get a shell — combine with --rm to avoid leaving shells behind)docker run -it --rm ubuntu bash
# Map ports: host:containerdocker run -d -p 8080:80 nginx # Access nginx at localhost:8080
# Set environment variablesdocker run -d -e DB_HOST=db my-app
# List running containersdocker ps
# List all containers including stoppeddocker ps -a
# Stop a container (sends SIGTERM, waits 10s, then SIGKILL)docker stop my-nginx
# Force kill immediatelydocker kill my-nginx
# Remove a stopped containerdocker rm my-nginx
# Stop and remove in one stepdocker rm -f my-nginx
# Remove ALL stopped containers at oncedocker container pruneInspecting a Running Container
Section titled “Inspecting a Running Container”# View container logsdocker logs my-nginxdocker logs -f my-nginx # Follow (tail -f equivalent)docker logs --tail 50 my-nginx # Last 50 lines
# Execute a command in a running containerdocker exec -it my-nginx bash # Open interactive shelldocker exec my-nginx ls /etc/nginx
# View resource usage (CPU, memory, network)docker stats
# Inspect container metadata (IP, mounts, env vars, etc.)docker inspect my-nginxContainer Filesystem
Section titled “Container Filesystem”-
Container filesystem is built from the image’s read-only layers + a thin read-write layer on top.
-
Any file modifications during runtime go into the writable layer and are scoped to that container instance.
-
Changes are not reflected in the image. To persist changes: either mount a volume, or create a new image with
docker commit(avoid in production - use a Dockerfile instead).Terminal window # Copy files into/out of a containerdocker cp my-nginx:/etc/nginx/nginx.conf ./nginx.confdocker cp ./nginx.conf my-nginx:/etc/nginx/nginx.conf# See what changed from the base image (A=added, C=changed, D=deleted)docker diff my-nginx
Common Gotchas
Section titled “Common Gotchas”- Containers don’t start after
docker run: Checkdocker ps -afor the exit status, thendocker logs <container>for the error. The most common cause is the ENTRYPOINT/CMD crashing immediately. - Port not accessible: Verify you used
-p host:containerand the container is actually running (docker ps, notdocker ps -a). - Data lost after
docker rm: Expected. Mount a volume if you need persistence. docker stopis slow: The app isn’t handlingSIGTERM. Fix signal handling in your app or Dockerfile (use exec form forCMD).- Container can’t reach the internet: DNS inside containers defaults to
8.8.8.8. Custom DNS servers on the host don’t automatically apply inside containers.