How to setup log rotation for Docker container
Tags: docker, logging
Published: 2018-06-14 03:45:41

---
title: How to setup log rotation for Docker container
published: true
description: Setup log rotation for docker containers and we could manually purge the log file in case it takes up too much disk space.
tags: [Docker, Logging]
cover_image: https://thepracticaldev.s3.amazonaws.com/i/oz7gz9nwa6bxm134upt7.png
---

_Originally posted on [Boatswain Blog](https://blog.boatswain.io/post/docker-container-log-rotation/)._

---

## We all need logs!

Sometimes working with [Docker][docker] makes me feel like working with a black box especially when playing with the [Docker][docker] image from by the community and it doesn't go the way as expected. In many cases, reading logs takes up a large portion of time for debugging.

![](https://thepracticaldev.s3.amazonaws.com/i/iwf9nqcvpp6bp8e4av5x.jpg)

This article is about setting up log rotation for [Docker][docker] containers.

## The default logging driver

We could configure different logging driver for containers and by default the **stdout** and **stderr** of the container are written in a json file located in _/var/lib/docker/containers/[container-id]/[container-id]-json.log_. If you leave it unattended, it could takes up a large amount of disk space as shown below.

![A large log file in json format](https://thepracticaldev.s3.amazonaws.com/i/ewyzkc6d6pjiorzwaszz.jpg)

### Purge the log manually

In case this json log file takes up a significant amount of the disk, we could purge it using the following command.

```shell
truncate -s 0 <logfile>
```

We could setup a cronjob to purge these json log files regularly but for long term, it would be better to setup log rotation.

## Setup the log rotation

### Configure the default logging driver

This could be done by adding the following values in _/etc/docker/daemon.json_. Create this file if it doesn't exist.

```json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "10"
  }
}
```

The _json-file_ logging driver has a few more options and we could even change to other logging drivers such as _syslog_. For more information, please refer to the [Docker Docs - Configure logging drivers](https://docs.docker.com/config/containers/logging/configure/).

Execute the following commands to reload the updated _daemon.json_. The new configuration will apply to all newly created container after restart.

```shell
$ systemctl daemon-reload

$ systemctl restart docker
```

### Configure the logging driver for a container

The configuration could also be done on container level if you do not want to apply it globally.

#### The docker run command

We could specify the logging driver and options in the _docker run_ command. For example:

```shell
$ docker run \
    --log-driver json-file \
    --log-opt max-size=10m \
    --log-opt max-file=10 \
    alpine echo hello world
```


#### Using docker-compose

The logging driver and options could also be configured using docker-compose. For example:

```yaml
version: '3.2'
services:
  nginx:
    image: 'nginx:latest'
    ports:
      - '80:80'
    logging:
      driver: "json-file"
      options:
        max-size: "1k"
        max-file: "3"
```

Verify if the setup is working.

![The logs are broken down into 1k files](https://thepracticaldev.s3.amazonaws.com/i/8rmiccbwvjvkn6a0rvh3.jpg)

## Summary

Although the default setting works fine, you never know when the container logs take up all the disk space and that could be avoided by the above few steps. Other than that, logs are important asset which is not only useful when something goes wrong but also contains a lot of hidden values. So never never let the logs go.

If you are looking for a **log management SAAS solution**, consider using [Boatswain][boatswain] and we help you managing all the logs as well as monitoring your [Docker][docker] servers. 💫

![Insufficient facts always invite danger](https://thepracticaldev.s3.amazonaws.com/i/bq2s1t4uivetgtremwxg.jpg)

> Insufficient facts always invite danger.
> -- Spock @ Star Trek




[docker]: https://www.docker.com/
[boatswain]: https://boatswain.io/