簡體   English   中英

連接被拒絕:服務器是否在主機“db”(172.21.0.2)上運行並接受端口 5432 上的 web_1 TCP/IP 連接?

[英]Connection refused: Is the server running on host “db” (172.21.0.2) and accepting web_1 TCP/IP connections on port 5432?

我知道這是一個典型的問題,但是我需要社區幫助來解決它。 當我運行 docker-compose 時,連接被拒絕:服務器是否在主機“db”(172.21.0.2)上運行並在端口 5432 上接受 web_1 TCP/IP 連接? docker-compose 的 Web 部分失敗,但 db 運行。 我可以使用 localhost:5432 連接到數據庫。 這不是等待問題,因為 tiangolo/uvicorn-gunicorn:python3.8-alpine3.10 實現了等待機制 - dockerize。 有誰知道問題出在哪里? 或者也許有人能指出我正確的方向? 所以我的 Dockerfile 是

FROM tiangolo/uvicorn-gunicorn:python3.8-alpine3.10

# copy requirements file
COPY ./requirements.txt /usr/src/app/requirements.txt

# install dependencies
RUN set -eux \
    && apk add --no-cache --virtual .build-deps build-base \
        libressl-dev libffi-dev gcc musl-dev python3-dev \
        postgresql-dev openssl \
    && pip install --upgrade pip setuptools wheel \
    && pip install -r /usr/src/app/requirements.txt \
    && rm -rf /root/.cache/pip

# copy project
COPY . /app

我的 docker-compose 是:

version: '3.7'

services:
    db:
        image: postgres:13-alpine
        volumes:
            - postgres_data:/var/lib/postgresql/data/
        ports:
            - 5432:5432
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
            - POSTGRES_DB=api

    web:
        build: ./src  
        ports:
            - 80:80
        depends_on:
            - db
        environment:
            - DATABASE_URL=postgresql://postgres:postgres@db/api

volumes:
    postgres_data:

我使用 dockerize 添加了一個等待,但是 web 部分失敗了:

Attaching to gunicorn_db_1, gunicorn_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   |
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
web_1  | 2020/08/30 15:33:51 Waiting for: tcp://db:5432
web_1  | 2020/08/30 15:33:51 Problem with dial: dial tcp 172.21.0.2:5432: connect: connection refused. Sleeping 30s
db_1   | selecting default time zone ... Etc/UTC
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | ok
db_1   |
db_1   |
db_1   | Success. You can now start the database server using:
db_1   |
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   |
db_1   | waiting for server to start....2020-08-30 15:33:53.037 UTC [45] LOG:  starting PostgreSQL 13beta3 (Debian 13~beta3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-08-30 15:33:53.044 UTC [45] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-08-30 15:33:53.059 UTC [46] LOG:  database system was shut down at 2020-08-30 15:33:52 UTC
db_1   | 2020-08-30 15:33:53.066 UTC [45] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | CREATE DATABASE
db_1   |
db_1   |
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   |
db_1   | 2020-08-30 15:33:53.399 UTC [45] LOG:  received fast shutdown request
db_1   | waiting for server to shut down....2020-08-30 15:33:53.404 UTC [45] LOG:  aborting any active transactions
db_1   | 2020-08-30 15:33:53.407 UTC [45] LOG:  background worker "logical replication launcher" (PID 52) exited with exit code 1
db_1   | 2020-08-30 15:33:53.409 UTC [47] LOG:  shutting down
db_1   | 2020-08-30 15:33:53.438 UTC [45] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   |
db_1   | PostgreSQL init process complete; ready for start up.
db_1   |
db_1   | 2020-08-30 15:33:53.530 UTC [1] LOG:  starting PostgreSQL 13beta3 (Debian 13~beta3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-08-30 15:33:53.531 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-08-30 15:33:53.531 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-08-30 15:33:53.539 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-08-30 15:33:53.549 UTC [63] LOG:  database system was shut down at 2020-08-30 15:33:53 UTC
db_1   | 2020-08-30 15:33:53.559 UTC [1] LOG:  database system is ready to accept connections
web_1  | 2020/08/30 15:34:21 Connected to tcp://db:5432
gunicorn_web_1 exited with code 0

乍一看,你的兩個文件對我來說似乎都不錯,實際上添加一個依賴應該可以解決這個問題,但是,根據文檔,這種方式不起作用。 它表達了容器之間的依賴關系,但這並不意味着容器將等待其他容器准備就緒。

對於舊版本的Compose,我們可以添加這樣的健康檢查

healthcheck:
  test: ["-U postgres"]
  interval: 3s
  timeout: 30s
  retries: 1

但是由於它不再受支持,因此最好選擇向該服務添加手動等待/睡眠。

有一個廣泛使用的 bash 腳本(等待它),您可以使用它來測試和等待 TCP 主機和端口的可用性。 它也是 Compose Documentation 中推薦的解決方案。 您可以將等待它復制到您的文件中並立即開始使用它。

web:
    command: /wait-for-it.sh db:5432

你也可能想看看這個: Wait for it usage with docker

我知道這是一個典型的問題,但是我需要社區的幫助來解決。 當我運行docker-compose時,連接被拒絕:服務器是否在主機“ db”(172.21.0.2)上運行並在端口5432上接受web_1 TCP / IP連接? docker-compose的Web部分失敗,但是db運行。 我可以使用localhost:5432連接到數據庫。 這不是一個等待問題,因為tiangolo / uvicorn-gunicorn:python3.8-alpine3.10實現了一個等待機制-dockerize。 有人知道問題出在哪里嗎? 也許有人能為我指出正確的方向? 所以我的Dockerfile是

FROM tiangolo/uvicorn-gunicorn:python3.8-alpine3.10

# copy requirements file
COPY ./requirements.txt /usr/src/app/requirements.txt

# install dependencies
RUN set -eux \
    && apk add --no-cache --virtual .build-deps build-base \
        libressl-dev libffi-dev gcc musl-dev python3-dev \
        postgresql-dev openssl \
    && pip install --upgrade pip setuptools wheel \
    && pip install -r /usr/src/app/requirements.txt \
    && rm -rf /root/.cache/pip

# copy project
COPY . /app

我的碼頭工人組成是:

version: '3.7'

services:
    db:
        image: postgres:13-alpine
        volumes:
            - postgres_data:/var/lib/postgresql/data/
        ports:
            - 5432:5432
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
            - POSTGRES_DB=api

    web:
        build: ./src  
        ports:
            - 80:80
        depends_on:
            - db
        environment:
            - DATABASE_URL=postgresql://postgres:postgres@db/api

volumes:
    postgres_data:

我使用dockerize添加了一個等待,但是Web部分失敗了:

Attaching to gunicorn_db_1, gunicorn_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   |
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
web_1  | 2020/08/30 15:33:51 Waiting for: tcp://db:5432
web_1  | 2020/08/30 15:33:51 Problem with dial: dial tcp 172.21.0.2:5432: connect: connection refused. Sleeping 30s
db_1   | selecting default time zone ... Etc/UTC
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | ok
db_1   |
db_1   |
db_1   | Success. You can now start the database server using:
db_1   |
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   |
db_1   | waiting for server to start....2020-08-30 15:33:53.037 UTC [45] LOG:  starting PostgreSQL 13beta3 (Debian 13~beta3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-08-30 15:33:53.044 UTC [45] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-08-30 15:33:53.059 UTC [46] LOG:  database system was shut down at 2020-08-30 15:33:52 UTC
db_1   | 2020-08-30 15:33:53.066 UTC [45] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | CREATE DATABASE
db_1   |
db_1   |
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   |
db_1   | 2020-08-30 15:33:53.399 UTC [45] LOG:  received fast shutdown request
db_1   | waiting for server to shut down....2020-08-30 15:33:53.404 UTC [45] LOG:  aborting any active transactions
db_1   | 2020-08-30 15:33:53.407 UTC [45] LOG:  background worker "logical replication launcher" (PID 52) exited with exit code 1
db_1   | 2020-08-30 15:33:53.409 UTC [47] LOG:  shutting down
db_1   | 2020-08-30 15:33:53.438 UTC [45] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   |
db_1   | PostgreSQL init process complete; ready for start up.
db_1   |
db_1   | 2020-08-30 15:33:53.530 UTC [1] LOG:  starting PostgreSQL 13beta3 (Debian 13~beta3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-08-30 15:33:53.531 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-08-30 15:33:53.531 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-08-30 15:33:53.539 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-08-30 15:33:53.549 UTC [63] LOG:  database system was shut down at 2020-08-30 15:33:53 UTC
db_1   | 2020-08-30 15:33:53.559 UTC [1] LOG:  database system is ready to accept connections
web_1  | 2020/08/30 15:34:21 Connected to tcp://db:5432
gunicorn_web_1 exited with code 0

只需將 Docker-Compose 的版本更改為 version: "3.5" 即可解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 連接到“localhost”的服務器,端口 5432 失敗:連接被拒絕服務器是否在該主機上運行並接受 TCP/IP 連接? 無法連接到服務器:連接被拒絕 服務器是否在主機上運行……並接受端口 5432 上的 TCP/IP 連接? 無法連接到服務器:連接被拒絕 服務器是否在主機“127.0.0.1”上運行並接受端口 5432 上的 TCP/IP 連接? psql:無法連接到服務器:連接被拒絕服務器是否在主機“ localhost”(:: 1)上運行並在端口5432上接受TCP / IP連接? Postgresql:連接被拒絕 服務器是否在主機“127.0.01”(127.0.0.1)上運行並接受端口 5432 上的 TCP/IP 連接? 無法連接到服務器:連接被拒絕。 服務器是否在主機“localhost”(127.0.0.1) 上運行並在端口 5432 上接受 TCP/IP 連接 HEROKU POSTRES錯誤:連接被拒絕服務器是否在主機“..compute-1.amazonaws.com ...”上運行並接受端口5432上的TCP / IP連接? 服務器是否在主機“localhost”(::1) 上運行並在端口 5432 上接受 TCP/IP 連接? Django/Postgresql:服務器是否在主機“xxx”上運行並接受端口 5432 上的 TCP/IP 連接? EC2 和 RDS 上的 Django 應用程序(服務器是否在主機“localhost”(127.0.0.1)上運行並在端口 5432 上接受 TCP/IP 連接?)
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM