These commands assume Docker Engine and the Compose plugin are installed. In the examples, web is a service name; replace <container> and <image> with real names or IDs.
Docker: container lifecycle
docker ps # List running containers
docker ps -a # List all containers, including stopped ones
docker run --name demo -d -p 8080:80 nginx # Run in background and publish a port
docker stop demo # Stop a container
docker start demo # Start a stopped container
docker restart demo # Restart a container
docker rm demo # Remove a stopped container
docker logs --tail=100 -f demo # Follow the latest logs
docker exec -it demo sh # Open a shell (use bash if the image includes it)
-p host-port:container-port, such as -p 8080:80, maps host port 8080 to container port 80. Before publishing a port on a public server, confirm who should be able to reach it and check the firewall rules.
Docker: images and resources
docker images # List local images
docker pull nginx:latest # Pull an image
docker build -t my-app:dev . # Build from the current directory
docker image inspect nginx # Inspect image details
docker stats # Show container resource usage
docker system df # Show Docker disk usage
Docker Compose: everyday workflow
Change to the project directory containing compose.yaml (or the compatible docker-compose.yaml filename):
docker compose config # Merge and validate configuration
docker compose up -d --build # Build and start in the background
docker compose ps # Show service status
docker compose logs -f --tail=100 # Follow logs for all services
docker compose logs -f web # Follow only the web service
docker compose exec web sh # Run a command in the running web service
docker compose restart web # Restart the web service
docker compose stop # Stop services but keep containers
docker compose start # Start stopped services
docker compose down # Stop and remove this project's containers and network
After changing configuration or an image
docker compose up -d --build
To rebuild only one service, specify its name:
docker compose up -d --build web
Review the impact before cleanup
docker compose down # Keeps named volumes by default
docker image prune # Remove dangling unused images
docker system df # Check disk use before cleanup
Be careful with docker compose down -v, docker volume prune, and docker system prune -a --volumes. They can delete volumes that hold databases or uploaded files, making data unrecoverable. Before cleanup on production, verify backups, volume ownership and the exact scope. Do not treat commands with -v as a routine restart.
Minimal Compose example
services:
web:
image: nginx:alpine
ports:
- "8080:80"
restart: unless-stopped
Save it as compose.yaml. From the same directory, run docker compose up -d, then inspect it with docker compose ps and docker compose logs -f web.