2

I have the following docker-compose.yml file:

version: "3.7"

services:
  dc-patients:
    build:
      context: .
      dockerfile: ./Dockerfile
      labels:
        service: "patients"
    image: hubaccount/patients:latest
    ports:
      - "8081:8080"
    env_file:
      - ./variables.env
    depends_on:
      - dc-patients-db
  dc-patients-db:
    image: postgres:16.2
    ports:
      - "8005:5432"
    volumes:
      - data-volume:/var/lib/postgresql/data
    env_file:
      - ./variables.env

volumes:
  data-volume:

When I run docker compose up -d this runs fine. My API in the dc-patients container runs fine and so does my database container.

The issue is, the database is empty and because I'm in the development phase, I want some test data in my patients table (e.g. 100 records). Right now I have to manually connect to the database and manually seed some test data in there.

Is it possible to have docker compose up -d run the container but also seed the patients table in the Postgres container with a 100 records (I can get a CSV file that has the test data).

And have docker compose down clear the data.

This way I can schedule a corn job on my VPS to run docker compose down followed by docker compose up -d every night so that my env always resets and I have data to work with?

I'm not really sure if such a thing is even doable, but I thought I'd ask!

1 Answer 1

2

Have a psql script run to load the database at startup:

  dc-patients-db:
    image: postgres:16.2
    ports:
      - "8005:5432"
    volumes:
      - data-volume:/var/lib/postgresql/data
      - ./seed.sql:/tmp/seed.sql:rw
    command: sh -c "psql -p 5432 postgres < /tmp/seed.sql"
    env_file:
      - ./variables.env

So it does not keep reloading the same data, you might want seed.sql to include steps to drop the existing database then re-create it.

2
  • I don't know what I'm missing, but I'm getting the following error: ~/code/dc/patients main* ❯ docker logs d97 psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? It is like it can't connect to psql inside the container!
    – J86
    Apr 9 at 22:20
  • @J86 maybe environment variables need to be set? I see the variables.env--does that include settings for POSTGRES_USER and POSTGRES_PASSWORD? You might need to log into the container and find out what exactly is happening. Also, see stackoverflow.com/questions/31645550/… Apr 10 at 1:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.