Could not translate host name “db” to address using Postgres, Docker Compose and Psycopg2

后端 未结 6 637
不思量自难忘°
不思量自难忘° 2021-02-01 16:39

In one folder I have 3 files: base.py, Dockerfile and docker-compose.yml.

base.py:

import psycopg2

conn = psycopg2.connect(\"dbname=\'b         


        
6条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 17:30

    Add network, link and depends_on configuration in docker compose file.

    something like this:

      services:
          db:
              build: .
              container_name: db
              networks:
                  - djangonetwork
          web:
              build: .
              depends_on:
                 - db
              links:
                 - db:db
              networks:
                 - djangonetwork
    
      networks:
          djangonetwork:
              driver: bridge
    

    the above configuration helped me to resolve the host name to connect to the db.

提交回复
热议问题