簡體   English   中英

Elixir + docker compose 與 postgres 的連接被拒絕

[英]Elixir + docker compose Connection with postgres refused

您好,我在使用 elixir 連接到 postgres 時遇到問題,我在 docker-compose 上嘗試了以下操作:

我使用了環境變量,但事實並非如此,所以我嘗試將字符串放入 dev.exs,但即便如此它仍然拒絕連接。

我測試了我的 postgres,它工作正常,並且數據庫創建了所有正確的日志:

postgres 數據庫 | 2021-02-23 22:46:30.410 UTC [1] LOG:數據庫系統已准備好接受連接

.env:

DB_NAME=spiritpay-dev
DB_USER=postgres
DB_HOST=localhost
DB_PASS=12345

docker-compose:

version: "3.7"
services:
  app:
      restart: on-failure
      environment: 
        DB_USER: postgres
        DB_PASSWORD: 12345
        DB_NAME: spiritpay-dev
        DB_HOST: postgres-db
      build: .
      command: /bin/sh docker-entrypoint.sh
      ports: 
        - "4000:4000"
      depends_on: 
        - postgres-db 
      links:
        - postgres-db
  postgres-db:
      image: "postgres:12"
      restart: always
      container_name: "postgres-db"
      environment: 
        POSTGRES_PASSWORD: ${DB_PASS}
        POSTGRES_USER: ${DB_USER}
        POSTGRES_DB: ${DB_NAME}
      ports: 
        - "5433:5433"

開發者.exs:

use Mix.Config

# Configure your database
config :spiritpay, Spiritpay.Repo,
  username: "postgres",
  password: "12345",
  database: "spiritpay-dev",
  hostname: "postgres-db",
  port: 5433,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

# For development, we disable any cache and enable
# debugging and code reloading.
#
# The watchers configuration can be used to run external
# watchers to your application. For example, we use it
# with webpack to recompile .js and .css sources.
config :spiritpay, SpiritpayWeb.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: []

# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# Mix task:
#
#     mix phx.gen.cert
#
# Note that this task requires Erlang/OTP 20 or later.
# Run `mix help phx.gen.cert` for more information.
#
# The `http:` config above can be replaced with:
#
#     https: [
#       port: 4001,
#       cipher_suite: :strong,
#       keyfile: "priv/cert/selfsigned_key.pem",
#       certfile: "priv/cert/selfsigned.pem"
#     ],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

# Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime

日志:

app_1 | 22:46:31.020 [錯誤] GenServer #PID<0.368.0> 終止 app_1 | ** (DBConnection.ConnectionError) tcp 連接 (postgres-db:5433): 連接被拒絕-:econnrefused app_1
| (db_connection 2.3.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2 app_1 | (連接 1.1.0) lib/connection.ex:622: Connection.enter_connect/5 app_1 | (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3 app_1 | 最后一條消息:無 app_1 | State:Postgrex.Protocol app_1 | **(混合)Spiritpay.Repo 的數據庫無法刪除:killed app_1 | [錯誤] Postgrex.Protocol (#PID<0.330.0>) 連接失敗:** (DBConnection.ConnectionError) tcp 連接 (postgres-db:5433): 連接被拒絕 -:econnrefused

第 1 點:

嘗試用 postgres-db in.env 替換 localhost,因為您的數據庫的主機就是這樣。 您 docker 撰寫將內部主機映射到服務名稱

文檔摘錄:

默認情況下,Compose 會為您的應用程序設置一個網絡。 服務的每個容器都加入默認網絡,並且可以被該網絡上的其他容器訪問,並且可以通過與容器名稱相同的主機名被它們發現。

要點2:

.env添加到您的應用服務:

services:
  app:
      restart: on-failure
      build: .
      command: /bin/sh docker-entrypoint.sh
      ports: 
        - "4000:4000"
      depends_on: 
        - postgres-db 
      links:
        - postgres-db
      env_file:
         - .env

在上面的示例中,您的配置似乎使您在 3 個地方定義的環境變量

  • in.env 未使用
  • 在應用服務的環境部分
  • 在 dev.exs 中硬編碼上述內容只會造成混亂和負擔

dev.exs從環境中加載配置

第3點

您使用的端口號錯誤 postgres 在5432上運行

第 4 點:

您可以使用以下命令偵聽來自0.0.0.0各處的連接來測試來自主機的連接:

    ports:
      - 0.0.0.0:5432:5432

然后您可以使用 netcat 在 localhost 上測試連接: nc -v localhost 5432

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM