[Raspberry Pi] I Thought /var/log Was Safe on a Ramdisk, but Docker Logs Were Being Written to the SD Card

Docker

To extend the life of the SD card, I configured /var/log to live on a Ramdisk. But Docker logs are output to /var/lib/docker/containers, so of course that did nothing for Docker logs.

Test Environment

Raspberry Pi 4 (8GB RAM)
Ubuntu 20.10 64-bit

$ cat /etc/fstab
LABEL=writable / ext4 defaults 0 0
LABEL=system-boot /boot/firmware vfat defaults 0 1
tmpfs /tmp tmpfs defaults,size=128m 0 0
tmpfs /var/tmp tmpfs defaults,size=128m 0 0
tmpfs /var/log tmpfs defaults,size=64m 0 0

What I Changed

Change Docker's logging driver settings. Check the official site for details.
Reference: Configure logging drivers

There are two ways to configure this: change the Docker daemon default by writing to /etc/docker/daemon.json, or configure the logging driver for each container.

In my Docker environment, I only run WordPress and a mail server with docker-compose, so I chose to configure each container in docker-compose.yml.

Run the following command to check the logging driver setting of the currently running Docker daemon.

$ docker info --format '{{.LoggingDriver}}'
json-file

By default, logs are output in json-file format under /var/lib/docker/containers/[container_id]. Logs needed for operation are already output to the host's /var/log, so I set the logging driver to none and stop this output.

Configuration Examples

Mail server settings. The parts in red are what I added this time.

version: '3'

services: mailserver: image: docker.io/mailserver/docker-mailserver:latest hostname: ${HOSTNAME} domainname: ${DOMAINNAME} container_name: ${CONTAINER_NAME} env_file: mailserver.env ports: - “25:25” - “143:143” - “587:587” - “993:993” volumes: - maildata:/var/mail - mailstate:/var/mail-state - /var/log/mailserver/mail:/var/log/mail/ - /var/log/mailserver/supervisor/:/var/log/supervisor/ # - maillogs:/var/log/mail - ./config/:/tmp/docker-mailserver/ - /etc/letsencrypt/:/etc/letsencrypt/ cap_add: [ “NET_ADMIN”, “SYS_PTRACE” ] restart: always logging: driver: none

volumes: maildata: mailstate:

maillogs:

WordPress settings. Again, add the parts shown in red.

version: '3.1'

services:

wordpress: image: wordpress restart: always logging: driver: none ports: - 127.0.0.1:8080:80 environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wp volumes: - ./html:/var/www/html - ./php.ini:/usr/local/etc/php/php.ini depends_on: - db

db: image: mysql/mysql-server:latest # image: mysql:5.7 restart: always logging: driver: none environment:

MYSQL_ROOT_PASSWORD: wordpress

  MYSQL_RANDOM_ROOT_PASSWORD: '1'
  MYSQL_USER: wordpress
  MYSQL_PASSWORD: wordpress
  MYSQL_DATABASE: wp
volumes:
  - db:/var/lib/mysql

volumes: db:

The data is persistent, so run docker-compose down/up to rebuild the containers and apply the logging driver settings. You can check each container's logging driver setting with the following command.

$ docker inspect -f '{{.HostConfig.LogConfig.Type}}' fc4503182c41
none

If it is none, the setting is complete.

Me
Me

If you do not take care of an SD card, it can fail in about a year, so this is a small but important setting for a Raspberry Pi server.

Me
Me

I keep making Docker-related operational mistakes: logs were not on the Ramdisk, mapped ports were exposed externally while ignoring firewall settings, and so on.

ramdisk
Sponsored links
Follow ☆Geha☆