Running Stopping and Removing Containers

Managing containers is a daily task in Docker. You start them, stop them, restart them, and clean them up. This topic covers every essential container management command with clear examples so you feel comfortable in any situation.

The Container Lifecycle

docker create → docker start → docker pause → docker unpause
                    ↓                              ↓
                Running ──────────────────────── Running
                    ↓
               docker stop (graceful, SIGTERM → 10s → SIGKILL)
               docker kill (immediate, SIGKILL)
                    ↓
                 Stopped
                    ↓
               docker rm
                    ↓
                  Gone

Starting Containers

Run a new container (creates and starts in one step):

docker run nginx

Run in the background (detached):

docker run -d nginx

Run with a name, port mapping, and detached:

docker run -d --name web -p 8080:80 nginx

Start a container that already exists but is stopped:

docker start web

Restart a running or stopped container:

docker restart web

Stopping Containers — Graceful vs Forced

The docker stop command sends a SIGTERM signal to the container's main process. This gives the application a chance to finish what it is doing and shut down cleanly. If the app does not stop within 10 seconds, Docker forces it to stop with SIGKILL.

docker stop web               ← waits up to 10 seconds for graceful shutdown
docker stop --time=30 web     ← waits up to 30 seconds before forcing
docker kill web               ← immediate forced stop, no grace period

Use docker stop for normal shutdowns. Use docker kill only when a container is completely frozen and unresponsive.

Removing Containers

A stopped container still occupies disk space with its writable layer. Remove it to free up space:

docker rm web                 ← remove stopped container named "web"
docker rm -f web              ← force remove even if running (stop + remove)

Remove all stopped containers at once:

docker container prune

Run a container and auto-delete it when it stops (useful for one-off tasks):

docker run --rm ubuntu echo "Hello and goodbye"

Inspecting Running Containers

See all running containers:

docker ps

See all containers (running + stopped):

docker ps -a

Get detailed JSON information about a container:

docker inspect web

See real-time resource usage (like Task Manager for containers):

docker stats
docker stats web              ← stats for one container
CONTAINER ID   NAME  CPU %   MEM USAGE / LIMIT   MEM %   NET I/O
a1b2c3d4e5f6   web   0.10%   5.3MiB / 7.7GiB     0.07%   1.2kB / 0B

Getting Inside a Running Container

Execute a command inside a running container:

docker exec web ls /etc/nginx
docker exec web cat /etc/nginx/nginx.conf

Open an interactive shell inside a running container:

docker exec -it web bash      ← use bash if available
docker exec -it web sh        ← use sh for minimal images (like alpine)

You are now inside the container. Explore files, check processes, run debug commands. Type exit to leave without stopping the container.

Copying Files Between Host and Container

Copy from host to container:
docker cp ./config.conf web:/etc/nginx/conf.d/config.conf

Copy from container to host:
docker cp web:/var/log/nginx/access.log ./access.log

Cleaning Up Everything

Remove all stopped containers:
docker container prune

Remove all unused images:
docker image prune

Remove unused networks:
docker network prune

Nuclear option — remove everything unused:
docker system prune

With volumes too (use carefully!):
docker system prune --volumes

Key Points

  • Use docker run to create and start, docker start to start an existing stopped container.
  • Use docker stop for graceful shutdown; docker kill for immediate force-stop.
  • Use docker exec -it to open a shell inside a running container for debugging.
  • Use docker system prune periodically to reclaim disk space from unused resources.

Leave a Comment