How to pass docker container ip address dynamically in a Strapi database.js config file

425 Views Asked by At

I have created a container in which I have started Postgres database now i want to run a strapi start command for that we have to add IP address of the database in database.js file how can i add dynamically Postgres container ip address for now i have created a network and assigned a particular ip for each container but i wanted to assign a ip address automatically and add that ip in config file.

docker-compose.yml

services:

  web:
    build: ./
    working_dir: /src
    networks:
      vpcbr:
        ipv4_address: 10.5.0.5
    entrypoint: "/bin/bash"
    command: "-c \"if [ ! -d \"node_modules\" ]; then npm i; fi; npm run start\""
    ports:
      - "8000:4242"
    links:
      - db
    environment:
      SEQ_DB: dbname
      SEQ_USER: postgres
      SEQ_PW: root
      PORT: 5432
      DATABASE_URL: postgres://postgres:root@db:5432/dbname
    volumes:
      - ./:/src

  db:
    image: postgres:10-alpine
    networks:
      vpcbr:
        ipv4_address: 10.5.0.4
    working_dir: /src
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
    - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: root
      
networks:
  vpcbr:
    driver: bridge
    ipam:
      config:
        - subnet: 10.5.0.0/16

Database.js

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connections: {
    default: {
      connector: "bookshelf",
      settings: {
        client: "postgres",
        database: "dbname",
        host: "10.5.0.4",
        port: 5432,
        username: "dbuser",
        password: "dbpass",
      },
      options: {
        pool: {
          min: 2,
          max: 6,
          createTimeoutMillis: 3000,
          acquireTimeoutMillis: 30000,
          idleTimeoutMillis: 30000,
          reapIntervalMillis: 1000,
          createRetryIntervalMillis: 100,
          propagateCreateError: false,
        },
      },
    },
  },
});

0

There are 0 best solutions below