Skip to main content

DevOps

Debugging Docker Containers via SSH and Logs

When a Docker container enters a restart loop at 3AM, you need a repeatable debugging workflow. This guide covers inspecting exit codes, reading container logs over SSH, diagnosing OOM events, and verifying remediation without leaving your terminal.

4 minute read

Understanding Docker Container Exit Codes

Exit code 137 is the most common OOM kill indicator — the kernel terminated the container because its memory usage exceeded the configured cgroup limit. Exit code 1 indicates a generic application error. Exit code 125 means the Docker daemon itself failed to start the container, often due to a configuration conflict.

Start every container debugging session by running `docker inspect <container_id>` and examining the `State.ExitCode` and `State.OOMKilled` fields. These two values immediately narrow down whether the failure is memory-related, application-level, or infrastructure-level.

For containerized Node.js and Python applications, heap exhaustion often manifests as exit code 1 with a `JavaScript heap out of memory` or `MemoryError` in the final log lines rather than an OOM kill — because the language runtime crashes before the kernel enforces the cgroup limit.

Reading Container Logs Over SSH Without Losing Context

When SSHing into a production Docker host, resist the instinct to run `docker logs <container> | tail -100`. This approach discards timestamps and loses the ability to filter by log level. Instead, use `docker logs --since 30m --timestamps <container>` to scope the window to the relevant incident timeframe.

For services that write structured JSON logs, pipe through `jq` to isolate error fields: `docker logs <container> 2>&1 | jq -r "select(.level == \"error\") | [.timestamp, .message] | @tsv"`. This reduces noise dramatically during high-volume incident investigation.

ShellMate log tailer plugin streams live container output directly inside the SSH session panel without requiring manual docker log commands, making it significantly faster to identify repeating error patterns during active incident response.

Diagnosing Restart Loops: The Three Most Common Causes

A container in perpetual restart loop (visible as `Restarting (1) X seconds ago` in `docker ps`) is caused by one of three scenarios: the entrypoint process crashes immediately on startup, a health check is failing causing Docker to restart the container, or a dependency service is not yet available and the application exits instead of retrying.

For entrypoint crashes, run `docker run --entrypoint sh <image>` to open an interactive shell inside the image and manually execute the startup command. This bypasses the restart policy and gives you a clean environment to observe the failure directly.

For health check failures, inspect the health check configuration with `docker inspect <container> | jq ".[].Config.Healthcheck"` and manually execute the health check command inside a running container to see its actual output and exit code.

Using ShellMate Docker Plugin for Visual Container Inspection

The ShellMate Docker plugin connects to the remote Docker daemon over the existing SSH session without requiring additional port exposure or API surface. From inside the plugin panel, you can view all running and stopped containers, their resource consumption, port bindings, environment variables, and health status.

Clicking a container opens its live log stream in a split pane alongside the container detail view — eliminating the need to switch between docker commands while also viewing resource graphs.

Related ShellMate resources

Questions about Debugging Docker Containers via SSH and Logs

How do I check why a Docker container exited?

Run `docker inspect <container_id>` and check the `State.ExitCode` and `State.OOMKilled` fields. Exit code 137 with OOMKilled=true means memory exhaustion. Exit code 1 means the application crashed. Also check `docker logs <container_id>` for the final output before exit.

How can I debug a container that exits immediately on start?

Override the entrypoint with `docker run --entrypoint sh -it <image>` to open an interactive shell and manually run the startup commands. This lets you see exactly what fails without the restart policy interfering.

What is the fastest way to tail Docker logs over SSH?

Use `docker logs -f --timestamps <container>` for live streaming with timestamps. For structured JSON logs, pipe through jq to filter by log level. ShellMate log tailer plugin automates this inside the active SSH workspace session.

Put the guide into practice

Organize hosts, choose a deliberate credential path, and keep SSH work visible in a focused desktop workspace.