How do you list and manage Docker containers?
Answer
Essential Docker container management commands: Listing: docker ps — list running containers (shows ID, image, command, created, status, ports, name); docker ps -a / --all — list all containers including stopped ones; docker ps -q — only container IDs (useful for scripting). Logs: docker logs mycontainer — view container logs; docker logs -f mycontainer — follow logs (tail); docker logs --tail 100 mycontainer — last 100 lines; docker logs --since 2h mycontainer — logs from last 2 hours. Inspect: docker inspect mycontainer — full JSON configuration including IPs, volumes, env vars, labels. Resource usage: docker stats — live CPU, memory, network, disk I/O for all running containers; docker stats mycontainer — for specific container. Removing: docker rm mycontainer — remove a stopped container; docker rm -f mycontainer — force remove running container; docker container prune — remove all stopped containers. Restart: docker restart mycontainer. Rename: docker rename old_name new_name. Copy files: docker cp mycontainer:/app/log.txt ./log.txt — copy from container to host.