简体   繁体   English

docker compose服务之间无法连接到Postgres db

[英]Cannot Connect to Postgres db between docker compose services

I am trying to connect postgresdb service with nodejs web service using docker compose 我正在尝试使用docker compose将postgresdb服务与Node.js Web服务连接

My docker-compose.yml file 我的docker-compose.yml文件

version: "3"                                                                                                                                                                                                                                                                                                                  
services:                                                                                                                                                                                                                                                                                                                     
  web:                                                                                                                                                                                                                                                                                                                        
      build: ./                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
      ports:                                                                                                                                                                                                                                                                                                                  
       - "40000:3000"                                                                                                                                                                                                                                                                                                        
      depends_on:                                                                                                                                                                                                                                                                                                                  
        - postgres
  postgres:                                                                                                                                                                                                                                                                                                                   
      image: kartoza/postgis:9.6-2.4                                                                                                                                                                                                                                                                                          
      restart: always                                                                                                                                                                                                                                                                                                         
      volumes:                                                                                                                                                                                                                                                                                                                
        - postgresdata:/data/db                                                                                                                                                                                                                                                                                               
      environment:                                                                                                                                                                                                                                                                                                            
        - POSTGRES_PASS=password                                                                                                                                                                                                                                                                                              
        - POSTGRES_DBNAME=sticki                                                                                                                                                                                                                                                                                              
        - POSTGRES_USER=renga                                                                                                                                                                                                                                                                                                 
        - ALLOW_IP_RANGE=0.0.0.0/0                                                                                                                                                                                                                                                                                            
      ports:                                                                                                                                                                                                                                                                                                                  
        - "1000:5432"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
volumes:                                                                                                                                                                                                                                                                                                                      
  postgresdata:                                                                                                                                                                                                                                                                                                               

So when i do docker-compose up in my root directory both services are running and i can access web service using localhost:40000 and postgres service using postico on localhost:1000 因此,当我在根目录中执行docker-compose up ,两个服务都在运行,并且我可以使用localhost:40000访问Web服务,并使用postico在localhost:1000上访问postgres服务

But in Node Web service i have written code to access postgres using Sequelize as 但是在Node Web服务中,我已经编写了使用Sequelize作为访问postgres的代码

const sequelize = new Sequelize('sticki', 'renga', 'password', {
  host: 'postgres',
  dialect: 'postgres',
});

But I get the following error 但我收到以下错误

SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:1000

Why does postgres Connection is made to 172.18.0.2 instead of localhost(0.0.0.0)? 为什么将postgres Connection建立为172.18.0.2而不是localhost(0.0.0.0)? What i am doing wrong? 我做错了什么?

For your web container postgres is a DNS name defined in compose as a service. 对于您的web容器, postgres是在compose中定义为服务的DNS名称。 It fetches the postgres DNS IP address via docker internal DNS & network, that's why it's resolving to 172.18.0.2 . 它通过docker内部DNS和网络获取postgres DNS IP地址,这就是为什么它解析为172.18.0.2的原因。 If you go to web container & ping postgres , you will get the same IP. 如果您访问web容器并ping postgres ,则将获得相同的IP。

As a fix, configure your node service to connect to host postgres on port 5432 since it's the container port. 作为修复,将节点服务配置为在端口5432上连接到主机postgres ,因为它是容器端口。 Port 1000 is the host machine port, if you want to use port 1000, configure node service to connect to your MACHINE_IP:1000 . 端口1000是主机端口,如果要使用端口1000,请配置节点服务以连接到MACHINE_IP:1000

PS - Localhost within a container means the container itself & nothing else. PS-容器中的Localhost表示容器本身,仅此而已。

Service name is taken from container_name - which is fixed. 服务名称取自container_name-是固定的。 In your case you do not have that and name is created from folder where docker-compose.yml is + _ + service name + _1. 在您的情况下,您没有该名称,并且名称是从docker-compose.yml为+ _ +服务名称+ _1的文件夹中创建的。 With this DNS name you can reach your service on the default network that docker-compose will create, from one service to reach the other. 使用此DNS名称,您可以在docker-compose将创建的默认网络上从一个服务访问另一个服务。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM