Inspecting and Debugging Containers
1. Viewing Container Logs (docker logs)
| Option | Values | Description |
|---|---|---|
| docker logs | CONTAINER |
View logs for a running or stopped container |
| docker logs -f | CONTAINER |
Follow log output in real-time |
| docker logs --since | 2024-01-01T00:00:00 |
Only show logs since a specific time |
| docker logs --until | 2024-01-02T00:00:00 |
Only show logs until a specific time |
| docker logs --tail | 100 |
Show only the last 100 lines of logs |
| docker logs --details | CONTAINER |
Show detailed log information including timestamps and stream type |
Example: Viewing logs since a specific time
docker logs --since "2024-01-01T00:00:00" my-app-container
2. Following Log Output
| Option | Values | Description |
|---|---|---|
| docker logs | CONTAINER_ID |
Display logs from a container |
| docker logs -f | CONTAINER_ID |
Follow log output in real-time |
| docker logs --since | 2024-01-01T00:00:00Z |
Show logs since a specific timestamp |
| docker logs --until | 2024-01-02T00:00:00Z |
Show logs until a specific timestamp |
| docker logs -t | CONTAINER_ID |
Include timestamps in output |
| docker logs --details | CONTAINER_ID |
Show additional details (available in newer versions) |
Example: Filtering Logs by Time
docker logs --since "2024-01-01T00:00:00Z" --until "2024-01-02T00:00:00Z" <container_id>
Note: The
-f flag allows you to follow logs in real-time, which is useful for monitoring application behavior during runtime.
3. Limiting Log Output (--tail, --since)
| Option | Description | Usage |
|---|---|---|
| --tail | Limits the number of lines from the end of the logs to display | docker logs --tail 50 container_name |
| --since | Shows logs only from a specific time or duration | docker logs --since "2024-01-01T00:00:00" container_name |
4. Inspecting Container Details
| Command | Description | Example |
|---|---|---|
| docker inspect | Get detailed configuration and status information about a container | docker inspect my-container |
| docker inspect --format | Format output using Go template syntax | docker inspect --format '{{.State.Status}}' my-container |
Example: Inspecting Container Configuration
docker inspect --format '{{.Config.Image}}' my-container
Note: The inspect command returns a JSON object with extensive container details including network settings, mount points, and environment variables.
5. Viewing Container Processes
| Command | Description | Options | Example |
|---|---|---|---|
| docker top | Display the running processes inside a container | -a - Show all processes, including those not attached to a TTY |
docker top my-container |
| docker ps | List running containers; includes process info in the output | -a - Show all containers, not just running ones |
docker ps --format "table {{.Names}}\t{{.Command}}\t{{.Status}}" |
| docker exec | Execute a command inside a running container | -it - Allocate a TTY and keep stdin open for interactive use |
docker exec -it my-container ps aux |
Note: The
docker top command provides a view similar to the Unix ps command, showing process details like PID, USER, and COMMAND.6. Monitoring Container Stats
| Command | Description | Options | Example |
|---|---|---|---|
| docker stats | Displays live stream of container resource usage statistics | --no-stream, --format, --all |
docker stats --no-stream |
| docker stats --format | Formats output using Go template | --format with JSON or Go template syntax |
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" |
Note: Stats are updated every second by default. Use
--no-stream to get a single snapshot.Key Stats Fields
CPU %- Percentage of CPU usedMEM USAGE / LIMIT- Memory usage and limitNET I/O- Network input/outputBLOCK I/O- Block read/write operationsPIDS- Number of processes inside container
7. Executing Commands in Container
| Command | Description | Example |
|---|---|---|
| docker exec | Execute a command in a running container | docker exec -it container_name /bin/bash |
| docker exec -d | Execute a command in the background | docker exec -d container_name echo "Hello" |
| docker exec -it | Execute a command with TTY and interactive mode | docker exec -it container_name sh |
Example: Running a command in the background
docker exec -d my_container /bin/echo "Background task completed"
Note: The
-it flags are commonly used together to provide an interactive terminal session inside the container.
8. Attaching to Running Container
| Command | Description | Options | Example |
|---|---|---|---|
| docker attach | Attach local standard input, output, and error streams to a running container | --no-stdin, --detach-keys |
docker attach --no-stdin my-container |
Note: The container must be running for
docker attach to work. It does not support interactive mode like docker exec.Warning: Attaching to a container can interfere with its normal operation if the container's main process is not designed to handle terminal input/output.
9. Copying Files To/From Container
| Command | Description | Example |
|---|---|---|
| docker cp [OPTIONS] SRC_PATH DEST_PATH | Copies files or directories between a container and the local filesystem | docker cp container:/app/data.txt ./local-data.txt |
Example: Copying a file from container to host
docker cp my-container:/usr/local/bin/app ./app-binary
Note: The
docker cp command requires the container to be running.
10. Viewing Container Changes
| Command | Description | Example |
|---|---|---|
| docker diff | Shows changes to files in the container's filesystem | docker diff container-name |
| docker inspect | Displays low-level information about a container | docker inspect container-name |
Example: Using docker diff
docker diff container-name
Output:
A /app/log.txt
C /app/config.json
Where:
A= Added fileC= Changed fileD= Deleted file
Note:
docker diff only shows changes to the filesystem, not environment variables or runtime state.11. Exporting Container Filesystem
Use the docker export command to export a container's filesystem as a tar archive.
| Option | Values | Description |
|---|---|---|
| --output, -o | filesystem.tar |
Path to the output tar archive file |
Note: The exported filesystem does not include any layers or metadata from the original image. It's a flat archive of the container's filesystem at the time of export.
12. Inspecting Container Exit Codes
| Command | Description | Example |
|---|---|---|
| docker wait | Blocks until container stops and prints its exit code | docker wait my-container |
| docker inspect | Shows detailed container configuration and state including exit code | docker inspect --format='{{.State.ExitCode}}' my-container |
| docker logs | Displays container logs, useful for diagnosing exit causes | docker logs my-container |
Example: Exit Code Analysis
docker run --rm ubuntu:20.04 /bin/sh -c 'exit 1'
docker wait $(docker ps -q --filter "ancestor=ubuntu:20.04")
Note: Exit code
0 indicates success, any non-zero value indicates an error or failure.