16

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

Richard
  • 14,798
  • 21
  • 70
  • 103

2 Answers2

37

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:
      - /pgtmpfs

Compose can define shared volumes of tmpfs as well.

Matt
  • 68,711
  • 7
  • 155
  • 158
0

By adding --user=postgres to your docker parameter list, you'll get to be user=postgres directly:

docker --user=postgres run -it postgres:9 bash
cd /usr/lib/postgresql/9.6/bin
./pg_ctl stop
mistige
  • 619
  • 7
  • 11