2

I am using python scripts to create LDAP connection, I have created an empty postgres database in which in want to insert some value, I am not able to connect to the postgres db inside from docker container.

Traceback (most recent call last):
  File "/usr/src/app/ldap_sync_users.py", line 41, in <module>
    connection = psycopg2.connect(user = psqlUser,
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "10.120.16.12 " to address: Name or service not known

Dockerfile looks like this
FROM python:3

WORKDIR /usr/src/app

RUN apt-get update && apt-get install -y \
    libsasl2-dev \
    python-dev \
    libldap2-dev \
    libssl-dev \
    python-pip

RUN pip install python-ldap
RUN pip install psycopg2

COPY ldap_file.py ./

CMD python ldap_sync_users.py 

and i use another docker script file 

#!/bin/bash

docker run -i \
    --name ldap_sync \
    --env-file=environment \
    --add-host=database:10.120.16.12 \
       ldapsync
Adrian Klaver
  • 15,886
  • 2
  • 17
  • 28
SaraK
  • 21
  • 2
  • 1
    Where is the database running; inside another container, on the same host but not in a container, somewhere else? You shouldn't generally need that `--add-host` option. – David Maze May 18 '21 at 18:35
  • database is running on host but not in the container, thats y i have added add-host which points to where db is running. – SaraK May 18 '21 at 19:56
  • 1
    On MacOS or Windows hosts you can use a special host name `host.docker.internal` for that; see [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze May 18 '21 at 20:40

0 Answers0