简体   繁体   English

从另一个正在运行的容器连接到 postgres 容器

[英]Connect to postgres container from another running container

I am struggling to connect to a running container with a postgres database from another container.我正在努力使用来自另一个容器的 postgres 数据库连接到正在运行的容器。 I can connect when running locally, but I suspect that there is some networking issue that I'm overlooking when trying to connect from another container.我可以在本地运行时连接,但我怀疑在尝试从另一个容器连接时我忽略了一些网络问题。 The specific error I am currently seeing is psycopg2.OperationalError: could not translate host name "my_network" to address: Temporary failure in name resolution我目前看到的具体错误是psycopg2.OperationalError: could not translate host name "my_network" to address: Temporary failure in name resolution

Here is my docker-compose:这是我的 docker-compose:

version: '3'                                                                                       
services:                                                                                          
  data_collection:                                                                                 
    build: ./docker/data_collection                                                                
    ports:                                                                                         
      - "8888:8888"                                                                                
      - "6006:6006"                                                                                
      - "8000:8000"                                                                                                           
    networks:                                                                                      
      - my_network                                                                                 
    depends_on:                                                                                    
      - db                                                                                         
  db:                                                                                              
    image: 'postgres:13.2-alpine'                                                                  
    ports: 
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=mypassword
      - POSTGRES_DB=spotify
    volumes:
      - db-data:/var/lib/postgresql/data # persist data even if container shuts down               
    networks:
      - my_network                                                                                 
                                                                                                   volumes:                                                                                           
  db-data: # names volumes can be managed easier using docker-compose                                                                                                                                 
networks:                                                                                          
  my_network:                                                                                      
~              

I then enter a shell in my data_colleciton container and try to connect by reading those params into a variables params , which gets passed in like this:然后我在我的data_colleciton容器中输入 shell 并尝试通过将这些参数读入变量params来连接,该变量像这样传入:

import psycopg2

params = dict(host='my_network', database='spotify', user='postgres', password='mypassword', port='5432')
conn = psycopg2.connect(**params)

However, running this locally and replacing the host above with localhost does work as expected但是,在本地运行它并用 localhost 替换上面的主机确实可以按预期工作

Change the host='my_network' to host='db' .host='my_network'更改为host='db' You are not connecting to the network, you are connecting to a specific host in that network.您没有连接到网络,而是连接到该网络中的特定主机。

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

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