Docker Container Logs Not Showing Fix
In this tutorial, you'll learn about Docker Container Logs Not Showing Fix. We cover key concepts, practical examples, and best practices.
You run docker logs my-container and get nothing, even though the container is running. Docker captures container logs from stdout and stderr, so when logs are missing the issue is usually the logging driver, the application not writing to stdout, or a log configuration mismatch.
The Problem
docker logs my-app
# (no output)
The container runs fine but logs show nothing. Check the container status:
docker ps -a --filter name=my-app
Expected:
CONTAINER ID IMAGE STATUS PORTS NAMES
abc123 my-app Up 10 minutes my-app
Wrong Code (Application Not Writing to stdout)
import logging
# WRONG — logs to file, never seen by Docker
logging.basicConfig(filename='/var/log/app.log', level=logging.INFO)
logging.info('Application started')
Right Code (Writing to stdout)
import logging
import sys
# RIGHT — Docker captures stdout
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.info('Application started')
Expected output:
INFO:root:Application started
Step-by-Step Fix
Step 1: Check the log driver
docker inspect my-app --format '{{.HostConfig.LogConfig.Type}}'
Expected output:
json-file
If it shows none or journald, logs are not stored where docker logs reads them.
Step 2: Switch to json-file driver
Edit /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Restart Docker:
sudo systemctl restart docker
Step 3: Verify stdout/stderr in your application
import sys
# Docker captures anything written to stdout/stderr
sys.stdout.write('This appears in docker logs\n')
sys.stderr.write('Errors also appear\n')
For Node.js:
// WRONG — file logging
const fs = require('fs');
fs.appendFileSync('/app/logs.txt', 'message');
// RIGHT — console.log writes to stdout
console.log('message');
Step 4: Test with a simple container
docker run --rm alpine echo "hello world"
docker ps -l -q | xargs docker logs
Expected:
hello world
Step 5: Check Docker daemon logs
sudo journalctl -u docker -n 50 --no-pager
Prevention Tips
- Always write application logs to stdout/stderr, not files
- Configure log rotation with
--log-opt max-size=10m - Use the
json-filelog driver unless you have a specific reason not to - Test log output with
docker logs --tail 10immediately after starting - Monitor log storage with
docker system df
Common Mistakes with container logs
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world DOCKER code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro