Skip
Arish's avatar

9. Container Logs and Debugging


Viewing Logs

bash
1# View logs
2docker logs mycontainer
3
4# Follow logs (live)
5docker logs -f mycontainer
6
7# Last 100 lines
8docker logs --tail 100 mycontainer
9
10# With timestamps
11docker logs -t mycontainer
12
13# Since specific time
14docker logs --since 2024-01-01 mycontainer
15docker logs --since 10m mycontainer

Executing Commands

bash
1# Run command in container
2docker exec mycontainer ls /app
3
4# Interactive shell
5docker exec -it mycontainer bash
6docker exec -it mycontainer sh
7
8# As different user
9docker exec -u root mycontainer whoami

Inspecting Containers

bash
1# Full inspection
2docker inspect mycontainer
3
4# Get IP address
5docker inspect -f '{{.NetworkSettings.IPAddress}}' mycontainer
6
7# Get mounted volumes
8docker inspect -f '{{.Mounts}}' mycontainer

Stats and Monitoring

bash
1# Live resource usage
2docker stats
3
4# Specific container
5docker stats mycontainer
6
7# One-time snapshot
8docker stats --no-stream