tmpfs And Docker To Speed Up Your Database Test Suite

tags = [ Linux , Docker , docker-compose , Testing , Database , ]

Storing files in memory is extremely fast that's why tmpfs exists, It's a temporary file system or a virtual memory file system which will speed up accessing your files.

The tmpfs facility allows the creation of filesystems whose contents reside in virtual memory. Since the files on such filesystems typically reside in RAM, file access is extremely fast. Linux Programmer's Manual

Docker with docker-compose backed up with a solution that will automatically create a tmpfs volume using volumes with driver options driver_opts

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - dbtmp:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: datapass
       MYSQL_DATABASE: database_x
       MYSQL_USER: userx
       MYSQL_PASSWORD: passx

volumes:
  dbtmp:
    driver_opts:
      type: tmpfs
      device: tmpfs

For manual creation and size allocation of a tmpfs:


1. Create a directory under /mnt/
sudo mkdir /mnt/dbtmp

2. Mount a tmpfs with an allocated size of 512M using dbtmp.

sudo mount -t tmpfs -o size=512M tmpfs /mnt/dbtmp

That's it.

Like most of Linux file-systems you need a mount point "/mnt/dbtmp" and a mount command with a proper fs type "mount -t tmpf".


Link Docker with your mount point using docker-compose volumes

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - /mnt/dbtmp:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: datapass
       MYSQL_DATABASE: database_x
       MYSQL_USER: userx
       MYSQL_PASSWORD: passx

The good thing about using Docker is that you can build multiple containers with different versions of the base image and different database images like if you want to test your app against PostgreSQL and MySQL at the same time, so that will ease your database integrations in a rabidly fast test suite.