0
0
Dockerdevops~5 mins

Docker logging drivers - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run applications inside Docker containers, you need to see what they print out to understand what is happening. Docker logging drivers control how and where these logs are saved or sent, so you can check them later or send them to other tools.
When you want to save container logs to a file on the host machine for later review.
When you want to send container logs to a centralized system like syslog or journald for easier monitoring.
When you want to limit the size of log files to avoid filling up your disk space.
When you want to use a special logging service like Fluentd or GELF to collect logs from many containers.
When you want to disable logging to save resources if logs are not needed.
Commands
This command runs an nginx container named 'my-app' using the default 'json-file' logging driver. It limits each log file to 10 megabytes and keeps up to 3 log files to save disk space.
Terminal
docker run --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 --name my-app nginx
Expected OutputExpected
Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx Digest: sha256:... Status: Downloaded newer image for nginx:latest f1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a8b9c0d1e2f
--log-driver - Selects the logging driver to use for the container.
--log-opt - Sets options for the logging driver, like max file size and number of files.
Shows the logs produced by the 'my-app' container so you can see what the application printed.
Terminal
docker logs my-app
Expected OutputExpected
127.0.0.1 - - [date] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0"
Runs an nginx container named 'no-logs' with logging disabled to save resources when logs are not needed.
Terminal
docker run --log-driver none --name no-logs nginx
Expected OutputExpected
Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx Digest: sha256:... Status: Downloaded newer image for nginx:latest abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890
--log-driver - Disables logging by setting the driver to 'none'.
Checks which logging driver is used by the 'my-app' container to confirm the logging setup.
Terminal
docker inspect --format='{{.HostConfig.LogConfig.Type}}' my-app
Expected OutputExpected
json-file
Key Concept

If you remember nothing else from Docker logging drivers, remember: they control where and how your container logs are saved or sent, helping you monitor and manage your apps.

Common Mistakes
Not specifying a logging driver and expecting logs to be sent to a central system.
Docker uses the default 'json-file' driver which saves logs locally, so logs won't appear in external systems.
Specify the correct logging driver like 'syslog' or 'fluentd' with --log-driver and configure options accordingly.
Setting log file size limits too high or not at all, causing disk space to fill up.
Without limits, logs can grow indefinitely and fill the disk, causing system problems.
Use --log-opt max-size and max-file to limit log file size and number of files.
Disabling logging with --log-driver none but still expecting to see logs with docker logs.
With logging disabled, docker logs shows nothing because no logs are saved.
Only disable logging if you do not need logs; otherwise use a proper logging driver.
Summary
Use --log-driver to choose how Docker saves or sends container logs.
Use --log-opt to set options like max file size and number of log files.
Check logs with docker logs and verify logging driver with docker inspect.