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
- Check if the container is running.
- Read logs.
- Check exit code.
- Inspect ports, mounts, and environment variables.
- Exec into the container if it is running.
- Test network connectivity.
- 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.
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-demoExpected 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
| Question | Command | What to inspect |
|---|---|---|
| Is it running? | docker ps -a | Exited, Restarting, Up |
| What did it print? | docker logs --tail 100 --timestamps CONTAINER | Application stdout/stderr available to configured logging driver |
| Why did it exit? | docker inspect --format '{{.State.ExitCode}} {{.State.OOMKilled}}' CONTAINER | Exit code and OOM status |
| Which port is published? | docker port CONTAINER | Host port and interface bindings |
| What is mounted? | docker inspect CONTAINER | Volumes, 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.
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_CONTAINERSome 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 .
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Prepare for Docker interviews with practical questions and real-time scenarios covering images, containers, Do...
3 min read · Updated Nov 16, 2026
Learn Docker environment variables and secrets, including docker run -e, env files, Compose environment, secre...
3 min read · Updated Nov 16, 2026
Learn Docker security best practices for images, containers, users, secrets, registries, scanning, least privi...
2 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.