How to Persist Docker Logs

You can deploy all kinds of software packages and applications with Docker and there’s a lot of very good reasons to do that, but a lot of new Docker users get tripped up on logging. They don’t know initially that each time a container is re-deployed or rebuilt, all the logged data will be destroyed. In some cases, this doesn’t matter, but for the most common case of application deployment, retaining logs for at least some period of time is an absolute must. Here are two great strategies for persisting your application logs in Docker.

Volumes

The simplest way to persist your application logs is to create a Docker volume. In simplest terms, Docker volumes are static directories on the host machine. Put even more simply, this method allows you to save write the log files to a directory on the host machine that will be persisted between rebuilds of the Docker container because it is not within the container itself.

3rd Party Services

We love working in Ruby on Rails and tend to use third-party services such as New Relic and Papertrail that integrate tightly with the framework. There’s a number of advantages to using services like this including pre-fabricated custom alerts, Slack integration, and sophisticated web-based dashboard user interfaces for browsing and searching application logs. We also like this method, because it does not rely on a static directory existing on the host machine and keeps the containerized app more in its container.

If you want to get started on this today, then download our free guide on persisting Docker logs. It containers step by step instructions for both the Volumes method and the using a third-party service.