Docker - Working With Containers
Docker - Working With Containers
Section titled “Docker - Working With Containers”This chapter explores common commands for managing and interacting with Docker containers throughout their lifecycle.
Inspecting Containers (docker inspect)
Section titled “Inspecting Containers (docker inspect)”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 IDdocker inspect my-running-containerViewing Processes (docker top)
Section titled “Viewing Processes (docker top)”Displays the running processes inside a specific container, similar to the top command in Linux.
docker top my-running-containerViewing Resource Usage (docker stats)
Section titled “Viewing Resource Usage (docker stats)”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 containersdocker stats
# Show stats for specific containersdocker stats container1 container2Press 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 containerdocker pause my-running-container
# Resume processes in the paused containerdocker unpause my-running-containerPausing 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-containerdocker start my-containerRemoving Containers (docker rm)
Section titled “Removing Containers (docker rm)”Deletes one or more containers. Containers must be stopped before removal unless the -f (force) flag is used.
# Remove a stopped containerdocker rm my-stopped-container
# Stop and remove a running containerdocker rm -f my-running-containerAttaching to a Container (docker attach)
Section titled “Attaching to a Container (docker attach)”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 applicationdocker attach my-interactive-containerUse 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.
Docker Container Lifecycle Overview
Section titled “Docker Container Lifecycle Overview”A container typically goes through the following states:
- Created: The container is created (e.g., via
docker createor the first step ofdocker 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.