【问题标题】:Postgres not accepting connection in DockerPostgres 不接受 Docker 中的连接
【发布时间】:2020-05-29 05:18:16
【问题描述】:

我在 docker toolbox windows 7 sp1 上运行 docker 项目,该项目没有给出任何错误,但仍然是由于 postgres 无法运行整个项目而无法正常工作。 Docker compose 文件的代码是:

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"

但是当我运行项目时,我得到了这个:

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: *.*

无论有没有数据量,都会出现同样的错误,并且项目没有运行。请帮忙

【问题讨论】:

    标签: postgresql docker docker-compose dockerfile


    【解决方案1】:

    我假设一旦 API 启动,它就会尝试连接到 Postgres。如果是,这是许多 Docker 开发人员遇到的典型错误,即 Postgres DB 尚未准备好接受连接并且应用程序正在尝试连接到它。

    您可以尝试以下任一方法来解决问题:

    1. 让您的 API 层等待一段时间(足以让 Postgres DB 启动)
    Thread.Sleep(60); # should be enough so that Postgres DB can start
    
    1. 实现一个重试机制,假设每次连接失败时等待 10 秒。

    如果这不起作用,我建议您检查是否在拥有您尝试访问的端口的容器之外安装了 Postgres DB。

    【讨论】:

    • 嗨,我是 docker 新手,请告诉我如何让 api 层等待我应该把这个 thread.sleep 放在哪里。该 api 确实尝试与 postgress 连接
    • @DeepeshAcharya 在你的节点应用程序连接到数据库的地方,放置一个有延迟的失败重试循环。
    • 我已经编辑了问题中的 docker-compose 文件,但它仍然不起作用
    【解决方案2】:

    连同 Allan Chua 的回答,请在您的 docker-compose 文件中提及对 Postgres 服务的启动依赖。

        depends_on:
          - postgres
    

    将此添加到您的 api 服务中。

    【讨论】:

    • 这也是很重要的一点。值得注意的是,这不会等待数据库完全初始化,而只是指定一个启动顺序。一个完整的解决方案将同时使用依赖和重试。
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 2022-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多