1

I have the following compose file, from this project:

version: "3"

volumes:
  static-files:
    driver: local
  postgres:
    driver: local

services:
    db:
      image: postgres:11.1
      volumes:
        - postgres:/var/lib/postgresql/data/pgdata
      env_file:
        - ./config/environment/development.env
      expose:
        - "5432"

    webapp:
      build:
        context: webapp
      volumes:
        - ./webapp/covidoff:/srv/covidoff
        - static-files:/srv/static-files
      ports:
        - "8000:8000"
        - "80:8000"
      depends_on:
        - db
      env_file:
        - ./config/environment/development.env

As you can see, there are two services being launched: a PostgreSQL database db and a Django webapp. Everything is working well so far, except for one thing: every time I stop the db container the data is lost.

Someone told me to try postgres:/var/lib/postgresql/data instead of postgres:/var/lib/postgresql/data/pgdata, but that didn't work either.

The problem is: this server is already live and with data. How can I solve the persistence problem and still keep the current data?

Some progress. A simple change made it so that the data persists after the container is stopped, I'm just not sure why. I replaced this:

PGDATA=pgdata

With this:

PGDATA=/var/lib/postgresql/data/pgdata

In my env file. But why does this work?

1 Answers1

1

The docker volume driver is not persisting your data between restarts of services. You can use the local-persist driver ; I have used this several times for this use case.

When you already have the data somewhere you can create the volume before starting the services and copy your data to the specified directory. Postgres should pick up the database from there.

OYPS
  • 58
  • 7
  • Great, thanks! You know how I can create the volume before starting the services? – André Fratelli Apr 04 '20 at 17:15
  • That’s described on the GitHub page. After installing the driver simply call: docker volume create -d local-persist -o mountpoint=/Data/path —name=volume_name – OYPS Apr 04 '20 at 17:20
  • Thank you! I'll give a try – André Fratelli Apr 04 '20 at 17:20
  • I already managed to use persistence (see my edit), but I'm not sure about the second step. How do I do that? – André Fratelli Apr 06 '20 at 07:23
  • `PGDATA` in your environment file overwrites the data directory postgres uses to store its data. `PGDATA=pgdata` seems to be nowhere, so the data was not persisted. You wrote that the server was already live and you have data? you can either export that (create a database dump) or copy the content of this installation to the newly created directory of the postgres container. A database dump is preferred here – OYPS Apr 06 '20 at 07:36
  • Got it. So just `pgdata` doesn't map anywhere? The data must liver somewhere, right? Couldn't I just copy that volume, wherever it is? – André Fratelli Apr 06 '20 at 07:45
  • But yes, I'm working on a data dump. Just want to know what my options are. – André Fratelli Apr 06 '20 at 07:46
  • I recovered data before by simply copying the data/volume to a new database installation, but that is not the desired way. If you have the chance to export the data, go ahead with that way! Thats the 'normal' and supported solution – OYPS Apr 06 '20 at 07:47