0

My docker-compose file is as below

version: "3"

volumes:
  postgres:
    driver: local

services:
  postgres:
    image: postgres:11.1
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=mysecretpassword
      - PGDATA=pgdata
    volumes:
      - postgres:/var/lib/postgresql/data/pgdata

  backend:
    build:
      context: backend/
    ports:
      - "8000:8000"
    environment:
      - PRODUCTION=false
      - config_file=/srv/backend/config/config.json
    volumes:
      - ./backend:/srv/backend
    depends_on:
      - postgres

  frontend:
    build:
      context: frontend/
    ports:
      - "8080:8080"
    volumes:
      - ./frontend:/srv/frontend

  scraper:
    build:
      context: scraper/
    volumes:
      - ./scraper:/srv/scraper

When I run docker I am getting below error,

backend_1   |     self.ensure_connection()
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
backend_1   |     return func(*args, **kwargs)
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
backend_1   |     self.connect()
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
backend_1   |     raise dj_exc_value.with_traceback(traceback) from exc_value
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
backend_1   |     self.connect()
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
backend_1   |     return func(*args, **kwargs)
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
backend_1   |     self.connection = self.get_new_connection(conn_params)
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
backend_1   |     return func(*args, **kwargs)
backend_1   |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
backend_1   |     connection = Database.connect(**conn_params)
backend_1   |   File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
backend_1   |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
backend_1   | django.db.utils.OperationalError: could not connect to server: No route to host
backend_1   |   Is the server running on host "postgres" (172.18.0.2) and accepting
backend_1   |   TCP/IP connections on port 5432?
backend_1   | 
backend_1   | ==> Django setup, executing: collectstatic
backend_1   | 
backend_1   | 0 static files copied to '/srv/backend/django/static', 184 unmodified.
backend_1   | ==> Starting ...

config.json file is as follow

{
  "production": false,
  "django": {
    "domain_frontend": "http://localhost:8080",
    "default_from_email": “admin@mysite.com",
    "secret_key": "mysecretkey",
    "allowed_hosts": [
      "localhost"
    ],
    "cors_whitelist": [
      "http://localhost:8080"
    ]
  },
  "postgres": {
    "db": "postgres",
    "user": "postgres",
    "password": "mysecretpassword",
    "host": "postgres",
    "port": "5432"
  }
}

I kill existing processing running on port 5432 and tried adding ports in the docker-compose file however it did not help.

I found there is help available on this topic and I tried most of those. But no luck yet.

Please help.

  • 1
    Does this answer your question? [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) as only depend on will not work if DB is not yet ready – Adiii Jul 17 '20 at 04:21

2 Answers2

0

From logs it was showing :

django.db.utils.OperationalError: could not connect to server: No route to host
Is the server running on host "postgres" (172.18.0.2) and
accepting TCP/IP connections on port 5432?

I guess the ip assigned to postgres and the one which is specified in config.json might be different.

Make sure that /srv/backend/config/config.json has same ip which got assigned to postgres

And also expose the port 5432 if required.

version: "3"

volumes:
  postgres:
    driver: local

services:
  postgres:
    image: postgres:11.1
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=mysecretpassword
      - PGDATA=pgdata
    ports:
      - "5432:5432"
    volumes:
      - postgres:/var/lib/postgresql/data/pgdata

  backend:
    build:
      context: backend/
    ports:
      - "8000:8000"
    environment:
      - PRODUCTION=false
      - config_file=/srv/backend/config/config.json
    volumes:
      - ./backend:/srv/backend
    depends_on:
      - postgres

  frontend:
    build:
      context: frontend/
    ports:
      - "8080:8080"
    volumes:
      - ./frontend:/srv/frontend

  scraper:
    build:
      context: scraper/
    volumes:
      - ./scraper:/srv/scraper

Try above docker-compose.yml and update postgres host as localhost and port 5432 in config.json

G1Rao
  • 424
  • 5
  • 11
0

I came across the same issue while running postgres db on docker. there were 2 postgres dbs running. if you do docker inspect container_name, you'll get something like IP address or Gateway. Just try connecting to either and it should work. If your "Ip address" or "Gateway" is just empty string, then 172.17.0.2 which i think is default container url, should work.


KKM
  • 626
  • 5
  • 12