tutiplain
tutiplain

Reputation: 1480

PostgreSQL docker container not writing data to disk

I am having some difficulty with docker and the postgres image from the Docker Hub. I am developing an app and using the postgres docker to store my development data. I am using the following command to start my container:

sudo docker run --name  some-postgresql -e POSTGRES_DB=AppDB  -e POSTGRES_PASSWORD=App123! -e POSTGRES_USER=appuser -e PGDATA="/pgdata" --mount source=mydata,target=/home/myuser/pgdata  -p 5432:5432/tcp postgres

When I finish working on my app, I usually have to run "docker container prune", in order to free up the container name and be able to run it again later. This worked until recently, when I upgraded my postgres image to run version 11 of PostgreSQL. Now, when I start my container and create data in it, the next time I use it the data is gone. I've been reading about volumes in the docker documentation cannot find anything that can tell my why this is not working. Can anyone please shed some light on this?

Upvotes: 1

Views: 1541

Answers (2)

isapir
isapir

Reputation: 23503

Specify a volume mount with -v $PGDATA_HOST:/var/lib/postgresql/data.

The default PGDATA inside the container is /var/lib/postgresql/data so there is no need to change that if you're not modifying the Docker image.

e.g. to mount the data directory on the host at /srv/pgdata/:

$ PGDATA_HOST=/srv/pgdata/
$ docker run -d -p 5432:5432 --name=some-postgres \
       -e POSTGRES_PASSWORD=secret                \
       -v $PGDATA_HOST:/var/lib/postgresql/data   \
       postgres

The \ are only needed if you break the command over multiple lines, which I did here for the sake of clarity.

Upvotes: 1

Thomasleveil
Thomasleveil

Reputation: 103955

since you specified -e PGDATA="/pgdata", the database data will be written to /pgdata within the container. If you want the files in /pgdata to survive container deletion, that location must be a docker volume. To make that location a docker volume, use --mount source=mydata,target=/pgdata.

In the end, it would be simpler to just run:

sudo docker run --name  some-postgresql -e POSTGRES_DB=AppDB  -e POSTGRES_PASSWORD=App123! -e POSTGRES_USER=appuser --mount source=mydata,target=/var/lib/postgresql/data  -p 5432:5432/tcp postgres

Upvotes: 0

Related Questions