K
K
kurrbanov2021-09-09 17:15:38
Python
kurrbanov, 2021-09-09 17:15:38

Why does the Docker container not see PostgreSQL?

One container should use aiopg to create a table and add data to it. The container with docker-compose and the program that adds the data is described as follows:
PS file is shortened so that it is not too cumbersome. The part that gives errors is shown.

version: "3.9"

services:
  db:
    image: postgres:13
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=drama
      - POSTGRES_USER=drama_user
      - POSTGRES_PASSWORD=pass
    ports:
      - "5432:5432"
  dramatiq:
    build:
      context: ./
      dockerfile: ./dramatiq/Dockerfile
    depends_on:
      - redis
      - db


The dramatiq container's Dockerfile is unremarkable. Just copy all the files in the directory, install the dependencies and run the code.

Here is the connection via aiopg:
connection = await aiopg.connect(database="drama", user="drama_user", password="pass", host="db")
cursor = await connection.cursor()


After I launch the docker containers, I see the following error in the logs:

dramatiq_1  |   File "/usr/local/lib/python3.9/site-packages/aiopg/connection.py", line 788, in _ready
dramatiq_1  |     state = self._conn.poll()
dramatiq_1  | psycopg2.OperationalError: could not connect to server: Connection refused
dramatiq_1  |   Is the server running on host "db" (172.29.0.3) and accepting
dramatiq_1  |   TCP/IP connections on port 5432?


With what such error can be connected?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2021-09-09
@kurrbanov

With what such error can be connected?

With the fact that your script runs faster than the base. docker depends works extremely primitively and correctly: if the container is running, then the dependency is resolved. But in a running container, it is far from a fact that the database has started. Between these two events can take a long time - a few seconds.
First, do a sleep(10) in the script. If it helps then here is the solution

K
kirillinyakin, 2021-09-09
@kirillinyakin

To access through db, you need to combine containers into one network

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question