skip to Main Content

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

2

Answers


  1. You can control this generally through environment variables.

    A key concept here is that Compose has the notion of a project. Everything in a Compose file is associated with a project, and the default Docker object names that Compose produces embed the project name. You can override the project name using a docker-compose -p option or the COMPOSE_PROJECT_NAME environment variable. The project name defaults to the current directory name, so if you want to run two Compose setups from the same directory, you need to set the project name for one or the other.

    The related note here is that your two Compose files are almost identical, except for the container names (which Compose can assign), the Docker-internal port number (which doesn’t need to change), the specific environment file, and the host port. You could flatten this out into a single Compose file that works in both environments, with the correct settings

    version: '3.8'
    
    services:
      db:
        image: postgres:latest
        volumes:
          - postgres_data:/var/lib/postgresql/data
        environment:
          - POSTGRES_DB=develop_db
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
    
     test:
        build: .
        ports:
          - "${HOST_PORT:-8000}:8000"
        env_file:
          - ./environments/.env.${COMPOSE_PROJECT_NAME}
        depends_on:
          - db
    
    volumes:
      postgres_data:
    

    Now you can run

    COMPOSE_PROJECT_NAME=develop HOST_PORT=7000 docker-compose up -d --build
    COMPOSE_PROJECT_NAME=prod HOST_PORT=8000 docker-compose up -d --build
    

    Compose will create a separate copy of this stack for each COMPOSE_PROJECT_NAME: two networks (develop_default and prod_default), two volumes, two database containers, and two application containers. The two environments will be logically separate from each other, even though they’re running on the same host system.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search