Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move Postresql to RAM disk in Docker?

I want to run the Docker image postgres:9, stop Postgres, move it to /dev/shm, and restart it, so I can run my application tests really fast.

But when I try to stop Postgres in the container using postgres or pg_ctl I get told cannot be run as root.

Since all Docker containers log you in as the root user what can I do to run the Postgres commands I need?

And which folders do I need to move to /dev/shm before restarting it?


Command to start the container if you want to try this: docker run -it postgres:9 bash cd /usr/lib/postgresql/9.6/bin ./pg_ctl stop

like image 681
Richard Avatar asked Feb 14 '17 12:02

Richard


People also ask

How do I change my Docker RAM usage?

To limit the maximum amount of memory usage for a container, add the --memory option to the docker run command. Alternatively, you can use the shortcut -m . Within the command, specify how much memory you want to dedicate to that specific container.

What is Docker memory swap?

--memory-swap represents the total amount of memory and swap that can be used, and --memory controls the amount used by non-swap memory. So if --memory="300m" and --memory-swap="1g" , the container can use 300m of memory and 700m ( 1g - 300m ) swap.

Where is postgres data stored Docker?

The Docker postgres images, by default, create a PostgreSQL instance under /var/lib/postgresql/data , which is in the container's private storage unless mapped to a volume.


1 Answers

Mount a tmpfs in the container and point the PostgreSQL data at it

docker run --tmpfs=/pgtmpfs -e PGDATA=/pgtmpfs postgres:9

Use size=Nk to set a size limit (rather than all free memory).

--tmpfs /pgtmpfs:size=131072k

The same can be done for MySQL

docker run --tmpfs=/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:8

Kubernetes

An emptyDir volume can set the medium property to Memory

apiVersion: v1
kind: Pod
metadata:
  name: tmpfs-pd
spec:
  containers:
  - image: docker.io/postgres:12
    name: tmpdb
    env:
    - name: PGDATA
      value: /pgtmpfs
    volumeMounts:
    - mountPath: /pgtmpfs
      name: tmpdata-volume
  volumes:
  - name: tmpdata-volume
    emptyDir:
      medium: Memory
      sizeLimit: 131072k

Docker Compose

And in a docker compose 3.6+ definition (not supported by stack)

version: "3.6"
services:
  db:
    image: docker.io/postgres:12
    environment:
      - PGDATA=/pgtmpfs
    tmpfs:
      - /run
      - /var/cache

Compose can define shared volumes of tmpfs as well.

like image 64
Matt Avatar answered Oct 16 '22 17:10

Matt