Want to populate Posgress database in docker automatically in docker-compose

62 Views Asked by At

I want the data_dump.sql to load when the container is first created I do not want it to dump data the second time when the container is stopped and started again, But first I am not even able to load by database dump in my database even when it is started.

I have added a volume in the Postgres service with data_dump_sql file but it was not loaded into the database even when I started the containers with docker-compose up(I even tried fully stoping the containers with docker-compose down) and started them again but the data has not been added into the database.

version: "3.8"

services:
  postgres:
    image: postgres:latest
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB=nanomedicine_db
    ports:  #outside:inside(container)
      - 5432:5432
    networks:
      - shared-network
    volumes:
      - postgres-volume:/var/lib/postgresql/data
      - ./devops/db/dummy_dump.sql:/docker-entrypoint-initdb.d/dummy_dump.sql
      # - ./backup-files:/backup-files

  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: root
    ports:
      - 5050:80
    networks:
      - shared-network
    depends_on:
      - postgres
    volumes:
      - pgadmin-volume:/var/lib/pgadmin
  
  nginx:
    container_name: nginx_container
    restart: always
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 3050:90
    networks:
      - shared-network
    depends_on:
      - server
      - client

  server:
    container_name: nanomedicine_server
    restart: unless-stopped
    image: nanomedicine-server-image:1.0.0
    build:
      context: nanomedicine-backend 
      target: production
      dockerfile: Dockerfile
    ports:  #outside:inside(container)
      - 8080:8080
    networks:
      - shared-network
    depends_on:
      - postgres

  client:
    container_name: nanomedicine_client
    restart: unless-stopped
    image: nanomedicine-client-image:1.0.0
    build:
      context: nanomedicine-frontend 
      target: production
      dockerfile: Dockerfile
    ports:  #outside:inside(container)
      - 3000:3000

    networks:
      - shared-network
    depends_on:
      - server
volumes:
  postgres-volume:
  pgadmin-volume:
networks:
  shared-network:

Data in SQL dump

I have created a dummy sql file with dummy data so that I can check if a table with data will be created in my already existing database with peristant data because of volumes. I was expecting a new table called 'dummyTable' to appear in pgadmin tables but only the old tables from postgres-volume are appering.

This is the pgadmin image of all tables available in pgadmin of docker. docker pgadmin

To get a way to populate my posgress database in docker to populate automatically through docker compose

0

There are 0 best solutions below