## ๐ Author
Birat Aryal โ birataryal.github.io
Created Date: 2025-06-18
Updated Date: Tuesday 17th June 2025 21:59:04
Website - birataryal.com.np
Repository - Birat Aryal
LinkedIn - Birat Aryal
DevSecOps Engineer | System Engineer | Cyber Security Analyst | Network Engineer
๐ Running and Managing Docker Containers
This document provides essential commands and practices to run, manage, and troubleshoot Docker containers effectively.
๐ Running Containers
Run a Container from an Image
docker run -d --name my-nginx -p 8080:80 nginx
-
-d: Detached mode (runs in background) -
--name: Assigns a name to the container -
-p: Maps port 8080 on host to port 80 in container
Run with Interactive Terminal
docker run -it ubuntu /bin/bash
-it: Interactive terminal access
Run and Remove After Exit
docker run --rm alpine echo "Hello, Docker!"
๐ Managing Containers
List All Running Containers
docker ps
List All Containers (including stopped)
docker ps -a
Start, Stop, and Restart Containers
docker start <container_id_or_name>
docker stop <container_id_or_name>
docker restart <container_id_or_name>
View Logs
docker logs <container_name>
Execute Command Inside Container
docker exec -it <container_name> /bin/bash
๐งผ Clean Up
Remove a Container
docker rm <container_id_or_name>
Remove All Stopped Containers
docker container prune
Remove All Unused Resources
docker system prune -a
โ ๏ธ Use with caution โ this will remove unused containers, networks, volumes, and images.
๐งช Inspect and Debug
Inspect Container Details
docker inspect <container_name>
Resource Usage (CPU, memory, etc.)
docker stats
Network Inspection
docker network ls
docker network inspect <network_name>
๐ Tips
-
Name containers meaningfully for easier management
-
Use
docker-composefor multi-container apps -
Monitor logs and resource usage for performance issues
-
Tag and version your images clearly