Skip to main content

Using Docker Debug

1. Starting Debug Session

Example: Attach debugger to running container

docker exec -it <container_id> /bin/bash
Option Description Usage
-it Allocate a pseudo-TTY and keep STDIN open Used with exec to enter a running container interactively
<container_id> Identifier of the running container Replace with actual container ID or name
/bin/bash Shell to run inside the container Can be replaced with other shells or debug tools
Note: Ensure the container is running before attempting to attach.
Warning: Using exec in a production environment can be risky if not properly secured.

2. Debugging Running Containers

Command Description Usage
docker exec Execute a command in a running container docker exec <container> <command>
docker logs Fetch the logs of a container docker logs <container>
docker top Display the running processes inside a container docker top <container>

Example: Inspecting a running container

docker exec -it my_container /bin/bash

Example: Viewing logs from a container

docker logs -f my_container

Example: Listing processes in a container

docker top my_container
Note: Use -it flags with docker exec to get an interactive terminal session.
Warning: Be cautious when running commands inside containers, especially with root privileges.

3. Debugging Stopped Containers

Command Description Notes
docker ps -a Lists all containers including stopped ones Useful for identifying container IDs or names
docker logs <container-id> Displays logs from a stopped container Helps in diagnosing issues that occurred during execution
docker inspect <container-id> Shows detailed configuration and state of a container Useful for debugging runtime behavior and configuration

Example: Inspecting a Stopped Container

docker inspect my-container

Example: Viewing Logs from a Stopped Container

docker logs my-container
Note: If a container has been removed, logs and inspect data may no longer be accessible.

4. Debugging Slim Images

Aspect Description Notes
Image Size docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | grep slim Use this to verify image size and compare with full images
Inspect Image docker inspect <image-id> Provides detailed metadata including layers and configuration
Layer Analysis docker history <image-id> Shows each layer and its size to identify bloated components

Example: Identify Large Layers in Slim Image

docker history my-slim-image
Note: Slim images may lack debugging tools like bash, curl, or netstat. Consider installing minimal debugging tools if needed.

Example: Add Debug Tools to Slim Image

FROM alpine:latest
RUN apk add --no-cache curl bash

5. Installing Debug Tools Temporarily

Tool Description Installation Method Notes
strace System call tracer for debugging processes apt-get install strace Useful for tracing system calls in containers
tcpdump Network packet analyzer apt-get install tcpdump Can capture and analyze network traffic inside containers
htop Interactive process viewer apt-get install htop Provides real-time system monitoring inside containers
netstat Network statistics utility apt-get install net-tools Displays network connections, routing tables, and interface statistics

Example: Installing strace in a Container

docker run -it ubuntu:latest bash
apt-get update
apt-get install -y strace
Note: Debug tools installed this way are only available during the container's runtime and will be lost when the container stops or is removed.

6. Using Shell in Debug Mode

Option Values Description
docker exec -it <container_id> /bin/sh /bin/sh Starts a shell session inside the running container
docker exec -it <container_id> /bin/bash /bin/bash Starts a bash shell session inside the running container

Example: Accessing Container Shell

docker exec -it my_container /bin/bash
# You are now inside the container shell
root@abc123:/#
Note: The -it flags are essential for an interactive terminal session.
Warning: Be cautious when running commands in debug mode as they can affect container state or data.

7. Inspecting Filesystem in Debug

Command Description Example
docker exec Execute a command in a running container docker exec -it container_name ls /app
docker cp Copy files/folders between container and host docker cp container_name:/app/file.txt ./local_file.txt

Example: Inspecting Files in a Running Container

docker exec -it my_container ls -l /app

Example: Copying Files from Container to Host

docker cp my_container:/app/data.json ./backup.json
Note: Use docker exec to run commands inside a running container without attaching to its shell.
Warning: Be cautious when copying files with docker cp as it can overwrite local files.

8. Debugging Network Issues

Command Description Notes
docker network ls List all networks Shows network ID, name, driver, and scope
docker network inspect <network> Inspect a specific network Shows detailed configuration and connected containers
docker exec -it <container> ping <host> Test network connectivity from container Useful for checking if a container can reach another host or IP
docker exec -it <container> nslookup <host> Resolve DNS from container Helps debug DNS resolution issues inside containers

Example: Inspecting a network

docker network inspect bridge

Example: Testing connectivity from a container

docker exec -it myapp ping google.com
Note: When debugging network issues, ensure that the container's network mode matches your expectations (e.g., host, bridge, none).
Warning: If containers cannot communicate, check firewall rules and ensure that the necessary ports are exposed or mapped correctly.

9. Analyzing Container State

Command Description Example
docker inspect Get detailed information about a container or image docker inspect my-container
docker stats Display live resource usage statistics for containers docker stats my-container
docker top Display the running processes inside a container docker top my-container

Example: Inspecting Container Configuration

docker inspect --format '{{.Config.Env}}' my-container

Example: Monitoring Container CPU and Memory

docker stats --no-stream my-container
Note: The docker inspect command returns a JSON object with extensive configuration and runtime details of the container.
Warning: Using docker stats without --no-stream will continuously update the output, which may not be suitable for scripts or automated tools.

10. Exiting Debug Sessions Cleanly

Command Description Notes
docker exec -it <container> /bin/bash Attach to a running container's shell Useful for debugging running processes
Ctrl+D or exit Exit the shell session gracefully Ensures clean termination of debug session
docker stop <container> Stop the container process Graceful shutdown of container resources

Example: Exiting Debug Session

$ docker exec -it my-app /bin/bash
root@abc123:/# exit
$ docker stop my-app
Note: Always ensure all processes in the container are terminated before stopping to prevent resource leaks.

Was this article helpful?