5 Docker Logging Best Practices
Containers offer a scalable way to run software when moving between environments. Docker is the most well-known infrastructure; read about best practices here.
Join the DZone community and get the full member experience.Join For Free
containers have become a huge topic in it, and especially in devops, over the past several years. simply stated, containers offer an easy and scalable way to run software reliably when moving from one environment to another. containers do this by providing an entire runtime environment in one package, which includes the application, plus all dependencies, libraries and other binaries, and configuration files needed to run it.
closely aligned with containers are “microservices” which represent a more agile way of developing applications. a microservices architecture structures an application as a set of loosely coupled services connected via functional apis that handle discrete business functions. instead of a large monolithic code base, microservices primarily offer a “divide and conquer” approach to application development.
leading the charge in the world of container infrastructures is docker , a platform for deploying containerized software applications. the real value of containers is that they allow teams to spin up a full runtime environment on the fly. docker is arguably the most influential platform today for getting businesses to adopt microservices.
similar to how virtual machines streamlined software development and testing by providing multiple instances of an os to end-users from one server, containers add an extra abstraction layer between an application and the host os. the big difference is that containers don’t require a hypervisor and only run one instance of an operating system; overall, this equates to far less memory and faster run time.
as with developing any application, logging is a central part of the process and especially useful when things go wrong. but logging in the world of containerized apps is different than with traditional applications. logging docker effectively means not only logging the application and the host os, but also the docker service.
there are a number of logging techniques and approaches to keep in mind when working with dockerized apps. we outline the top five best practices in more detail below.
in an application-based approach, the application inside the containers uses a logging framework to handle the logging process. for instance, a java application might use log4j2 to format and send log files to a remote server and bypass the docker environment and os altogether.
while application-based logging gives developers the most control over the logging event, the approach also creates a lot of overhead on the application process.
this approach might be useful for those who are working within more traditional application environments since it allows developers to continue using the application’s logging framework (i.e., log4j2) without having to add logging functionality to the host.
using data volumes
containers by nature are transient, meaning that any files inside the container will be lost if the container shuts down. instead, containers must either forward log events to a centralized logging service (such as loggly ) or store log events in a data volume. a data volume is defined as “a marked directory inside of a container that exists to hold persistent or commonly shared data.”
the advantage of using data volumes to log events is that since they link to a directory on the host, the log data persists and can be shared with other containers. the advantage of this approach is that it decreases the likelihood of losing data when a container fails or shuts down.
instructions for setting up a docker data volume in ubuntu can be found here .
docker logging driver
a third approach to logging events in docker is by using the platform’s logging drivers to forward the log events to a syslog instance running on the host. the docker logging driver reads log events directly from the container’s stdout and stderr output; this eliminates the need to read to and write from log files, which translates into a performance gain.
however, there are a few drawbacks to using the docker logging driver: 1) it doesn’t allow for log parsing, only log forwarding 2) docker log commands work only with log driver json files 3) containers terminate when the tcp server becomes unreachable.
instructions for configuring the default logging driver for docker may be found here .
dedicated logging container
this approach has the primary advantage of allowing log events to be managed fully within the docker environment. since a dedicated logging container can gather log events from other containers, aggregate them, then store or forward the events to a third-party service, this approach eliminates the dependencies on a host.
additional advantages of dedicated logging containers are: 1 ) automatically collect, monitor, and analyze log events 2) scale your log events automatically without configuration 3) retrieve logs through multiple streams of log events, stats, and docker api data.
sidecars have become a popular approach to managing microservices architectures. the idea of a sidecar comes from the analogy of a how a motorcycle sidecar is attached to a motorcycle. to quote one source , “a sidecar runs alongside your service as a second process and provides ‘platform infrastructure features’ exposed via a homogeneous interface such as a rest-like api over http.”
from a logging standpoint, the advantage of a sidecar approach is that each container is linked to its own logging container (the application container saves the log events and the logging container tags and forwards them to a logging management system like loggly).
the sidecar approach is especially useful for larger deployments where more specialized logging information and custom tags are necessary. though, setting up sidecars are more notably complex and difficult to scale.
Published at DZone with permission of Jeffrey Walker. See the original article here.
Opinions expressed by DZone contributors are their own.