Docker ContainerDevOps AutomationDevOps GuidesDocker Guide

Article & Tutorial

Docker Logs, Debugging and Troubleshooting Guide

Troubleshoot Docker containers, logs, ports, image errors, permissions, volumes, networks, exit codes, health checks, and common Docker problems.

Contents

Docker Logs, Debugging and Troubleshooting Guide

Docker troubleshooting becomes easier when you follow a clear flow: check container status, read logs, inspect configuration, verify ports, test networks, and review volumes.

Quick troubleshooting flow

  1. Check if the container is running.
  2. Read logs.
  3. Check exit code.
  4. Inspect ports, mounts, and environment variables.
  5. Exec into the container if it is running.
  6. Test network connectivity.
  7. Rebuild image if needed.

Check container status

docker ps docker ps -a

Read logs

docker logs CONTAINER_NAME docker logs -f CONTAINER_NAME docker logs --tail 100 CONTAINER_NAME

Inspect container details

docker inspect CONTAINER_NAME

Inspect output can show environment variables, mounts, ports, network settings, image ID, restart policy, and exit code.

Exec inside a running container

docker exec -it CONTAINER_NAME sh docker exec -it CONTAINER_NAME bash

Common issue: container exits immediately

Usually the main process finished or failed. Check logs and exit code.

docker ps -a docker logs CONTAINER_NAME

Common issue: port not working

docker run -d -p 8080:80 nginx docker port CONTAINER_NAME

Check whether the app listens on the correct container port and whether the host port is already used.

Common issue: container cannot reach database

  • Check if both containers are on the same network.
  • Use service/container name instead of localhost.
  • Check database startup readiness.
  • Check credentials and environment variables.

Network debug commands

docker network ls docker network inspect NETWORK_NAME docker exec -it app sh ping db nslookup db

Volume debug commands

docker volume ls docker volume inspect VOLUME_NAME docker inspect CONTAINER_NAME | grep Mounts -A 20

Common mistakes

  • Debugging randomly without reading logs.
  • Using localhost inside one container to reach another container.
  • Forgetting port publishing.
  • Deleting containers before checking logs.
  • Removing volumes during cleanup and losing data.

Best practices

  • Always start with docker ps -a and logs.
  • Name containers and services clearly.
  • Use health checks for important apps.
  • Keep Dockerfiles simple and reproducible.
  • Document common fixes for your team.

Related Melochip guides

  • Docker Learning Path
  • Docker Commands Explained
  • GitLab CI/CD Troubleshooting Guide

Official references

  • docker logs reference
  • docker inspect reference

Worked example: a container exits with code 42

A Docker container stops when its main process exits. This controlled illustration intentionally runs a process that prints a message and exits with code 42. Inspect its logs and exit state before deleting the disposable example container.

exit-code-demo.sh
docker run --name melochip-exit-demo alpine:3.20 sh -c 'echo "Demo failure"; exit 42'
docker ps -a --filter name=melochip-exit-demo
docker logs --tail 20 melochip-exit-demo
docker inspect --format '{{.State.ExitCode}} | {{.State.Status}} | {{.State.OOMKilled}}' melochip-exit-demo
# Remove only this disposable example:
docker rm melochip-exit-demo

Expected behavior: the logs include 'Demo failure', exit code is 42, status is exited and OOMKilled is false. For a real failed service, review its exact exit code, logs, configuration and memory status before restarting. This is an illustrative example, not a reported executed test.

Read-only Docker diagnostics

QuestionCommandWhat to inspect
Is it running?docker ps -aExited, Restarting, Up
What did it print?docker logs --tail 100 --timestamps CONTAINERApplication stdout/stderr available to configured logging driver
Why did it exit?docker inspect --format '{{.State.ExitCode}} {{.State.OOMKilled}}' CONTAINERExit code and OOM status
Which port is published?docker port CONTAINERHost port and interface bindings
What is mounted?docker inspect CONTAINERVolumes, restart policy, networks; inspect privately for secrets

When a container cannot reach its database

Remember that localhost inside a container points to that container, not another database container. On a user-defined bridge network, containers can generally resolve one another by name. Check that both containers share the intended network and the database is ready before changing credentials.

network-debug.sh
docker network ls
docker inspect --format '{{json .NetworkSettings.Networks}}' APP_CONTAINER
docker inspect --format '{{json .NetworkSettings.Networks}}' DB_CONTAINER
# If getent exists inside the app image:
docker exec APP_CONTAINER getent hosts DB_CONTAINER

Some minimal images lack getent, ping and nslookup. A separate diagnostic container attached to the relevant test network can help. Avoid posting raw inspect output containing environment secrets. Do not prune production volumes while troubleshooting.

Why docker logs might be empty

docker logs depends on the logging driver and how the application emits output. Check the driver using docker inspect --format '{{.HostConfig.LogConfig.Type}}' CONTAINER; if necessary review the configured logging backend. Official references: https://docs.docker.com/reference/cli/docker/container/logs/ and https://docs.docker.com/reference/cli/docker/container/inspect/ . Melochip Docker commands guide: https://melochip.com/posts/docker-commands-explained .

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.