Essential Docker Compose Log Commands
1. Basic Log Viewing
# View logs of all services defined in docker-compose.yml
docker-compose logs
# View logs of specific services
docker-compose logs service1 service2
# Follow logs in real-time (similar to tail -f)
docker-compose logs -f
# Show timestamps with logs
docker-compose logs -t
# Combination of following and timestamps
docker-compose logs -f -t service1
2. Filtering and Limiting Logs
# View only the last N lines of logs
docker-compose logs --tail=100
# Filter logs since a specific time
docker-compose logs --since="2023-01-01T00:00:00"
# Until a specific time
docker-compose logs --until="2023-01-02T00:00:00"
# Combine with specific services
docker-compose logs --tail=50 service1
3. Log Management Commands
# Check if logs exist and where they are stored
docker-compose ps
# Remove stopped service containers (and their logs)
docker-compose rm
# Remove volumes (be careful as this can delete persistent data)
docker-compose down -v
# Force recreation of containers and their logs
docker-compose up -d --force-recreate
4. Advanced Inspection
# Inspect container details including log configuration
docker inspect container_name
# View logs directly from Docker (alternative method)
docker logs container_name
# View log drivers and logging configuration
docker info | grep Logging
Understanding Docker Compose Logging Architecture
Docker Compose relies on Docker's logging system, which uses logging drivers to handle container logs. By default, Docker uses the json-file
driver that stores logs as JSON files on the host.
Here's a diagram showing how logging works in Docker Compose:
Loading diagram...
Loading diagram...
# Example docker-compose.yml with custom logging options
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "80:80"
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
api:
image: node:alpine
command: node server.js
logging:
driver: "local"
options:
max-size: "20m"
max-file: "5"
database:
image: postgres:13
logging:
driver: "syslog"
options:
syslog-address: "tcp://localhost:514"
tag: "{{.Name}}"
metrics:
image: prometheus:latest
logging:
driver: "fluentd"
options:
fluentd-address: "localhost:24224"
tag: "docker.{{.Name}}"