Docker Containers
Run, manage, and debug Docker containers with ports, volumes, and environment variables.
What you'll learn
- Running containers with docker run and key flags
- Mapping ports and mounting volumes
- Passing environment variables to containers
- Inspecting and debugging running containers
Concept
Running Containers
The docker run command starts a container from an image. The -d flag runs it in the background (detached). --name gives the container a readable name. -p 8080:80 maps port 8080 on your host to port 80 in the container — this is how you make a containerized web server accessible from outside. Without port mapping, the container's port is only reachable from inside the Docker network.
Volumes
Containers are ephemeral — when a container is removed, any data written inside it is lost. Volumes solve this by storing data on the host filesystem, managed by Docker. Use -v mydata:/data to mount a named volume, or -v /host/path:/container/path to mount a specific host directory. Volumes persist across container restarts and removals, making them essential for databases and any stateful application.
Environment Variables
You pass configuration to containers with the -e flag: docker run -e NODE_ENV=production myapp. For many variables, use --env-file .env to load from a file. Inside the container, the application reads these variables the same way it reads any environment variable. This is the standard pattern for configuring containerized applications without baking secrets or environment-specific values into the image.
Inspecting and Debugging
docker ps lists running containers. docker logs <container> shows the container's stdout and stderr. docker exec -it <container> bash opens an interactive shell inside a running container — invaluable for debugging. docker inspect <container> returns detailed JSON information about the container's configuration, network, and mounts. docker stats shows real-time resource usage.
Container Networking
By default, Docker creates a bridge network for containers. Containers on the same network can communicate using their names as hostnames. Port mapping (-p host:container) exposes a container port to the host. User-defined networks provide better isolation and DNS resolution. For multi-container apps, Docker Compose automatically creates a network and connects all services. Understanding networking is essential for connecting your application to databases, caches, and external services in a containerized environment.
Example
# Run a web server with port mapping
docker run -d --name web -p 8080:80 nginx:alpine
# Run with volumes and environment variables
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
-p 5432:5432 \
postgres:16
# Inspect and debug
docker ps # list running containers
docker logs -f web # follow container logs
docker exec -it web sh # open a shell inside
docker inspect db | head -50 # view container details
docker stats # real-time resource usage
docker run starts containers with port mapping (-p), volumes (-v), and env vars (-e). docker exec opens a shell for debugging, and docker stats shows resource usage.
Try it
- Docker Compose Generator
Generate compose files for multi-container setups.
- Env File Generator
Create .env files for container configuration.
- YAML Formatter
Format and prettify YAML configuration files.
Common mistakes
The mistake
Forgetting to map ports when running a container
The fix
Use -p hostPort:containerPort to expose a container's port. Without it, the service is only reachable inside the Docker network, not from your host or browser.
The mistake
Storing persistent data inside the container filesystem
The fix
Use volumes (-v) for any data that must survive container removal. Container filesystems are ephemeral — data is lost when the container is deleted.
Practice Docker Containers
6 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.