I have the following deployment yaml that takes a postgres image and runs it in a pod:
1 apiVersion: apps/v1
1 kind: Deployment
2 metadata:
3 name: postgres-deployment
4 spec:
5 replicas: 1
6 selector:
7 matchLabels:
8 component: postgres
9 template:
10 metadata:
11 labels:
12 component: postgres
13 spec:
14 initContainers:
15 - name: change-user
16 image: busybox:latest
17 command: ["sh", "-c", "chown -R 999:999 /var/lib/postgresql/data"]
18 securityContext:
19 runAsUser: 999
20 volumes:
21 - name: postgres-storage
22 persistentVolumeClaim:
23 claimName: postgres-persistent-volume-claim
24 containers:
25 - name: postgres
26 image: prikshet/postgres
27 ports:
28 - containerPort: 5432
29 volumeMounts:
30 - name: postgres-storage
31 mountPath: /var/lib/postgresql/data
32 subPath: postgres
33 imagePullPolicy: Always
Before having an initContainer, I was getting the error that /var/lib/postgresql/data had wrong ownership so I created an initContainer to fix those ownership and also tried to change the securityContext. The initContainer gives an error on the pod startup:
chown: /var/lib/postgresql/data: No such file or directory
How to fix this error?
My postgres container
1 FROM postgres:latest
1 COPY deployment/postgres_init /docker-entrypoint-initdb.d
2 USER 999
3 RUN initdb
4 CMD postgres -c hba_file=docker-entrypoint-initdb.d/pg_hba.conf -c config_fi le=docker-entrypoint-initdb.d/postgresql.conf