Docker packing containers are easy to build and run. On the other hand they may moreover swiftly prevent for a lot of reasons. The tough phase perpetually is figuring out why.
This knowledge covers good techniques to debug a stopped Docker container, from checking logs and cross out codes to holding its state previous to you restart anything.
Check container status and logs
When a container stops, your first prevent will have to always be the Docker logs. Docker keeps a document of everything that was once as soon as written to stdout and stderr while the container was once as soon as running.
First, let’s see all boxes, at the side of stopped ones:
docker ps -a
Seek for your container throughout the file. Follow its determine or container ID.

Then, check its logs:
docker logs [container_name_or_id]
This perpetually finds the fast function. In all probability your software threw an exception, or a dependency failed to start out out. If the logs don’t show anything obtrusive, don’t worry. We have now now further equipment.
Working out cross out codes
As you’ll see from the screenshot above, every Docker container exits with a code. Move out code 0 means success (the container completed its job). Any other amount indicates an error. You’ll see the cross out code throughout the docker ps -a output, or get it right away:
docker inspect [container_name_or_id] --format='{{.State.ExitCode}}'
No longer abnormal cross out codes:
| Move out Code | Signal | Perhaps Reason why |
|---|---|---|
| 0 | N/A | Good fortune. Container finished job |
| 1 | N/A | Commonplace software error |
| 137 | SIGKILL (9) | Out-of-memory killer or energy prevent |
| 143 | SIGTERM (15) | Graceful shutdown request |
| 139 | SIGSEGV (11) | Segmentation fault (memory get right of entry to) |
| 255 | N/A | Move out status out of range |
While you see cross out code 137, your container possibly hit a memory limit and was once as soon as killed during the machine. Move out code 143 most often means something asked Docker to forestall the container gracefully.
Previous than you try to put across the container once more up, there could also be one mistake worth fending off.
Don’t restart too briefly
The usual instinct is to run docker get began [container] or succeed in for Docker Compose. That can erase useful evidence, in particular if the container was once as soon as no longer configured with chronic storage. That you simply will have to lose:
- Tool logs that weren’t captured via Docker’s logging driver
- Brief files created during the failed run
- Crash dumps or core files
- Database transaction logs (if running a database)
- Configuration changes made at runtime
So previous to you even think about restarting, you wish to have to care for the evidence.
Stay logs previous to they disappear
Docker keeps logs for stopped boxes, then again there are limits. By way of default, Docker uses the “json-file” logging driver and no longer the usage of a size limit, then again in production, you’re going to have log rotation or different drivers.
First, save the logs to a file right away:
docker logs [container_name_or_id] > container_logs.txt
For boxes with numerous output, you could wish to limit to the ultimate N traces:
docker logs --tail 1000 [container_name_or_id] > recent_logs.txt
While you suspect the issue came about a while previously, you’ll include the timestamps:
docker logs --timestamps [container_name_or_id] | grep -i "error|exception|fail"
Save container filesystem state
When a container stops, its filesystem nevertheless exists except for it was once as soon as started with --rm.
You’ll extract files from it the use of the following directions:
| Command | What it preserves | Highest for |
|---|---|---|
docker cp |
Particular files/directories | Speedy extraction of logs, configs, temp files |
docker export |
Complete filesystem (as tar archive) | Complete backup for later forensic analysis |
docker commit |
The entire thing: files, setting, metadata, state | Perfect snapshot for staff sharing or behind schedule analysis |
Replica files out previous to restarting
Use docker cp to extract crucial 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 forth ./container_etc
Seek for application-specific directories too. If you already know your app writes to /app/logs or /wisdom, reproduction those.
Create an entire filesystem backup
For essential debugging scenarios, create a whole backup of the container’s filesystem:
docker export [container_name_or_id] > container_fs.tar
This creates a tar archive of the entire container filesystem. You’ll uncover it later:
tar -tf container_fs.tar | head -20 # List first 20 files tar -xf container_fs.tar ./var/log # Extract merely the log list
Creating a snapshot with docker commit
You’ll moreover use docker commit.
This command creates a brand spanking new Docker image from a stopped container, holding everything at the side of files, setting, metadata. It’s like taking a snapshot of the container exactly as it stopped.
docker commit [container_name_or_id] debug-snapshot
Now it’s essential to have a brand spanking new image referred to as debug-snapshot. You’ll get began it, uncover it, even push it to a registry for any person else to check up on:
# Get began the snapshot container docker run -it debug-snapshot /bin/bash # List files within (from out of doors) docker run --rm debug-snapshot ls -la /var/log # Push to Docker Hub for staff analysis docker tag debug-snapshot yourusername/debug-snapshot docker push yourusername/debug-snapshot
This works well in a production setting because it preserves the container state utterly. You’ll analyze it later, even if the original container gets removed or rebuilt.
On the other hand keep in mind that devoted footage may also be large as they arrive with all container layers. Use them judiciously, and clean up when you’re completed with the following command:
docker rmi debug-snapshot
Make longer term debugging more straightforward
For smoother debugging next time, use chronic volumes for logs and information, organize the appropriate logging driver, and imagine scripting the preservation steps you utilize most perpetually.
When a container stops, save the evidence first, then restart it.
The publish Methods to Debug a Stopped Docker Container appeared first on Hongkiat.
Supply: https://www.hongkiat.com/blog/debug-stopped-docker-container/


0 Comments