how do i backup a database in docker
<p>i'm running my app using docker-compose with the below yml file</p>

<pre><code>  postgres:
    container_name: postgres
    image: postgres:${POSTGRES_VERSION}
    volumes:
      - postgresdata:/var/lib/postgresql/data
    expose:
      - "5432"
    environment:
      - POSTGRES_DB=42EXP
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

  node:
    container_name: node
    links:
      - postgres:postgres
    depends_on:
      - postgres

volumes:
  postgresdata:
</code></pre>

<p>As you can see  here ,i'm using a <code>named volume</code> to manage postgres state.</p>

<p>According to the official docs, i can backup a volume like the below</p>

<pre><code>docker run --rm --volumes postgresdata:/var/lib/postgresql/data -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
</code></pre>

<p>Some other tutorials suggested i use the <code>pg-dump</code> function provided by postgres for backups.</p>

<pre><code>pg_dump -Fc database_name_here &gt; database.bak
</code></pre>

<p>I guess i would have to go inside the postgres container to perform this function and mount the backup directory to the host.</p>

<p>Is one approach better/preferable than the other?</p>