Docker packing containers are simple to construct and run. However they might additionally abruptly forestall for lots of causes. The difficult section ceaselessly is determining why.

This information covers sensible tactics to debug a stopped Docker container, from checking logs and go out codes to keeping its state prior to you restart anything else.

Test container standing and logs

When a container stops, your first forestall must at all times be the Docker logs. Docker assists in keeping a report of the entirety that was once written to stdout and stderr whilst the container was once operating.

First, let’s see all packing containers, together with stopped ones:

docker playstation -a

Search for your container within the record. Observe its identify or container ID.

docker ps -a showing stopped container list

Then, test its logs:

docker logs [container_name_or_id]

This ceaselessly unearths the speedy purpose. Perhaps your utility threw an exception, or a dependency failed to start out. If the logs don’t display anything else glaring, don’t concern. We have now extra gear.

Figuring out go out codes

As you’ll see from the screenshot above, each Docker container exits with a code. Go out code 0 method luck (the container finished its activity). Some other quantity signifies an error. You’ll see the go out code within the docker playstation -a output, or get it immediately:

docker investigate cross-check [container_name_or_id] --format='{{.State.ExitCode}}'

Not unusual go out codes:

Go out Code Sign Most likely Reason
0 N/A Luck. Container completed activity
1 N/A Normal utility error
137 SIGKILL (9) Out-of-memory killer or power forestall
143 SIGTERM (15) Swish shutdown request
139 SIGSEGV (11) Segmentation fault (reminiscence get admission to)
255 N/A Go out standing out of vary

When you see go out code 137, your container most likely hit a reminiscence prohibit and was once killed through the gadget. Go out code 143 typically method one thing requested Docker to prevent the container gracefully.

Earlier than you attempt to convey the container again up, there may be one mistake price warding off.

Don’t restart too quickly

The standard intuition is to run docker get started [container] or achieve for Docker Compose. That may erase helpful proof, particularly if the container was once now not configured with continual garage. That you must lose:

  • Software logs that weren’t captured through Docker’s logging motive force
  • Transient recordsdata created all the way through the failed run
  • Crash dumps or core recordsdata
  • Database transaction logs (if operating a database)
  • Configuration adjustments made at runtime

So prior to you even take into accounts restarting, you want to maintain the proof.

Keep logs prior to they disappear

Docker assists in keeping logs for stopped packing containers, however there are limits. Via default, Docker makes use of the “json-file” logging motive force and not using a dimension prohibit, however in manufacturing, you will have log rotation or other drivers.

First, save the logs to a dossier straight away:

docker logs [container_name_or_id] > container_logs.txt

For packing containers with a large number of output, you may need to prohibit to the closing N strains:

docker logs --tail 1000 [container_name_or_id] > recent_logs.txt

When you suspect the problem took place some time in the past, you’ll come with the timestamps:

docker logs --timestamps [container_name_or_id] | grep -i "error|exception|fail"

Save container filesystem state

When a container stops, its filesystem nonetheless exists except it was once began with --rm.

You’ll extract recordsdata from it the usage of the next instructions:

Command What it preserves Perfect for
docker cp Explicit recordsdata/directories Fast extraction of logs, configs, temp recordsdata
docker export Whole filesystem (as tar archive) Whole backup for later forensic research
docker devote The whole thing: recordsdata, surroundings, metadata, state Best snapshot for crew sharing or not on time research

Reproduction recordsdata out prior to restarting

Use docker cp to extract essential directories:

docker cp [container_name_or_id]:/var/log ./container_logs
docker cp [container_name_or_id]:/tmp ./container_tmp
docker cp [container_name_or_id]:/and so on ./container_etc

Search for application-specific directories too. If you recognize your app writes to /app/logs or /knowledge, replica the ones.

Create a complete filesystem backup

For vital debugging eventualities, create an entire backup of the container’s filesystem:

docker export [container_name_or_id] > container_fs.tar

This creates a tar archive of all the container filesystem. You’ll discover it later:

tar -tf container_fs.tar | head -20  # Listing first 20 recordsdata
tar -xf container_fs.tar ./var/log   # Extract simply the log listing

Making a snapshot with docker devote

You’ll additionally use docker devote.

This command creates a brand new Docker symbol from a stopped container, keeping the entirety together with recordsdata, surroundings, metadata. It’s like taking a snapshot of the container precisely because it stopped.

docker devote [container_name_or_id] debug-snapshot

Now you could have a brand new symbol known as debug-snapshot. You’ll get started it, discover it, even push it to a registry for anyone else to inspect:

# Get started the snapshot container
docker run -it debug-snapshot /bin/bash

# Listing recordsdata inside of (from outdoor)
docker run --rm debug-snapshot ls -la /var/log

# Push to Docker Hub for crew research
docker tag debug-snapshot yourusername/debug-snapshot
docker push yourusername/debug-snapshot

This works neatly in a manufacturing surroundings as it preserves the container state totally. You’ll analyze it later, even though the unique container will get got rid of or rebuilt.

However needless to say dedicated pictures will also be huge as they come with all container layers. Use them judiciously, and blank up whilst you’re achieved with the next command:

docker rmi debug-snapshot

Make long term debugging more uncomplicated

For smoother debugging subsequent time, use continual volumes for logs and knowledge, arrange the precise logging motive force, and believe scripting the preservation steps you employ maximum ceaselessly.

When a container stops, save the proof first, then restart it.

The submit Methods to Debug a Stopped Docker Container gave the impression first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/debug-stopped-docker-container/

[ continue ]