🚀 Day 22: Docker Cleanup & Container Management Commands

Ritesh SinghRitesh Singh
3 min read

Welcome to Day 22 of my #100DaysOfDevOps journey! Today, I focused on mastering some essential Docker housekeeping commands to manage containers, images, volumes, and perform clean-up tasks effectively.


🧠 Concepts Covered

  • Listing running and stopped containers

  • Removing stopped containers and unused images

  • Managing Docker volumes

  • Executing commands inside running containers

  • Understanding how Docker resources accumulate over time and how to keep your environment clean


🔧 Docker Commands Practiced

CONTAINER

🔍 List Containers

docker ps              # Lists only running containers
docker ps -a           # Lists all containers (including stopped ones)

🧹 Remove Containers

docker rm <container_id>   # Removes a specific container by ID or name

💡 Tip: Use docker ps -a to find stopped containers you no longer need.


Create & run a new container

docker run <image_name>

//if image not available locally, it’ll be downloaded from DockerHub

Run container in background

docker run -d <image_name>

Run container with custom name

docker run - -name <container_name> <image_name>

IMAGES

📦 List Docker Images

docker images

🧽 Remove Docker Images

docker rmi <image_id>      # Removes an image by its ID

💡 Useful for deleting old or unused images to free up disk space.

Remove unused images

docker image prune

Build an image from a Dockerfile

docker build -t <image_name>:<version> . //version is optional
docker build -t <image_name>:<version> . -no-cache //buid withot cache

Port Binding in container

docker run -p<host_port>:<container_port> <image_name>

Set environment variables in a container

docker run -e <var_name>=<var_value> <container_name> (or <container_id)

Start or Stop an existing container

docker start|stop <container_name> (or <container_id)

Inspect a running container

docker inspect <container_name> (or <container_id)

TROUBLESHOOT

Fetch logs of a container

docker logs <container_name> (or <container_id)

Open shell inside running container

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

DOCKERHUB

Pull an image from DockerHub

docker pull <image_name>

Publish an image to DockerHub

Docker push <username>/<image_name>

Login into DockerHub

docker login -u <image_name>
Or
docker login
//also, docker logout to remove credentials

Search for an image on DockerHub

docker search <image_name>

VOLUME

📋 List All Volumes

docker volume ls

🆕 Create a New Named Volume

docker volume create <volume_name>

❌ Delete a Named Volume

docker volume rm <volume_name>

📂 Mounting Volumes

🔗 Mount a Named Volume to a Running Container

docker run --volume <volume_name>:<mount_path>

# Or using --mount
docker run --mount type=volume,src=<volume_name>,dst=<mount_path>

❓ Mount an Anonymous Volume with running container

docker run --volume <mount_path>

📎 Create a Bind Mount

docker run --volume <host_path>:<container_path>

# Or using --mount
docker run --mount type=bind,src=<host_path>,dst=<container_path>

🧹 Remove Unused Local Volumes

docker volume prune

📝 Note: Use this to remove anonymous volumes that are no longer used.


🌐 Docker Networks – Quick Reference

📋 List All Networks

docker network ls

🛠️ Create a Network

docker network create <network_name>

❌ Remove a Network

docker network rm <network_name>

Remove all unused networks

docker network prune

⚠️ Warning: This will remove all unused networks. Make sure no containers depend on them.


🔗 Connect with Me


0
Subscribe to my newsletter

Read articles from Ritesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritesh Singh
Ritesh Singh

Hi, I’m Ritesh 👋 I’m on a mission to become a DevOps Engineer — and I’m learning in public every single day.With a full-time commitment of 8–10 hours daily, I’m building skills in: ✅ Linux✅ Git & GitHub✅ Docker & Kubernetes✅ AWS EC2, S3✅ Jenkins, GitHub Actions✅ Terraform, Prometheus, Grafana I post daily blogs on Hashnode, push projects to GitHub, and stay active on LinkedIn and Twitter/X. Let’s connect, collaborate, and grow together 🚀 #100DaysOfDevOps #LearningInPublic #DevOps