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
Print All Number Divisible by 7 and Contain 7 from 0 to 100
How to Convert Python Code to Application
Pandas Dataframe Calculations With Previous Row
How to Extract a Value (I Want an Int Not Row) from a Dataframe and Do Simple Calculations on It
How to Select the Last Column of Dataframe
Get Only Unique Words from a Sentence in Python
How to Read Numbers from File in Python
Json.Decoder.Jsondecodeerror: Expecting Value: Line 1 Column 1 (Char 0) Python
Pyspark - Sum a Column in Dataframe and Return Results as Int
Python 3 Error - Typeerror: Input Expected At Most 1 Arguments, Got 3
How to Send Email to Multiple Recipients Using Python Smtplib
Python Selenium, Find Out When a Download Has Completed
How to Fill a Column With the Value of Another Column Based on a Condition on Some Other Columns
How to Split a Byte String into Separate Bytes in Python