Skip to content

## ๐Ÿ“ 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

Bash
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

Bash
docker run -it ubuntu /bin/bash
  • -it: Interactive terminal access

Run and Remove After Exit

Bash
docker run --rm alpine echo "Hello, Docker!"

๐Ÿ“‹ Managing Containers

List All Running Containers

Bash
docker ps

List All Containers (including stopped)

Bash
docker ps -a

Start, Stop, and Restart Containers

Bash
docker start <container_id_or_name>
docker stop <container_id_or_name>
docker restart <container_id_or_name>

View Logs

Bash
docker logs <container_name>

Execute Command Inside Container

Bash
docker exec -it <container_name> /bin/bash

๐Ÿงผ Clean Up

Remove a Container

Bash
docker rm <container_id_or_name>

Remove All Stopped Containers

Bash
docker container prune

Remove All Unused Resources

Bash
docker system prune -a

โš ๏ธ Use with caution โ€“ this will remove unused containers, networks, volumes, and images.


๐Ÿงช Inspect and Debug

Inspect Container Details

Bash
docker inspect <container_name>

Resource Usage (CPU, memory, etc.)

Bash
docker stats

Network Inspection

Bash
docker network ls
docker network inspect <network_name>

๐Ÿ“Œ Tips

  • Name containers meaningfully for easier management

  • Use docker-compose for multi-container apps

  • Monitor logs and resource usage for performance issues

  • Tag and version your images clearly