对于Postgres而言,在使用码头服务器的nodeJS上进行ECONNREFUSED [英] ECONNREFUSED for Postgres on nodeJS with dockers
问题描述
我正在使用postgresql构建在NodeJS上运行的应用程序。
我使用SequelizeJS作为ORM。
为了避免使用真正的postgres守护进程并在我自己的设备上使用nodej,我使用docker-compose的容器。
当我运行 docker-compose up
它启动pg数据库
数据库系统准备就绪接受连接
和nodejs服务器。
但服务器无法连接到数据库。
错误:connect ECONNREFUSED 127.0.01:5432
如果我尝试运行服务器而不使用容器(在我的机器上有真正的nodejs和postgresd)它可以工作。 / p>
但是我希望它与容器正常工作。我不明白我在做错什么。
这里是 docker-compose.yml
/ p>
web:
image:node
命令:npm start
ports:
- 8000:4242
链接:
- db
working_dir:/ src
环境:
SEQ_DB:mydatabase
SEQ_USER:用户名
SEQ_PW:pgpassword
PORT:4242
DATABASE_URL:postgres://用户名:pgpassword@127.0.0.1:5432 / mydatabase
卷:
- ./:/src
db:
image:postgres
ports:
- 5432:5432
环境:
POSTGRES_USER:用户名
POSTGRES_PASSWORD:pgpassword
有人可以帮助我吗?
docker :))
您的 DATABASE_URL
指 127.0.0.1
,这是回送适配器(更多这里)。这意味着连接到我自己。
当同一主机上运行两个应用程序(不使用Docker)时,它们都可以在同一个适配器上进行寻址(也称为 localhost
)。
当在容器中运行这两个应用程序时,它们不像以前那样在localhost上。相反,您需要将 web 容器指向
db c> / code>适配器 - 其中
docker-compose
为您设置。
更改:
DATABASE_URL:postgres:// username:pgpassword@127.0.0.1:5432 / mydatabase
pre>
到
DATABASE_URL:postgres:// username:pgpassword @db:5432 / mydatabase
这个工作感谢Docker链接:
web
容器有一个文件(/ etc / hosts
),一个db
条目指向IP已经开启了db
容器。这是系统(在这种情况下,容器)在尝试解析主机名时将会看到的第一个位置。I'm building an app running on NodeJS using postgresql. I'm using SequelizeJS as ORM. To avoid using real postgres daemon and having nodejs on my own device, i'm using containers with docker-compose.
when I run
docker-compose up
it starts the pg databasedatabase system is ready to accept connections
and the nodejs server. but the server can't connect to database.
Error: connect ECONNREFUSED 127.0.01:5432
If I try to run the server without using containers (with real nodejs and postgresd on my machine) it works.
But I want it to work correctly with containers. I don't understand what i'm doing wrong.
here is the
docker-compose.yml
fileweb: image: node command: npm start ports: - "8000:4242" links: - db working_dir: /src environment: SEQ_DB: mydatabase SEQ_USER: username SEQ_PW: pgpassword PORT: 4242 DATABASE_URL: postgres://username:pgpassword@127.0.0.1:5432/mydatabase volumes: - ./:/src db: image: postgres ports: - "5432:5432" environment: POSTGRES_USER: username POSTGRES_PASSWORD: pgpassword
Could someone help me please?
(someone who likes docker :) )
解决方案Your
DATABASE_URL
refers to127.0.0.1
, which is the loopback adapter (more here). This means "connect to myself".When running both applications (without using Docker) on the same host, they are both addressable on the same adapter (also known as
localhost
).When running both applications in containers they are not both on localhost as before. Instead you need to point the
web
container to thedb
container's IP address on thedocker0
adapter - whichdocker-compose
sets for you.Change:
DATABASE_URL: postgres://username:pgpassword@127.0.0.1:5432/mydatabase
to
DATABASE_URL: postgres://username:pgpassword@db:5432/mydatabase
This works thanks to Docker links: the
web
container has a file (/etc/hosts
) with adb
entry pointing to the IP that thedb
container is on. This is the first place a system (in this case, the container) will look when trying to resolve hostnames.这篇关于对于Postgres而言,在使用码头服务器的nodeJS上进行ECONNREFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!