/ db-seeds / postgres / docker-compose.yml
docker-compose.yml
 1  # A Docker Compose must always start with the version tag.
 2  # We use '3' because it's the last version.
 3  version: '3'
 4  
 5  # You should know that Docker Compose works with services.
 6  # 1 service = 1 container.
 7  # For example, a service, a server, a client, a database...
 8  # We use the keyword 'services' to start to create services.
 9  services:
10    # The name of our service is "database"
11    # but you can use the name of your choice.
12    # Note: This may change the commands you are going to use a little bit.
13    database:
14      # Official Postgres image from DockerHub (we use the last version)
15      image: 'postgres:latest'
16  
17      # By default, a Postgres database is running on the 5432 port.
18      # If we want to access the database from our computer (outside the container),
19      # we must share the port with our computer's port.
20      # The syntax is [port we want on our machine]:[port we want to retrieve in the container]
21      # Note: You are free to change your computer's port,
22      # but take into consideration that it will change the way
23      # you are connecting to your database.
24      ports:
25        - 5432:5432
26  
27      environment:
28        POSTGRES_USER: postgres # The PostgreSQL user (useful to connect to the database)
29        POSTGRES_PASSWORD: admin # The PostgreSQL password (useful to connect to the database)
30        POSTGRES_DB: newdb # The PostgreSQL default database (automatically created a
31  
32  
33    db-seed:
34      image: "bitnami/postgresql:12" # postgres + network tools
35      depends_on:
36        - database
37      environment:
38        - ALLOW_EMPTY_PASSWORD=yes
39        - SEED_DB=${SEED_DB}
40      command: bash -c "until pg_isready -h database -U postgres -t 10; do echo '..waiting for db' ; sleep 1; done; echo 'db up'; cd /database;cat /database/ddl.sql /database/ddl_*.sql > /database/final_seed.sql && PGPASSWORD=admin psql -h database -U postgres -d newdb <  /database/final_seed.sql && rm /database/final_seed.sql"
41      volumes:
42        - './:/database'