Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres does not keep data after docker-compose down #1445

Closed
benkelukas opened this issue Mar 20, 2018 · 14 comments
Closed

Postgres does not keep data after docker-compose down #1445

benkelukas opened this issue Mar 20, 2018 · 14 comments
Labels

Comments

@benkelukas
Copy link

benkelukas commented Mar 20, 2018

Info:

  • Docker version ($ docker --version): Docker version 17.12.0-ce, build c97c6d6
  • Laradock commit ($ git rev-parse HEAD): 9a9d30f
  • System info (Mac, PC, Linux): Linux
  • System info disto/version: Ubuntu 17.04

Issue:

No data gets created for postgres, the data/postgres directory is empty even after running migrations and filling some data into tables. As a consequence, whenever i do docker-compose down and then docker-compose up the database is empty. Also, i noticed that the postgres data folder is owned by root, but this worked with mysql, mysql retained data in database even after container restart.


Expected behavior:

Postgres should retain data between container restart


Reproduce:

Clone laradock
Set data path in .env to ./data
Start containers
Setup posgtres
Create some tables
fill tables with test data
Stop containers
Start containers - postgres is empty


Relevant Code:

laradock .env

DATA_SAVE_PATH=./data

Here is my full .env - https://pastebin.com/0LU7G5wx

Edit

It also does not work when setting DATA_SAVE_PATH to ~/.laradock/data

Edit 2

It works for MySQL, the data folder gets created with 999:docker group and owner

@benkelukas
Copy link
Author

benkelukas commented Mar 20, 2018

@bestlong
Alrady tried that, same result. The ./data/postgres/data folder is owned by root:root, but when using MySQL it is owned by 999:docker - i think that is the issue, but i do not know what to do to fix it. I'm running docker without sudo, so that should not be a problem.

@bestlong
Copy link
Member

run

docker-compose up postgres

hav any error message?

@benkelukas
Copy link
Author

I do not think so, here is the full output: https://pastebin.com/SdhQxk50

@bestlong
Copy link
Member

make sure docker-compose.yml about postgresql part like:

### PostgreSQL Container ####################################

    postgres:
      build: ./postgres
      volumes:
        - ${DATA_SAVE_PATH}/postgres:/var/lib/postgresql
      ports:
        - "${POSTGRES_PORT}:5432"
      environment:
        - POSTGRES_DB=${POSTGRES_DB}
        - POSTGRES_USER=${POSTGRES_USER}
        - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      networks:
        - backend

and build image again

docker-compose build postgres

@benkelukas
Copy link
Author

Tried that, did not work. This is diff of your config (left) and my config (right): https://www.diffchecker.com/VCVy7Dyc

I ssh'd into the postgres container: docker-compose exec --user=root postgres bash
Then i listed the directory with data: ls -la /var/lib/postgresql/data/
This is the output:

drwx------   19 postgres postgres      4096 Mar 20 08:44 .
drwxr-xr-x    3 root     root          4096 Mar 20 08:37 ..
-rw-------    1 postgres postgres         3 Mar 20 08:41 PG_VERSION
drwx------    6 postgres postgres      4096 Mar 20 08:41 base
drwx------    2 postgres postgres      4096 Mar 20 08:45 global
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_commit_ts
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_dynshmem
-rw-------    1 postgres postgres      4535 Mar 20 08:41 pg_hba.conf
-rw-------    1 postgres postgres      1636 Mar 20 08:41 pg_ident.conf
drwx------    4 postgres postgres      4096 Mar 20 08:44 pg_logical
drwx------    4 postgres postgres      4096 Mar 20 08:41 pg_multixact
drwx------    2 postgres postgres      4096 Mar 20 08:44 pg_notify
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_replslot
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_serial
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_snapshots
drwx------    2 postgres postgres      4096 Mar 20 08:44 pg_stat
drwx------    2 postgres postgres      4096 Mar 20 08:46 pg_stat_tmp
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_subtrans
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_tblspc
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_twophase
drwx------    3 postgres postgres      4096 Mar 20 08:41 pg_wal
drwx------    2 postgres postgres      4096 Mar 20 08:41 pg_xact
-rw-------    1 postgres postgres        88 Mar 20 08:41 postgresql.auto.conf
-rw-------    1 postgres postgres     22729 Mar 20 08:41 postgresql.conf
-rw-------    1 postgres postgres        24 Mar 20 08:44 postmaster.opts
-rw-------    1 postgres postgres        94 Mar 20 08:44 postmaster.pid

So the directories are in the container, but they do not get written onto host disk.

@bestlong
Copy link
Member

bestlong commented Mar 20, 2018

$ docker-compose up -d postgres
$ docker inspect laradock_postgres_1

you will get detail info.

my result a part:

        "HostConfig": {
            "Binds": [
                "/Users/allen/.laradock/data/postgres:/var/lib/postgresql:rw"
            ],
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/allen/.laradock/data/postgres",
                "Destination": "/var/lib/postgresql",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "7a8161eb3d95633f8676065b436edafdf5f0ac669375310d8bd13dfa48512596",
                "Source": "/var/lib/docker/volumes/7a8161eb3d95633f8676065b436edafdf5f0ac669375310d8bd13dfa48512596/_data",
                "Destination": "/var/lib/postgresql/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
            "Volumes": {
                "/var/lib/postgresql": {},
                "Source": "/Users/allen/.laradock/data/postgres",
                "Destination": "/var/lib/postgresql",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "7a8161eb3d95633f8676065b436edafdf5f0ac669375310d8bd13dfa48512596",
                "Source": "/var/lib/docker/volumes/7a8161eb3d95633f8676065b436edafdf5f0ac669375310d8bd13dfa48512596/_data",
                "Destination": "/var/lib/postgresql/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }

@benkelukas
Copy link
Author

benkelukas commented Mar 20, 2018

Everything looks fine, here is extracted info relating to volumes:

"HostConfig": {
            "Binds": [
                "/media/benke/CODE/laradock-postgress-issue-reproduce/laradock_pg_issue_rep/data/postgres:/var/lib/postgresql:rw"
            ],
"Mounts": [
            {
                "Type": "bind",
                "Source": "/media/benke/CODE/laradock-postgress-issue-reproduce/laradock_pg_issue_rep/data/postgres",
                "Destination": "/var/lib/postgresql",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "1c9fe3924f7074ba6167067857c4ca18e657f681b49ed5369b4d5626a2778b8c",
                "Source": "/var/lib/docker/volumes/1c9fe3924f7074ba6167067857c4ca18e657f681b49ed5369b4d5626a2778b8c/_data",
                "Destination": "/var/lib/postgresql/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

Do you see anything wrong? Here is the full output: https://pastebin.com/ysEiYWfLput

@benkelukas
Copy link
Author

Solved, see here: https://stackoverflow.com/questions/41637505/how-to-persist-data-in-a-dockerized-postgres-database-using-volumes

The gist of it is to change this:

### PostgreSQL Container ####################################

    postgres:
      build: ./postgres
      volumes:
        - ${DATA_SAVE_PATH}/postgres:/var/lib/postgresql
    ...

to this

### PostgreSQL Container ####################################

    postgres:
      build: ./postgres
      volumes:
        - ${DATA_SAVE_PATH}/postgres:/var/lib/postgresql/data
    ...

Note the extra data after /postgresql/

i think we should update laradock's docker-compose.yml to reflect this behaviour.

@bestlong
Copy link
Member

my os is MAC, i will try it again at ubuntu .

@bestlong
Copy link
Member

i think #1421 can fix this.

@benkelukas
Copy link
Author

Yes, that should do it, will have to wait for the build to pass.

@garbinmarcelo
Copy link
Contributor

Any updates on this issue?
O.S.: Windows 10

@stale
Copy link

stale bot commented Feb 19, 2020

Hi 👋 this issue has been automatically marked as stale 📌 because it has not had recent activity 😴. It will be closed if no further activity occurs. Thank you for your contributions ❤️.

@stale stale bot added the Stale label Feb 19, 2020
@stale
Copy link

stale bot commented Mar 11, 2020

Hi again 👋 we would like to inform you that this issue has been automatically closed 🔒 because it had not recent activity during the stale period. We really really appreciate your contributions, and looking forward for more in the future 🎈.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants