Skip to content

Docker - Working With Containers

This chapter explores common commands for managing and interacting with Docker containers throughout their lifecycle.

Provides detailed, low-level information about a container in JSON format. Useful for finding IP addresses, volume mounts, environment variables, port mappings, and more.

# Inspect container by name or ID
docker inspect my-running-container

Displays the running processes inside a specific container, similar to the top command in Linux.

docker top my-running-container

Provides a live stream of resource usage statistics (CPU, Memory, Network I/O, Disk I/O) for one or more running containers.

# Show stats for all running containers
docker stats
# Show stats for specific containers
docker stats container1 container2

Press Ctrl+C to exit the live stream.

Pausing and Unpausing (docker pause, docker unpause)

Section titled “Pausing and Unpausing (docker pause, docker unpause)”

These commands suspend or resume all processes within a container using the cgroups freezer.

# Pause all processes in the container
docker pause my-running-container
# Resume processes in the paused container
docker unpause my-running-container

Pausing is useful for temporarily freeing up resources or for snapshotting container state (though volume data isn’t frozen).

Stopping, Starting, Restarting (docker stop, start, restart, kill)

Section titled “Stopping, Starting, Restarting (docker stop, start, restart, kill)”

These commands manage the running state of containers:

  • docker stop <container>: Sends a SIGTERM signal to the main process, allowing graceful shutdown (waits for a default timeout, then sends SIGKILL).
  • docker kill <container>: Sends a SIGKILL signal immediately, forcefully stopping the container.
  • docker start <container>: Starts one or more stopped containers.
  • docker restart <container>: Stops and then starts a container.
docker stop my-container
docker start my-container

Deletes one or more containers. Containers must be stopped before removal unless the -f (force) flag is used.

# Remove a stopped container
docker rm my-stopped-container
# Stop and remove a running container
docker rm -f my-running-container

Connects your terminal’s standard input, output, and error streams to the main process running inside a container. Useful for interacting with foreground processes.

# Attach to a container running an interactive shell or application
docker attach my-interactive-container

Use Ctrl+P, Ctrl+Q to detach without stopping the container. Note that docker exec is usually preferred for running additional commands or getting a shell in a running container.

A container typically goes through the following states:

  • Created: The container is created (e.g., via docker create or the first step of docker run) but not started.
  • Running: The container is actively executing its main process (docker run, docker start).
  • Paused: All processes within the container are suspended (docker pause).
  • Stopped/Exited: The container’s main process has finished or been terminated (docker stop, or the process completed/crashed).
  • Removing: The container is being deleted (docker rm).
  • Dead: The container cannot be reached by the daemon (rare).

Commands like start, stop, pause, unpause, kill, and restart transition the container between these states, while rm removes it entirely.