0

My docker-compose file:

version: '3'

services:
  app:
    container_name: application
    build: .cloud/php
    image: app-application
    depends_on:
      - pgres
    ports:
      - "9050:9000"
    volumes:
      - ./:/var/www:cached
    networks:
      - application_network

  nginx:
    container_name: application.nginx
    image: nginx
    ports:
      - "8050:8000"
    volumes:
      - .cloud/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:cached
      - ./:/var/www:cached
    depends_on:
      - app
    networks:
      - application_network

  pgres:
    container_name: application.postgres
    image: postgres
    restart: always
    ports:
      - "54325:5432"
    environment:
      POSTGRES_DB: application
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: example
      PGDATA: /tmp
    volumes:
      - .cloud/postgres/data:/var/lib/postgresql/data
    networks:
      - application_network
networks:
  application_network:
    driver: bridge

When I run docker-compose down, postgresql data is not persisting and database resets completely. I tried to put the postgresql volume into its own /postgresql/data folder but still same result. What am I missing here?

3
  • 1
    I think it has to do something with the way you're using PGDATA. If you don't understand it, I would remove it. On Docker Hub they're suggesting to define it as a subdirectory of /var/lib/postgresql/data. I also looked at the docker-entrypoint.sh but didn't quite understand how they're using that env variable. Commented Mar 26, 2020 at 19:46
  • Perhaps, the other thing you could try is .cloud/postgres/data:/tmp instead of .cloud/postgres/data:/var/lib/postgresql/data. Commented Mar 26, 2020 at 19:52
  • Thanks for the suggestions, removing PGDATA did the trick. Commented Mar 27, 2020 at 18:39

1 Answer 1

4

The problem is here

    environment:
      PGDATA: /tmp
    volumes:
      - .cloud/postgres/data:/var/lib/postgresql/data

With the PGDATA env var, you are setting the cluster data directory explicitly. So, what it does is, once you start your container (e.g. via docker-compose up), Postgres server will persist your db data as per your PGDATA setting within the /tmp directory. On the other hand you are mounting a different path in the container (/var/lib/postgresql/data) instead of the /tmp path. Thus once you run docker-compose down, the container and the persisted data in /tmp will be gone for good, since /tmp is not set as mounting point. So, unless you really need to do so, you better not touch it at all.

A working configuration might look so (shortened for the sake of brevity):

version: '3'
services:
  pgres:
    image: "postgres" # use latest official postgres version or a specific version e.g. postgres:10.12
    volumes:
      - .cloud/postgres/:/var/lib/postgresql/data/ # persist data even if container shuts down

Another thing is, even let's say you configured it via PGDATA like so

    environment:
      PGDATA: /tmp
    volumes:
      - .cloud/postgres/data:/tmp

It wouldn't be a good idea, since the path /tmp is in your case, as the name tmp (temporary) suggests, really not reliable. docker-compose down will probably not affect it at all, but on the next boot, i.e. once you run docker-compose up again, the underlying OS (Linux) will very much likely delete the content of the directory /tmp, therefore the mounted local dir .cloud/postgres/data will be also immediately emptied and your persisted data will be gone. In any case the /tmp dir is somewhat managed by Linux, thus the data saved there might simply disappear any time - so don't rely on it!

Sign up to request clarification or add additional context in comments.

2 Comments

Teşekkürler, PGDATA env kaldırınca çalıştı :) For the future readers, removing PGDATA from env solves the problem.
I removed PGDATA, then it was fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.