<p>I am trying to mount a persistent disk on my container which runs a Postgres custom image. I am using Kubernetes and following <a href="https://cloud.google.com/container-engine/docs/tutorials/persistent-disk/" rel="nofollow noreferrer">this</a> tutorial.</p>
<p>This is my <code>db_pod.yaml</code> file:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: lp-db
  labels:
    name: lp-db
spec:
  containers:
    - image: my_username/my-db
      name: my-db
      ports:
        - containerPort: 5432
          name: my-db
      volumeMounts:
        - name: pg-data
          mountPath: /var/lib/postgresql/data
  volumes:
    - name: pg-data
      gcePersistentDisk:
        pdName: my-db-disk
        fsType: ext4
</code></pre>
<p>I create the disk using the command <code>gcloud compute disks create --size 200GB my-db-disk</code>.</p>
<p>However, when I run the pod, delete it, and then run it again (like in the tutorial) my data is not persisted.</p>
<p>I tried multiple versions of this file, including with PersistentVolumes and PersistentVolumeClaims, I tried changing the mountPath, but to no success.</p>
<h2>Edit</h2>
<p>Dockerfile for creating the Postgres image:</p>
<pre><code>FROM ubuntu:trusty
RUN rm /bin/sh &amp;&amp; \
    ln -s /bin/bash /bin/sh

# Get Postgres
RUN echo &quot;deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main&quot; &gt;&gt; /etc/apt/sources.list.d/pgdg.list
RUN apt-get update &amp;&amp; \
    apt-get install -y wget
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Install virtualenv (will be needed later)
RUN apt-get update &amp;&amp; \
    apt-get install -y \
        libjpeg-dev \
        libpq-dev \
        postgresql-9.4 \
        python-dev \
        python-pip \
        python-virtualenv \
        strace \
        supervisor


# Grab gosu for easy step-down from root
RUN gpg --keyserver pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4
RUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends ca-certificates wget &amp;&amp; rm -rf /var/lib/apt/lists/* \
    &amp;&amp; wget -O /usr/local/bin/gosu &quot;https://github.com/tianon/gosu/releases/download/1.2/gosu-$(dpkg --print-architecture)&quot; \
    &amp;&amp; wget -O /usr/local/bin/gosu.asc &quot;https://github.com/tianon/gosu/releases/download/1.2/gosu-$(dpkg --print-architecture).asc&quot; \
    &amp;&amp; gpg --verify /usr/local/bin/gosu.asc \
    &amp;&amp; rm /usr/local/bin/gosu.asc \
    &amp;&amp; chmod +x /usr/local/bin/gosu \
    &amp;&amp; apt-get purge -y --auto-remove ca-certificates wget

# make the &quot;en_US.UTF-8&quot; locale so postgres will be utf-8 enabled by default
RUN apt-get update &amp;&amp; apt-get install -y locales &amp;&amp; rm -rf /var/lib/apt/lists/* \
    &amp;&amp; localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8

# Adjust PostgreSQL configuration so that remote connections to the database are possible.
RUN echo &quot;host all  all    0.0.0.0/0  md5&quot; &gt;&gt; /etc/postgresql/9.4/main/pg_hba.conf

# And add ``listen_addresses`` to ``/etc/postgresql/9.4/main/postgresql.conf``
RUN echo &quot;listen_addresses='*'&quot; &gt;&gt; /etc/postgresql/9.4/main/postgresql.conf
RUN echo &quot;log_directory='/var/log/postgresql'&quot; &gt;&gt; /etc/postgresql/9.4/main/postgresql.conf

# Add all code from the project and all config files
WORKDIR /home/projects/my-project
COPY . .

# Add VOLUMEs to allow backup of config, logs and databases
ENV PGDATA /var/lib/postgresql/data
VOLUME /var/lib/postgresql/data

# Expose an entrypoint and a port
RUN chmod +x scripts/sh/*
EXPOSE 5432
ENTRYPOINT [&quot;scripts/sh/entrypoint-postgres.sh&quot;]
</code></pre>
<p>And entrypoint script:</p>
<pre><code>echo &quot; I am &quot; &amp;&amp; gosu postgres whoami

gosu postgres /etc/init.d/postgresql start &amp;&amp; echo 'Started postgres'
gosu postgres psql --command &quot;CREATE USER myuser WITH SUPERUSER PASSWORD 'mypassword';&quot;  &amp;&amp; echo 'Created user'
gosu postgres createdb -O myuser mydb &amp;&amp; echo 'Created db'

# This just keeps the container alive.
tail -F /var/log/postgresql/postgresql-9.4-main.log
</code></pre>

<p>In the end, it seems that the real problem was the fact that I was trying to create the database from my entrypoint script. 
Things such as creating a db or a user should be done <em>at container creation time</em> so I ended up using the standard Postgres image, which actually provides a simple and easy way to create an user and a db.</p>

<p>This is the fully functional configuration file for Postgres.</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: postgres
  labels:
    name: postgres
spec:
  containers:
    - name: postgres
      image: postgres
      env:
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: POSTGRES_USER
          value: myuser
        - name: POSTGRES_PASSWORD
          value: mypassword
        - name: POSTGRES_DB
          value: mydb
      ports:
        - containerPort: 5432
      volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: pg-data
  volumes:
    - name: pg-data
      persistentVolumeClaim:
        claimName: pg-data-claim
</code></pre>

<p>Thanks to all those who helped me :)</p>
