title: How to persist data in a dockerized postgres database using volumes
body: <p>My docker compose file has three containers, web, nginx, and postgres. Postgres looks like this:</p>
  <pre><code>postgres:
    container_name: postgres
    restart: always
    image: postgres:latest
    volumes:
      - ./database:/var/lib/postgresql
    ports:
      - 5432:5432
  </code></pre>
  <p>My goal is to mount a volume which corresponds to a local folder called <code>./database</code> inside the postgres container as <code>/var/lib/postgres</code>. When I start these containers and insert data into postgres, I verify that <code>/var/lib/postgres/data/base/</code> is full of the data I'm adding (in the postgres container), but in my local system, <code>./database</code> only gets a <code>data</code> folder in it, i.e. <code>./database/data</code> is created, but it's empty. Why?</p>
  <p>Notes:</p>
  <ul>
  <li><a href="https://github.com/docker/compose/issues/2971" rel="noreferrer">This suggests my above file should work.</a></li>
  <li><a href="https://stackoverflow.com/questions/39175194/docker-compose-persistent-data-mysql">This person is using docker services which is interesting</a></li>
  </ul>
  <h3>UPDATE 1</h3>
  <p>Per Nick's suggestion, I did a <code>docker inspect</code> and found:</p>
  <pre><code>    &quot;Mounts&quot;: [
          {
              &quot;Source&quot;: &quot;/Users/alex/Documents/MyApp/database&quot;,
              &quot;Destination&quot;: &quot;/var/lib/postgresql&quot;,
              &quot;Mode&quot;: &quot;rw&quot;,
              &quot;RW&quot;: true,
              &quot;Propagation&quot;: &quot;rprivate&quot;
          },
          {
              &quot;Name&quot;: &quot;e5bf22471215db058127109053e72e0a423d97b05a2afb4824b411322efd2c35&quot;,
              &quot;Source&quot;: &quot;/var/lib/docker/volumes/e5bf22471215db058127109053e72e0a423d97b05a2afb4824b411322efd2c35/_data&quot;,
              &quot;Destination&quot;: &quot;/var/lib/postgresql/data&quot;,
              &quot;Driver&quot;: &quot;local&quot;,
              &quot;Mode&quot;: &quot;&quot;,
              &quot;RW&quot;: true,
              &quot;Propagation&quot;: &quot;&quot;
          }
      ],
  </code></pre>
  <p>Which makes it seem like the data is being stolen by another volume I didn't code myself. Not sure why that is. Is the postgres image creating that volume for me? If so, is there some way to use <em>that volume</em> instead of the volume I'm mounting when I restart? Otherwise, is there a good way of disabling that other volume and using my own, <code>./database</code>?</p>
  <h3>UPDATE 2</h3>
  <p>I found the solution, thanks to Nick! (and another friend) Answer below.</p>