Reputation: 549
I'm trying to run a service that depends on a postgresql database but I can't figure out how to link the two together / connect.
I'm 'depending on' the database which seems to mean I should be able to use the service name as the database host name (i.e due_database
) but whenever I do I just get the error:
could not translate host name "due_database" to address: Name or service not known
What's wrong here?
version: '3'
services:
due:
restart: always
build:
context: ./
dockerfile: ./docker/bot/Dockerfile
command: ./due
depends_on:
- due_database
due_database:
build:
context: ./
dockerfile: ./docker/database/Dockerfile
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD="docker"
entrypoint: /entry.sh
volumes:
- ./.dueutil_db:/dueutil_data
Update:
version: '2.1'
services:
due:
restart: always
build:
context: ./
dockerfile: ./docker/bot/Dockerfile
entrypoint: /entry.sh
command: ./due
depends_on:
due_database:
condition: service_healthy
environment:
- PGPASSWORD="docker"
due_database:
image: postgres:11
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD="docker"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
volumes:
- ./.dueutil_db:/var/lib/postgresql/data
I fixed the first issue by moving my create database scripts outside the postgres image, since that conflicts with the postgres entrypoint. However I still can't figure out how to connect to the database from my app's container.
I'm currently trying psql -U postgres -h due_database postgres -f <sql script>
but I'm just getting:
psql: could not connect to server: Connection refused
Is the server running on host "due_database" (172.18.0.2) and accepting
TCP/IP connections on port 5432?
Note I did see: psql: could not translate host name "somePostgres" to address: Name or service not known but the question seems outdated & did not get any good answers.
Upvotes: 3
Views: 19337
Reputation: 549
So my first issue was trying to make changes to the PostgreSQL image, copy some scripts in and add a custom entry point. That overrides the postgres startup script & means the database fails to load.
The second issue was using the non-alpine postgres image. Switching to postgres:12.1-apline
'just worked'. I'm not sure what's wrong with the other image though.
Upvotes: 0
Reputation: 421
Try to use network, I'm not sure but maybe it can help you :
version: '3'
# ----------------
# CREATE NEW NETWORK
networks:
app:
driver: bridge
# ----------------
services:
due:
restart: always
build:
context: ./
dockerfile: ./docker/bot/Dockerfile
command: ./due
depends_on:
- due_database
# ---------------
# SPECIFY NETWORK
networks:
- app
# ---------------
due_database:
build:
context: ./
dockerfile: ./docker/database/Dockerfile
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD="docker"
entrypoint: /entry.sh
volumes:
- ./.dueutil_db:/dueutil_data
# ---------------
# SPECIFY NETWORK
networks:
- app
# ---------------
Upvotes: 2