iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

stop / start / rm

docker stop, kill, rm, prune: cleanly ending containers and reclaiming disk + memory.

Docker — stop, kill, rm, prune

EXAMPLE
# ===== Stop =====
docker stop <name>           # SIGTERM, then SIGKILL after 10s (default)
docker stop -t 30 <name>     # 30s grace period
docker stop $(docker ps -q)  # all running

# ===== Kill (force) =====
docker kill <name>           # SIGKILL immediately
docker kill -s SIGUSR1 <name> # custom signal

# ===== Remove =====
docker rm <name>              # remove stopped container
docker rm -f <name>           # force stop + remove
docker rm -v <name>           # also remove anonymous volumes
docker rm $(docker ps -aq)    # all containers

# Cleanup all stopped:
docker container prune

# ===== Inspect first if unsure =====
docker ps -a --filter status=exited
docker inspect <name> | jq '.[0].State'

# ===== Images =====
docker images
docker rmi <image>            # remove an image
docker rmi -f <image>         # force; ignores child references

# Cleanup unused:
docker image prune            # untagged + dangling
docker image prune -a         # all images not used by a container

# ===== Volumes =====
docker volume ls
docker volume rm <name>
docker volume prune           # unused volumes

# ===== Networks =====
docker network ls
docker network rm <name>
docker network prune          # unused networks

# ===== System-wide cleanup =====
docker system df              # disk usage by category
docker system prune           # stopped containers + unused networks + dangling images
docker system prune -a        # PLUS unused images (more aggressive)
docker system prune --volumes # PLUS unused volumes (most aggressive)

# Specify time:
docker system prune --filter 'until=72h'   # only items older than 72h

# ===== Logs cleanup =====
# Container logs grow without bound by default. Configure log rotation:
docker run --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 ...

# Truncate existing container logs:
echo "" | sudo tee $(docker inspect --format='{{.LogPath}}' <name>)

# ===== Auto-clean for development =====
# Use --rm on one-shots:
docker run --rm -it ubuntu bash

# Compose:
docker compose down       # stop + remove containers + networks
docker compose down -v    # also remove volumes (data loss!)

# ===== Graceful shutdown patterns =====
# Your container should handle SIGTERM and shut down cleanly within the grace period.
# Example Node.js:
process.on('SIGTERM', async () => {
  await server.close();
  process.exit(0);
});

# ===== Patterns to internalise =====
# - --rm for ephemeral runs (no leftover containers)
# - docker compose down for whole-stack teardown
# - Periodic 'docker system prune' on dev machines
# - Log rotation on every long-running container

# ===== Pitfalls =====
# - 'docker rm -f' loses any unsaved state in the container
# - 'docker system prune --volumes' deletes named volumes too (data loss)
# - Slow SIGTERM handlers -> SIGKILL after grace period; data corruption possible
# - Forgetting to remove volumes when removing containers (orphaned volumes)

Why it matters

Stop / kill / rm / prune is the container lifecycle. SIGTERM with grace period gives clean shutdowns; prune reclaims disk. Use --rm on ephemerals, compose down for stacks, and log rotation on anything that runs long. The cleanest dev machines run prune weekly.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
docker stop api
docker start api
docker rm  api
docker rm  -f api
Try it Yourself »

Discussion

Loading…