Could Not Translate Host Name "Db" to Address Using Postgres, Docker Compose and Psycopg2

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

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.

could not translate host name "db" to address: Temporary failure in name resolution

Just add POSTGRES_PASSWORD to the db instance too.

version: "3.9"

services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db

Can not connect PostgreSQL database from docker to python

You need to expose the BD port in the docker compose like this :

db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: admin_123
POSTGRES_USER: admin
ports:
- "5432:5432"

And then connect with localhost:5432

could not translate host name to "db" to address: Unknown host

Your code can run in two different environments, and hard-coding the connection information might not be correct.

You mention in a comment that you're running something like:

docker-compose up -d db
python manage.py makemigrations

In this environment python is running outside of Docker. If you add ports: [5432:5432] to the database configuration in the docker-compose.yml file, the database will be accessible via (probably) localhost. On the other hand, when you run docker-compose up, the application runs inside Docker and the database will be reachable at db.

You can use an environment variable to configure this. I find it useful to give these variables default values that would be useful for a developer, and set them to different values in my deployment setup (the docker-compose.yml).

DATABASES = {
'default': {
...
'HOST': os.getenv('DB_HOST', 'localhost'),
...
}
}
version: "3.9"
services:
db:
ports:
- '5432:5432' # makes this accessible from your development environment
...
web:
environment:
- DB_HOST=db
...


Related Topics



Leave a reply



Submit