3

I am running the docker project on docker toolbox windows 7 sp1 and the project does not give any error but still due to postgres not working the whole project in not functional. The code of Docker compose file is :

version: '3'
services:
  postgres:
    image: 'postgres:latest'
    restart: always
    ports:
      - "5432:5432" 
    environment:
      POSTGRES_DB: "db"
      POSTGRES_PASSWORD: postgres_password
      POSTGRES_HOST_AUTH_METHOD: "trust"
      DATABASE_URL: postgresql://postgres:p3wrd@postgres:5432/postgres
    deploy:  
      restart_policy:  
        condition: on-failure  
        window: 15m  
  redis:
    image: 'redis:latest'
  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '3050:80'  
  api:
    depends_on:
      - "postgres"
    build:
      dockerfile: Dockerfile.dev
      context: ./server
    volumes:
      - ./server/copy:/usr/src/app/data
    environment:
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - PGUSER=postgres
      - PGHOST=postgres
      - PGDATABASE=postgres
      - PGPASSWORD=postgres_password
      - PGPORT=5432
  client:
    depends_on:
      - "postgres"
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    volumes:
      - ./client/copy:/usr/src/app/data
      - /usr/src/app/node_modules
  worker:
    build:
      dockerfile: Dockerfile.dev
      context: ./worker
    volumes:
      - ./worker/copy:/usr/src/app/data
      - /usr/src/app/node_modules 
    depends_on:
      - "postgres"

But when i run the project i get this :

redis_1     | 1:C 29 May 2020 05:07:37.909 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1     | 1:C 29 May 2020 05:07:37.910 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1     | 1:C 29 May 2020 05:07:37.911 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1     | 1:M 29 May 2020 05:07:37.922 * Running mode=standalone, port=6379.
redis_1     | 1:M 29 May 2020 05:07:37.928 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1     | 1:M 29 May 2020 05:07:37.929 # Server initialized
redis_1     | 1:M 29 May 2020 05:07:37.929 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1     | 1:M 29 May 2020 05:07:37.933 * Loading RDB produced by version 6.0.3
redis_1     | 1:M 29 May 2020 05:07:37.934 * RDB age 8 seconds
redis_1     | 1:M 29 May 2020 05:07:37.934 * RDB memory usage when created 0.81 Mb
redis_1     | 1:M 29 May 2020 05:07:37.934 * DB loaded from disk: 0.001 seconds
postgres_1  |
postgres_1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1  |
postgres_1  | 2020-05-29 05:07:38.927 UTC [1] LOG:  starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1  | 2020-05-29 05:07:38.928 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2020-05-29 05:07:38.929 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2020-05-29 05:07:38.933 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2020-05-29 05:07:38.993 UTC [24] LOG:  database system was shut down at 2020-05-29 05:07:29 UTC
api_1       |
api_1       | > @ dev /usr/src/app
api_1       | > nodemon
api_1       |
api_1       | [nodemon] 1.18.3
api_1       | [nodemon] to restart at any time, enter `rs`
api_1       | [nodemon] watching: *.*

With or without data volumes the same error comes and due to which the project is not running. Please Help

2 Answers 2

2

I would assume that once the API starts, it attempts to connect to the Postgres. If yes, this is a typical error that many Docker developers experience where the Postgres DB is not yet ready to accept connections and apps are trying to connect to it.

You can solve the problem by trying either of the following approaches:

  1. Make your API layer wait for a certain amount of time (Enough for the Postgres DB to boot up)
Thread.Sleep(60); # should be enough so that Postgres DB can start
  1. Implement a retry mechanism that will wait for let's say 10 seconds everytime the connection fails to establish.

If this doesn't work, I would recommend for you to check whether there is a Postgres DB installed outside of the container that owns the port you were trying to access.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi am new to docker so can you please tell me how to make api layer wait where should i put this thread.sleep to do so . The api does try to connect with postgress
@DeepeshAcharya Where your node application connects to the database, put a fail-retry loop with a delay.
I have edited the docker-compose file in the question but it still does not work
1

Along with Allan Chua's answer, Please mention a startup dependency on Postgres service in your docker-compose file.

    depends_on:
      - postgres

Add this to your api service.

1 Comment

This is also an important point. It's worth noting that this will not wait for the database to be completely initialized, but simply specifies a launch order. A complete solution will employ both depends on and a retry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.