Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create two postgres database when docker-compose up?

Dockerfile:

FROM python:3.9
ENV PYTHONUNBUFFERED=1
RUN apt-get update && apt-get upgrade -y  \
&& apt-get install -y gcc gunicorn3 libcurl4-gnutls-dev librtmp-dev libnss3 libnss3-dev wget \
&& apt-get clean \
&& apt -f install -y

WORKDIR /App

COPY requirements.txt /App/
RUN pip install -r requirements.txt
COPY . /App/
>! RUN pip install django~=4.1.1

RUN mkdir -p /App/data/db/
RUN chown -R 1000:1000 /App/data/db/

EXPOSE 7000
EXPOSE 8001

`

I have base-compose file that contain database image, here is file content:

version: '3.3'

networks:
  shared_network:
   driver: bridge

services:
  testdb:
    image: postgres:latest
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=develop_db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    networks:
      - shared_network

volumes:
  postgres_data:

Here is my docker-compose file:

version: '3.3'

include:
  - ./base-compose.yml

services:
  test_develop:
    container_name: test_develop
    build: .
    command: python manage.py runserver 0.0.0.0:7000
    ports:
      - "7000:7000"
    env_file:
      - ./environments/.env.develop
    depends_on:
      - testdb
    networks:
      - shared_network
    links:
      - testdb

Here is my docker-compose-prod.yml file:

version: '3.3'

include:
  - ./base-compose.yml

services:
  test_production:
    container_name: test_production
    build: .
    command: python manage.py runserver 0.0.0.0:8001
    ports:
      - "8001:8001"
    env_file:
      - ./environments/.env.prod
    depends_on:
      - testdb
    networks:
      - shared_network
    links:
      - testdb

when i run the docker-compose up --build it create develop_db but i want to create prod_db too.

I try to create two database names develop_db and prod_db, when docker-compose up --build.

I used these two commands to run both docker-compose file.

docker-compose -f docker-compose up --build
docker-compose -f docker-compose-prod.yml up --build
like image 856
spartan Avatar asked Jun 23 '26 22:06

spartan


1 Answers

I just created init.sql file and add it into docker-entrypoint-init.db, this is how i did. Its better to create psql folder and add init.sql file into it.

Remove - POSTGRES_DB=develop_db this from the base-compose file and change it.

version: '3.3'

networks:
  shared_network:
   driver: bridge

services:
  testdb:
    image: postgres:latest
    volumes:
      - postgres_data:/var/lib/postgresql/data
      # add here 
      - ./psql/init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    networks:
      - shared_network

volumes:
  postgres_data:

Here is my init.sql file. I just created DATABASE simply make sure you create DATABASE using USER & PASSWORD.

-- Creation of DATABASE

CREATE DATABASE test_db;
CREATE DATABASE test_prod_db;

[enter image description here enter image description here enter image description here enter image description here

like image 163
Tanveer Avatar answered Jun 25 '26 11:06

Tanveer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!