Elixir + docker compose 与 postgres 的连接被拒绝
Elixir + docker compose Connection with postgres refused
你好,我在使用 elixir 连接到 postgres 时遇到问题,我在 docker-compose 上尝试了以下操作:
我使用了环境变量,但不是,所以我尝试将字符串放入 dev.exs,但即便如此它仍然拒绝连接。
我测试了我的 postgres,它工作正常并且数据库创建正常
日志:
postgres-db | 2021-02-23 22:46:30.410 UTC [1] LOG: database system
is ready to accept connections
.env:
DB_NAME=spiritpay-dev
DB_USER=postgres
DB_HOST=localhost
DB_PASS=12345
docker-撰写:
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"
dev.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 [error] GenServer #PID<0.368.0>
terminating app_1 | ** (DBConnection.ConnectionError) tcp
connect (postgres-db:5433): connection refused - :econnrefused app_1
| (db_connection 2.3.1) lib/db_connection/connection.ex:100:
DBConnection.Connection.connect/2 app_1 | (connection
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 | Last message: nil app_1 | State:
Postgrex.Protocol app_1 | ** (Mix) The database for
Spiritpay.Repo couldn't be dropped: killed app_1 | [error]
Postgrex.Protocol (#PID<0.330.0>) failed to connect: **
(DBConnection.ConnectionError) tcp connect (postgres-db:5433):
connection refused - :econnrefused
第 1 点:
尝试用 .env 中的 postgres-db 替换 localhost,因为你的数据库主机就是那个。您 docker 将内部主机映射到服务名称
摘自docs:
By default Compose sets up a single network for your app. Each
container for a service joins the default network and is both
reachable by other containers on that network, and discoverable by
them at a hostname identical to the container name.
Point2:
将 .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 个地方定义了环境变量
- 在未使用的.env中
- 在应用服务的环境部分
- 在 dev.exs 中硬编码
以上只会造成混乱和负担
在 dev.exs
从环境中加载配置
Point3
你使用的端口号是错误的 postgres 在 5432
上运行
第 4 点:
您可以通过侦听来自任何地方的连接来测试来自主机的连接 0.0.0.0
使用:
ports:
- 0.0.0.0:5432:5432
然后您可以使用 netcat 在本地主机上测试连接:
nc -v localhost 5432
你好,我在使用 elixir 连接到 postgres 时遇到问题,我在 docker-compose 上尝试了以下操作:
我使用了环境变量,但不是,所以我尝试将字符串放入 dev.exs,但即便如此它仍然拒绝连接。
我测试了我的 postgres,它工作正常并且数据库创建正常 日志:
postgres-db | 2021-02-23 22:46:30.410 UTC [1] LOG: database system is ready to accept connections
.env:
DB_NAME=spiritpay-dev
DB_USER=postgres
DB_HOST=localhost
DB_PASS=12345
docker-撰写:
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"
dev.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 [error] GenServer #PID<0.368.0> terminating app_1 | ** (DBConnection.ConnectionError) tcp connect (postgres-db:5433): connection refused - :econnrefused app_1
| (db_connection 2.3.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2 app_1 | (connection 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 | Last message: nil app_1 | State: Postgrex.Protocol app_1 | ** (Mix) The database for Spiritpay.Repo couldn't be dropped: killed app_1 | [error] Postgrex.Protocol (#PID<0.330.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres-db:5433): connection refused - :econnrefused
第 1 点:
尝试用 .env 中的 postgres-db 替换 localhost,因为你的数据库主机就是那个。您 docker 将内部主机映射到服务名称
摘自docs:
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Point2:
将 .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 个地方定义了环境变量
- 在未使用的.env中
- 在应用服务的环境部分
- 在 dev.exs 中硬编码 以上只会造成混乱和负担
在 dev.exs
从环境中加载配置
Point3
你使用的端口号是错误的 postgres 在 5432
第 4 点:
您可以通过侦听来自任何地方的连接来测试来自主机的连接 0.0.0.0
使用:
ports:
- 0.0.0.0:5432:5432
然后您可以使用 netcat 在本地主机上测试连接:
nc -v localhost 5432