Browse Source

Static postgres host in docker

Nicolas Meienberger 3 years ago
parent
commit
efca46ccc8
4 changed files with 45 additions and 13 deletions
  1. 7 7
      docker-compose.dev.yml
  2. 33 5
      docker-compose.yml
  3. 2 0
      scripts/start.sh
  4. 3 1
      templates/env-sample

+ 7 - 7
docker-compose.dev.yml

@@ -12,10 +12,10 @@ services:
       - 5432:5432
     environment:
       POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
-      POSTGRES_USERNAME: ${POSTGRES_USERNAME}
-      POSTGRES_DBNAME: ${POSTGRES_DBNAME}
+      POSTGRES_USERNAME: tipi
+      POSTGRES_DBNAME: tipi
     healthcheck:
-      test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DBNAME} -U $${POSTGRES_USERNAME}" ]
+      test: [ "CMD-SHELL", "pg_isready -d tipi -U tipi" ]
       interval: 5s
       timeout: 10s
       retries: 120
@@ -45,10 +45,10 @@ services:
       JWT_SECRET: ${JWT_SECRET}
       ROOT_FOLDER_HOST: ${ROOT_FOLDER_HOST}
       NGINX_PORT: ${NGINX_PORT}
-      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
-      POSTGRES_USER: ${POSTGRES_USERNAME}
-      POSTGRES_DB: ${POSTGRES_DBNAME}
-      POSTGRES_HOST: ${POSTGRES_HOST}
+      POSTGRES_PASSWORD: tipi
+      POSTGRES_USERNAME: tipi
+      POSTGRES_DBNAME: ${POSTGRES_DBNAME}
+      POSTGRES_HOST: tipi-db
     networks:
       - tipi_main_network
 

+ 33 - 5
docker-compose.yml

@@ -15,6 +15,27 @@ services:
     networks:
       - tipi_main_network
 
+  tipi-db:
+    container_name: tipi-db
+    image: postgres:latest
+    restart: on-failure
+    stop_grace_period: 1m
+    volumes:
+      - ./data/postgres:/var/lib/postgresql/data
+    ports:
+      - 5432:5432
+    environment:
+      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
+      POSTGRES_USERNAME: tipi
+      POSTGRES_DBNAME: tipi
+    healthcheck:
+      test: [ "CMD-SHELL", "pg_isready -d tipi -U tipi" ]
+      interval: 5s
+      timeout: 10s
+      retries: 120
+    networks:
+      - tipi_main_network
+
   api:
     image: meienberger/runtipi:${TIPI_VERSION}
     command: bash -c "cd /api && npm run start"
@@ -22,16 +43,23 @@ services:
     container_name: api
     ports:
       - 3001:3001
+    depends_on:
+      tipi-db:
+        condition: service_healthy
     volumes:
       ## Docker sock
       - /var/run/docker.sock:/var/run/docker.sock:ro
       - ${PWD}:/tipi
     environment:
-      - INTERNAL_IP=${INTERNAL_IP}
-      - TIPI_VERSION=${TIPI_VERSION}
-      - JWT_SECRET=${JWT_SECRET}
-      - ROOT_FOLDER_HOST=${ROOT_FOLDER_HOST}
-      - NGINX_PORT=${NGINX_PORT}
+      INTERNAL_IP: ${INTERNAL_IP}
+      TIPI_VERSION: ${TIPI_VERSION}
+      JWT_SECRET: ${JWT_SECRET}
+      ROOT_FOLDER_HOST: ${ROOT_FOLDER_HOST}
+      NGINX_PORT: ${NGINX_PORT}
+      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
+      POSTGRES_USERNAME: tipi
+      POSTGRES_DBNAME: tipi
+      POSTGRES_HOST: tipi-db
     networks:
       - tipi_main_network
 

+ 2 - 0
scripts/start.sh

@@ -142,6 +142,7 @@ ENV_FILE=$(mktemp)
 [[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE"
 
 JWT_SECRET=$(derive_entropy "jwt")
+POSTGRES_PASSWORD=$(derive_entropy "postgres")
 
 for template in ${ENV_FILE}; do
   sed -i "s/<dns_ip>/${DNS_IP}/g" "${template}"
@@ -153,6 +154,7 @@ for template in ${ENV_FILE}; do
   sed -i "s/<architecture>/${ARCHITECTURE}/g" "${template}"
   sed -i "s/<nginx_port>/${NGINX_PORT}/g" "${template}"
   sed -i "s/<proxy_port>/${PROXY_PORT}/g" "${template}"
+  sed -i "s/<postgres_password>/${POSTGRES_PASSWORD}/g" "${template}"
 done
 
 mv -f "$ENV_FILE" "$ROOT_FOLDER/.env"

+ 3 - 1
templates/env-sample

@@ -9,4 +9,6 @@ TIPI_VERSION=<tipi_version>
 JWT_SECRET=<jwt_secret>
 ROOT_FOLDER_HOST=<root_folder>
 NGINX_PORT=<nginx_port>
-PROXY_PORT=<proxy_port>
+PROXY_PORT=<proxy_port>
+
+POSTGRES_PASSWORD=<postgres_password>