当前位置: 首页 > 知识库问答 >
问题:

Elixir+docker与postgres的连接被拒绝

商兴朝
2023-03-14

postgres-db 2021-02-23 22:46:3.410 UTC[1]日志:数据库系统已准备好接受连接

.环境:

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"
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[error]GenServer#PID<0.368.0>终止app_1**(dbconnection.connectionerror)tcp连接(postgres-DB:5433):连接拒绝-:econndelection app_1
(db_connection2.3.1)lib/db_connection/connection.ex:100:dbconnection.connection/2 app_1(connection 1.1.0)lib/connection.ex:622:connection.enter_connect/5 app_1(stdlib_1**(混合)无法删除Spiritpay.repo的数据库:杀死app_1[错误]postgrex.协议(#pid<0.330.0>)连接失败:**(dbconnection.connectionerror)tcp连接(postgres-db:5433):连接被拒绝-:econndelection

共有1个答案

濮阳耀
2023-03-14

第一点:

请尝试在.env中用postgres-db替换localhost,因为数据库的主机就是这样。docker compose将内部主机映射到服务名称

文件摘录:

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
    null

要点3

您使用的端口号是错误的postgres在5432上操作

第四点:

    ports:
      - 0.0.0.0:5432:5432
 类似资料:
  • 我是docker的新手,我正在尝试通过本地主机连接我的pgAdmin容器到postgres容器。 无法连接到服务器:连接被拒绝服务器是否在主机“localhost”(127.0.0.1)上运行并接受端口5432上的TCP/IP连接?无法连接到服务器:地址不可用是否在主机“localhost”(::1)上运行并接受端口5432上的TCP/IP连接的服务器? 我成功地通过容器上的给出的IPAddres

  • 问题内容: 我正在尝试通过代码访问Redis服务器,但未连接。但是,如果我猛击redis容器,我可以访问redis-cli。 docker-compose.yml看起来像这样 Dockerfile_redis 当我尝试使用此代码连接到Redis服务器时 它发出此警告 有谁知道如何将Redis容器连接到PHP容器? 问题答案: 你的问题 Docker Compose 为不同的服务创建了单独的dock

  • 我对React和Node还不熟悉,尽管我的代码有几天没有出现错误,但今天我的终端出现了错误,我无法连接到我的mongo数据库。我使用节点服务器运行服务器。js命令,但我得到了错误。有人能帮我找到解决办法吗?谢谢 错误 服务器正在端口5000上运行(节点:3339)DeprecationWarning:当前的服务器发现和监控引擎已弃用,并将在未来的版本中删除。要使用新的服务器发现和监控引擎,请将选项

  • 我使用的是Postgres 9.1.5。JDBC在第一个实例中运行良好,但当我再次尝试时,它开始拒绝连接。例如,我使用JDBC登录到我的web应用程序,效果很好。但是,我尝试使用连接获取其他数据,它引发了一个异常:连接被拒绝。我确信我的代码没有问题,因为上次运行良好的登录JDBC也引发了一个异常:连接被拒绝。 重新启动postgres windows服务后,它再次工作。然后,登录JDBC,获取一些

  • 试图在localhost中建立从app容器到mysql容器的连接,出现连接拒绝异常 我们正在采取一种docker的方法来调用rest api服务来采用微服务的方法。我们正在建立应用程序容器和mysql容器之间的连接,同时我们编写了一个docker-compose文件,创建了mysql容器和应用程序容器,为这两个容器公开了端口。下面是运行docker-compose文件docker-compose

  • 问题内容: 我创建了一个小工具,过去它像一个魅力一样可以部署小型WordPress实例。看起来像这样: 但是当我现在启动它时(也许是因为docker更新到Docker 1.9.1版本,build ),它失败了 当我的猫的有项针对MySQL: 而且我能够ping通MariaDB服务器。 当我安装WordPress并多次重启后,MariaDB容器打印出来: 是哪个指示它正在运行,不是吗? 我如何获得W