๐ณ Day 6: Docker Container restart kill stop rename pause unpause port diff top stats attach Commands - With Examples!

Table of contents
- ๐ฌ Video Demonstration
- ๐ 1. Docker start/stop
- ๐ 2. Docker restart
- ๐ 3. Docker kill
- ๐ 4. Docker rename
- ๐ 5. Docker pause
- ๐ 6. Docker unpause
- ๐ 7. Docker port
- ๐ 8. Docker diff
- ๐ 9. Docker top
- ๐ 10. Docker stats
- ๐ฅ๏ธ 11. Docker attach
- ๐ Docker attach vs Docker exec
- โ Best Practices
- ๐ Additional Resources

Welcome to another Docker tutorial! Today we dive into restart kill stop rename pause unpause port diff top stats attach that every DevOps engineer should know. These tools help you stop, restart, rename, pause, and monitor containers effectively.
๐ฌ Video Demonstration
๐ 1. Docker start/stop
Gracefully stops a container (sends SIGTERM, then SIGKILL after timeout).
docker stop ubuntu
docker stop alpine ubuntu
docker stop $(docker ps -q)
โก๏ธ Use Case: Shut down containers safely for maintenance.
๐ 2. Docker restart
Restarts a container (stops and starts it again).
docker restart ubuntu
docker restart alpine ubuntu
docker restart $(docker ps -q)
docker restart $(docker ps -aq)
โก๏ธ Use Case: Refresh a container after config changes.
๐ 3. Docker kill
Forcibly stops a container by sending a SIGKILL. Not a best practices
docker kill alpine
docker kill alpine ubuntu
docker kill $(docker ps -q)
โก๏ธ Use Case: Terminate an unresponsive container immediately.
๐ 4. Docker rename
Renames an existing container.
docker rename <old_name> <new_name>
docker rename alpine alpine1
docker rename alpine1 alpine
โก๏ธ Use Case: Give containers meaningful names after creation.
๐ 5. Docker pause
Pauses all processes in a container using cgroups freezer.
docker pause <container_name_or_id>
docker pause alpine
docker pause alpine ubuntu
docker pause $(docker ps -q)
โก๏ธ Use Case: Temporarily halt CPU processing without stopping the container.
๐ 6. Docker unpause
Resumes a paused container.
docker unpause <container_name_or_id>
docker unpause alpine
docker unpause alpine ubuntu
docker unpause $(docker ps -q)
โก๏ธ Use Case: Resume a paused container back to normal execution.
๐ 7. Docker port
Displays the public-facing ports and their mappings to the container's internal ports.
docker port <container_name_or_id>
docker port um
โก๏ธ Use Case: Identify which host ports map to container ports.
๐ 8. Docker diff
Shows changes made to a containerโs filesystem since it started.
docker diff <container_name_or_id>
docker diff alpine
docker diff um
โก๏ธ Use Case: Inspect what has been added, changed, or deleted. After container creation.
๐ 9. Docker top
Displays the running processes inside a container.
docker top <container_name_or_id>
docker top alpine
โก๏ธ Use Case: Inspect whatโs running in a container like ps
on Linux.
๐ 10. Docker stats
Live stream of resource usage (CPU, memory, network) by containers.
docker stats
docker stats ubuntu
โก๏ธ Use Case: Monitor container performance in real-time. ๐ฅ๏ธ Tip: Use docker stats <container_name>
to focus on a specific container.
๐ฅ๏ธ 11. Docker attach
Connects your terminal to a running containerโs standard input/output streams of PID 1. It connect to the main process of the container.
docker attach nd
http://192.168.1.22:8083/
use Ctrl + P + Q to detach
โก๏ธ Use Case: Interact with a running container as if you're inside it (e.g., for debugging or running commands).
Great question! docker attach
and docker exec
are both used to interact with running containers, but they serve different purposes and behave differently.
๐ Docker attach
vs Docker exec
Feature | docker attach | docker exec |
Purpose | Connect to the main process of the container | Run a new command inside the container |
Interaction | Connects to stdin, stdout, stderr of PID 1 | Starts a separate process inside the container |
Effect | Feels like youโre inside the containerโs main terminal | Just runs a single command (e.g., bash , top ) |
Multiple Sessions | Not ideal for multiple users (shared terminal) | Safe to use multiple times concurrently |
Exit Behavior | Detaching can stop the container if not run with -it | Command ends, container keeps running |
Use Case | Debugging the main app process | Running one-off commands, inspecting container |
Detach Option | Can use Ctrl + P + Q to detach | Not needed โ just exit the command |
โ Example Usages:
Attach to a container's main process:
docker attach my_container
Run a shell inside a container (without stopping the app):
docker exec -it my_container bash
๐ฅ Tip:
Use
docker exec
when you want safe, isolated interaction (like debugging or running a command).Use
docker attach
only if you really need to tap into the main running process (e.g., when itโs a shell or logging service).
โ Best Practices
โ Use
docker stop
beforedocker kill
for graceful shutdown.โ Use
docker pause
only for short periods.โ Monitor containers regularly with
docker stats
.โ Always name your containers meaningfully (use
docker rename
).
๐ Additional Resources
๐ Docker CLI Docs
๐ณ Docker Hub
Subscribe to my newsletter
Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ibrar Ansari
Ibrar Ansari
Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!