Knows Not Much
Knows Not Much

Reputation: 31546

why is PGDATA directory empty?

I created a docker image with the following docker-compose.yml file


    version: "3.7"
    services: 
      db:
        image: postgres:alpine
        container_name: db
        environment:
          POSTGRES_USER: ${POSTGRES_USER}
          POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
          POSTGRES_DB: ${POSTGRES_DB}
          POSTGRES_INITDB_ARGS: '-A md5'
        volumes:
          - ./pgdata:/var/lib/postgressql/data
        ports:
          - "5432:5432"
        healthcheck:
          test: ["CMD", "psql", "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}"]
          interval: 30s
          timeout: 5s
          retries: 5
      api:
        build: api
        container_name: api
        volumes:
          - ./api/migrations:/migrations
        ports:
          - "8080:8080"
        links:
          - db
        depends_on:
          db:
            condition: service_healthy

when I do docker-compose up everything is working fine. I am able to connect to Postgres, I can create tables. I can query those tables.

The only problem is that the ./pgdata directory is empty! why is that? since I have done volumes: - ./pgdata:/var/lib/postgressql/data I should have some files getting created in this directory as I create databases and tables right?

I did ls -al command in the pgdata directory and it shows nothing.

Upvotes: 0

Views: 2155

Answers (1)

myeongkil kim
myeongkil kim

Reputation: 2586

I entered the docker-hub-psql-site and checked the working directory. https://hub.docker.com/_/postgres

In the case of postgresql, it was set as the PGDATA environment variable, and it was as follows. Dockerfile

...
ENV PGDATA=/var/lib/postgresql/data
...

In other words, I have confirmed that there is a typo postgressql in your docker-compose.yaml, and it will be fixed if you modify it as follows.

version: "3.7"
services: 
  db:
    image: postgres:alpine
    container_name: db
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_INITDB_ARGS: '-A md5'
    volumes:
#     - ./pgdata:/var/lib/postgressql/data
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD", "psql", "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}"]
      interval: 30s
      timeout: 5s
      retries: 5

[NOTE] Apart from the question, I know that the healthcheck option in docker-compose only supports version 2. Please refer to the article below stackoverflow/docker-compose-healthcheck-does-not-work

Upvotes: 2

Related Questions