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

后端 未结 6 620
不思量自难忘°
不思量自难忘° 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:19

    Another possible scenario,

    Check if ports have been used or not by other docker container. Use command:

    $ docker container ls --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" -a
    

    Then change your ports/expose in docker-compose file

    0 讨论(0)
  • 2021-02-01 17:20

    if you add this to your db container in your docker-compose.yml it should resolve the issue

    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
    
    0 讨论(0)
  • 2021-02-01 17:27

    The problem is you should not be running python base.py as part of the RUN directive.

    The RUN directive is executed only when you are building the image. The postgres container is not running at this point, nor has the network been created. Instead you want to use the CMD directive.

    Change the Dockerfile to this:

    FROM ubuntu:16.04
    
    RUN apt-get update
    RUN apt-get -y install python-pip
    RUN apt-get update
    RUN pip install --upgrade pip
    RUN pip install psycopg2-binary
    
    COPY base.py base.py
    
    CMD ["python", "base.py"]
    

    The above should result in the hostname db to be resolved. However if your python code doesn't have any reconnection logic for connecting to the database the container will likely still error out. This because the postgres container will be running but the database won't be ready to accept connections.

    This can be temporarily fixed by adding restart: always to your docker-compose.yml.

    version: '3'
    services:
      db:
        image: 'postgres:latest'
        expose:
          - "5432"
        environment:
          POSTGRES_PASSWORD: pw1234
          POSTGRES_DB: base123
      aprrka:
        restart: always
        build: .    
        depends_on:
          - db
    

    Hopefully this will get you up and running.

    0 讨论(0)
  • 2021-02-01 17:27

    Changing the host from "db" to "postgres" worked for me.

    Also use postgres defaults for username and password which is

    username: postgres
    password:
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 17:30

    If you're using docker-compose first add this line to your .yml:

    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
    

    After that you start only you db docker:

    docker-compose up db
    

    It should start normally with a message like:

    Recreating db... done
    Attaching to db
    db_1           | ********************************************************************************
    db_1           | WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow
    db_1           |          anyone with access to the Postgres port to access your database without
    db_1           |          a password, even if POSTGRES_PASSWORD is set. See PostgreSQL
    db_1           |          documentation about "trust":
    db_1           |          https://www.postgresql.org/docs/current/auth-trust.html
    db_1           |          In Docker's default configuration, this is effectively any other
    db_1           |          container on the same system.
    db_1           | 
    db_1           |          It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace
    db_1           |          it with "-e POSTGRES_PASSWORD=password" instead to set a password in
    db_1           |          "docker run".
    db_1           | ********************************************************************************
    
    0 讨论(0)
提交回复
热议问题