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

Ibrar AnsariIbrar Ansari
4 min read

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

Watch on Youtube


๐Ÿ“Œ 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

Featuredocker attachdocker exec
PurposeConnect to the main process of the containerRun a new command inside the container
InteractionConnects to stdin, stdout, stderr of PID 1Starts a separate process inside the container
EffectFeels like youโ€™re inside the containerโ€™s main terminalJust runs a single command (e.g., bash, top)
Multiple SessionsNot ideal for multiple users (shared terminal)Safe to use multiple times concurrently
Exit BehaviorDetaching can stop the container if not run with -itCommand ends, container keeps running
Use CaseDebugging the main app processRunning one-off commands, inspecting container
Detach OptionCan use Ctrl + P + Q to detachNot 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 before docker 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

0
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!