PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 23, 2022

[FIXED] How to move Postresql to RAM disk in Docker?

 October 23, 2022     docker, postgresql     No comments   

Issue

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


Solution

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

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

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:15
    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:15
    environment:
      - PGDATA=/pgtmpfs
    tmpfs:
      - /run
      - /var/cache

Compose can define shared volumes of tmpfs as well.



Answered By - Matt
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing