How to connect to the Postgresql service inside Docker Swarm?

I have a Docker Swarm cluster setup with 3 servers (1 manager and 2 employees).

I started the Postgresql service using the following command:

docker service create --name postgresql \
  --mount src=pg_data,dst=/var/lib/postgresql/data/pgdata \
  -p 6542:5432 \
  -e POSTGRES_USER="user" \
  -e POSTGRES_DB="db" \
  -e POSTGRES_PASSWORD="pass" \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  --constraint 'node.role == manager' \
  postgres

I also created a data volume earlier:

docker volume create pg_data

Now I have another service that I want to run, basically this is a Java application that I put in a Docker image and I want to connect it to the postgresql service. I tried the following combinations for the url:

jdbc: postgresql: //172.18.0.1: 5432 / db (docker_gwbridge)

jdbc: postgresql: //172.17.0.1: 5432 / db (docker0)

JDBC: PostgreSQL: // local: 5432 / db

JDBC: PostgreSQL: // PostgreSQL: 5432 / db

Any idea what might work?

+4
source share
1

overlay network .

:

docker network create -d overlay mynet

, postgresql :

docker service create --name postgresql \
  --mount ... \
  --network mynet \
  postgres

Java-:

docker service create --name myjavaapp \
  --network mynet \
  myjavaapp

postgresql DNS-, :

jdbc:postgresql://postgresql:5432/db

mynet ( , , ), DNS, . , IP- docker inspect Java.

publish -p 6542:5432 postgresql, , , .

, .

SO QA .

+3

Source: https://habr.com/ru/post/1680450/


All Articles