Docker is an open-source platform that streamlines applications’ creation, deployment, and running using portable containers. It packages lightweight containers with all dependencies so that the software works uniformly across different environments.
Docker logs, the show’s star today, play a crucial role in monitoring and troubleshooting the said containers. So, they are essential for anyone using Docker.
Here is a basic rundown of terms that you might come across below:
- Docker Container: a standard unit of software with code and all dependencies
- Log entries: log messages from containers
- Log data: a sum of all container logs
- Container_name: Unique name of a specific container
- Log Drivers: tools that determine where logs are stored
- Log Rotation: a tool to limit and manage storage
Understanding Docker Logs
Docker logs are the output generated by a specific container, showing application errors and system processes to highlight troublesome behavior. Essentially, they help monitor and troubleshoot container processes to show what is happening inside.
Accessing Docker Logs
Docker captures logs from your container using two main streams: stdout and stderr. By default, these logs are managed by the JSON-file logging driver, which stores them as JSON files on the system.
Here is an example code for running and viewing container logs.
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
- View logs:
- Follow logs in real-time:
Where Are Docker Logs Stored?
Docker logs are stored in different locations depending on your system and the logging driver configured by the Docker daemon.
By default, Docker stores container logs in:
- Linux: /var/lib/docker/containers/<container_id>/<container_id>-json.log
- Windows: C:\ProgramData\Docker\containers\<container_id>\<container_id>-json.log
Other logging drivers and their locations include:
- Syslog: Logs are sent to the systems syslog service
- Journald: Logs are sent to systemd journal
- Fluentd, gelf, awslogs, etc: logs sent to third party cloud computing services
Managing Docker Logs
To manage docker logs effectively, focusing on the environment setup, log formats, integrations, and configuration is essential.
Here are the top key factors that help manage docker logs successfully.
1. Environment Setup
Setting up the right environment is a core step in managing systems. However, for docker logs, it is a crucial step you cannot skip at any cost. This ensures you have all the necessary tools and that your docker daemon is configured correctly.
- Docker Installation: Ensure that Docker is correctly installed on your system.
On Linux, use:
On macOS/Windows, use Docker Desktop.
- Configure Logging Drivers: Configure the ideal logging driver for your use case, such as the production environment as the default json-file might sometimes not be the ideal file.
You can modify the daemon.json file and restart the docker using;
2. API and Plugin Integration
For advanced log management, Docker supports third-party and external tools and plugins that integrate with logging systems.
Alternatively, the Docker API provides access to custom container logs, which helps in custom tooling. Many monitoring solutions use his functionality to aggregate logs. You can also extend logging capabilities using a plugin that supports multiple backends.
Example (Fluentd): Set Fluentd as a logging driver for easier integration with external systems.
3. Working with Unix Systems
Docker logs are typically stored in Unix-based systems in specific locations, such as /var/lib/docker/containers/<container_id>/ directory.
Here is how you can view these logs:
If you are a syslog logging driver, Docker sends logs to the system’s logging service. You can use tools like journalctl (for systemd) or syslog to access these logs.
journalctl -u docker.service
4. Log Format and Configuration
Logs are stored in JSON format, and each entry contains:
- Timestamp
- Stream (stdout/ stderr)
- Log message
You can view a specific number of lines using:
5. Advanced Logging Configuration
Log Drivers:
- json-file (default)
- syslog
- journald
- aws logs
- gelf
Example of using the syslog driver:
Or
How to Log into a Docker Container
You can log into a Docker container using the following methods:
- Using docker exec
Start an interactive shell using the following:
- Using Docker attach
For real-time logs or interaction, attach to the container primary process:
- Using Docker run
To create a new container interactively:
- Exiting the Containing
Simply type exit to leave the container.
Useful tips:
- Use docker ps to list and name all running containers
- Restart the container with docker start <container_id> before logging in.
Best Practices for Docker Log Management
Efficiently using the best practices for docker logging is essential for performance, scalability, and troubleshooting.
- Using the right logging driver with proper configuration is essential. Therefore, use the one that sets perfectly with your use case. Here is a cheat sheet.
- json-file: Default driver
- fluentd: Integrates with Fluentd for centralized logging.
- awslogs: For storing logs in Amazon CloudWatch.
- gelf: To send logs to Graylog or similar tools.
- syslog or journald: For local system logging.
- Log rotation helps prevent logs from consuming excessive disk space. Here is an example code:
- Use a driver to implement a central logging system using a driver like fluentd.
- Use metatags to make logs more identifiable. You can use the following source code.
- Use structured logging by formatting logs as JSON.
- Avoid using sensitive information for logging and always encrypt data when transferring remote data.
- Regularly monitor and analyze logs in the development environment.
Troubleshooting common issues
While managing docker logging, a whole lot of issues can arise. Here are a few troubleshooting methods for them;
- If you are missing out on logs, use this code:
The main cause for this issue is insufficient permissions.
- Logs consume too much disk space due to a lack of log rotation or verbose logging. You can manually clear logs using truncate -s 0 /var/lib/docker/containers/<container_id>/<container_id>-json.log.
Or use this source code:
- Logs are not forwarded to a centralized system due to network issues or misconfigured logging drivers. Use Fluentd or syslog to check logging driver settings.
- Cannot access real-time logs due to log buffering in external systems. Fix using docker logs -f commands.
- Logs are overwritten due to no log rotation or improper configuration. Fix by enabling log rotation in the Docker daemon.
Frequently Asked Questions – Docker Logs
1. How to check docker logs?
Docker logs are the output from a container’s processes written to stdout and stderr. They help monitor and debug containerized applications. Logs can be viewed using docker logs or a logging driver.
2. How to log into a docker container?
By default, docker logs are stored as JSON files in /var/lib/docker/containers/<container_id>/<container_id>-json.log on Linux systems. The exact line depends on the operating system.
3. Where are docker logs stored?
Use the docker logs <container_id> command to view logs. Add options like -f to follow logs in real-time or –tail <N> to see the last N lines.
4. Where are docker container logs stored?
Use docker exec -it <container_id> /bin/bash to access a running container interactively. If /bin/bash isn’t available, use /bin/sh.esz
Wrapping it Up!
Understanding the basics behind docker logs is essential to ensure smooth debugging and troubleshooting. Start with the basics and implement the best practices early to minimize the need for troubleshooting.