How to set jdbc url to connect to database container - postgresql

I'm usiing docker-compose to containerize my rest api. One service for my spring app et the other for the database. The probleme is the spring boot api try to connect by default to the database by using localhost but since it is the container it create an error ( Connection to localhost:5432 refused. Check that the hostname and port). And the container stop working since the app don't work. How to tell to my spring boot api to try to conect to the database container.
version: "3"
services:
app_service:
container_name: app_service
build: .
ports:
- "8080:8080"
depends_on:
- postgresql
networks:
- net
postgresql:
image: postgres:15.0
container_name: db
ports:
- "5432:5432"
restart: unless-stopped
environment:
POSTGRES_PASSWORD : Access/fedora/6
POSTGRES_DB : postgres
volumes:
- psql:/var/lib/postgres/psql
networks:
- net
volumes:
psql:
networks:
net:
cloud:
aws:
credentials:
secret-key: 'qdfqdfqsdfqsdqsdfqsdfqs'
access-key: 'qfqsdfqserfqegdrhfjkgkh'
region:
static: eu-west-3
stack:
auto: 'false'
spring:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: 'true'
show-sql: 'true'
hibernate:
ddl-auto: update
servlet:
multipart:
max-file-size: 5MB
enabled: 'true'
max-request-size: 10MB
file-size-threshold: 2MB
datasource:
hikari:
jdbc-url: jdbc:postgresql://db:5432/postgres
password: Access/fedora/6
application:
bucket:
name: moood-app
I tried to search if there was a image of a spring project that I could use but i did'nt find one that can help me.
I tried to find a way to set a default jdbc url for the spring container but didn't find it.
I hope to find a way to set the connexion between the containers

You can pass the value via environment. Something like this:
services:
app_service:
container_name: app_service
build: .
ports:
- "8080:8080"
depends_on:
- postgresql
networks:
- net
environment:
SPRING_DATASOURCE_HIKARI_JDBC_URL: jdbc:postgresql://db:5432/postgres

Related

Connect to Postgres in DataGrip

I try to connect to my Postgres database built by docker in DataGrip but I get connection error.
Here is my application.yml file:
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: update
datasource:
url: jdbc:postgresql://db:5432/postgis_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
username: postgres
password: postgres
Here is docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:latest
restart: always
volumes:
- db:/var/lib/postgresql/data
environment:
POSTGRES_DB: postgis_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
expose:
- 5432
networks:
- app-network
geolocation-service:
image: geolocation-service
build:
context: .
dockerfile: dockerfile
restart: always
ports:
- "8080:8080"
networks:
- app-network
volumes:
db:
networks:
app-network:
DataGrip connection settings:
And the error I get:
Does anybody knows how to solve this?
Connecting to this db by shell works fine:
docker exec -it a37 psql -U postgres postgis_db
Please check pg_hba.conf file, you have to allow connections from all hosts for this user, by default it is restricted to localhost.

Docker postgrsql not connected in spring boot

I have used docker-compose.yaml. I have configured in docker-compose.yml bellow like:
Step 1: In docker-compose.yaml I user bellow code
services:
postgres:
image: postgres
container_name: postgres
hostname: postgres
volumes:
- db-data:/var/lib/postgresql/data
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: accountdb
account-opening:
image: ehaque95/pocv1-account-opening:0.0.1-SNAPSHOT
mem_limit: 700m
ports:
- "8081:8081"
networks:
- account-network
depends_on:
- postgres
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/accountdb
volumes:
db-data:
Step 2 :
I have configure applicaiton.yml in spring boot bellow like:
spring:
datasource:
url: jdbc:postgresql://postgres:5432/accountdb
username: postgres
password: postgres
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
database: postgresql
hibernate:
ddl-auto: update
properties:
dialect: org.hibernate.dialect.PostgreSQLDialect
Note : postgresql server IP change frequently. When i run docker inpspect ,it shows some "IPAddress": "172.19.0.2" or sometimes "IPAddress": "172.19.0.3".It shows when I run docker-compose up again.
It shows connection error.what is the wrong of my code connect to postgresql. Please help me
For two containers to communicate with each other, they must be on the same Docker network. By default, Compose creates a network named default and attaches containers to it; if you specify other networks: for a container, then they are not attached to the default network.
In your docker-compose.yml, the account-opening container has a networks: block, but postgres doesn't:
version: '3.8'
services:
postgres:
...
# no networks:, so implicitly
# networks: [default]
account-opening:
...
networks: [account-network] # and not default
# The postgres container is on the default network
# This container is on account-network
# And so this host name doesn't resolve
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/accountdb
There's nothing wrong with using the default network. For most typical applications you can delete all of the networks: blocks in the entire file. Then the default network will get created with default settings, and all of the containers will attach to that network, and be able to address each other by their Compose service name.
try
url: jdbc:postgresql://localhost:5432/accountopendb
if you are running both the spring app and the postgresql container in the same machine in your datasource config.

Docker: Unable to connect one container with another on same network

I've a following docker-compose.yaml file:
version: "3"
services:
# Database
database:
image: mongo
container_name: database
restart: always
volumes: ["dbdata:/data/db"]
environment:
- MONGO_INITDB_ROOT_USERNAME=myusername
- MONGO_INITDB_ROOT_PASSWORD=mypassword
ports:
- 7000:27017
command: mongod
networks:
- webapp
# Server
server:
container_name: server
restart: always
build: ./server
volumes: ["./server:/var/www", "/var/www/node_modules"]
ports:
- "9000:3000"
depends_on:
- database
networks:
- webapp
networks:
webapp:
driver: bridge
volumes:
dbdata:
Now, I'm able to connect with my mongo database from my local windows machine through mongodb client but my server container is not able connect with database container.
I'm using following connection URI on my server to establish a connection:
mongodb://myusername:mypassword#database:7000/mydatabase
It must be noted that on my local machine I'm using the same URI to connect successfully with the database, the only difference is that I'm using localhost instead of database (container name) on my local machine.
Can you please tell me what I'm doing wrong here?

Docker Compose - Service name not working as environment variable

I'm trying to use docker-compose to setup a Spring Cloud project.
I'm using spring cloud configuration so I have a Configuration Server and some services.
For now, I have 3 services in my docker-compose.yml
version: '3'
services:
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'root' # TODO: Change this
MYSQL_USER: 'user'
MYSQL_PASS: 'password'
MYSQL_ROOT_HOST: '%'
volumes:
- "db:/opt/mysql/docker:rw"
ports:
- "3307:3306"
config:
image: config-server
restart: always
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/ec_settings?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
ports:
- "8100:8100"
gateway:
image: gateway
restart: always
depends_on:
- config
environment:
- CONFIG_URI=http://config:8100
ports:
- '8081:8080'
volumes:
db: {}
In gateway microservice, in bootstrap.yml, i have this setting
spring:
cloud:
config:
uri: ${CONFIG_URI}
When i put up the docker composer i see that gateway service is trying to fetch configuration from http://config:8100
Fetching config from server at : http://config:8100
So, the variable passes to Spring Boot but docker-compose does not replace the service name with its actual link.
The very strange thing is that SPRING_DATASOURCE_URL environment variable gets translated correctly in config service to connect to db service.
I finally solved it thanks to this link Docker - SpringConfig - Connection refused to ConfigServer
The problem was the service was trying to fetch the config url too early.
Solution is to put in bootstrap.yml this settings
spring:
cloud:
config:
fail-fast: true
retry:
max-attempts: 20

Connection Error accessing postgres docker container

I setup a prisma project recently and here is my docker-compose.yml file
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:3306"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
I can open my prisma playground and it functions without any issue. but I can't create a direct connection to the postgre container with dbeaver.
dbeaver Error message
Connection reset
Why my connection to the database fails?
This photo will be helpful.
postgres by default listens on port 5432.
In your postgres container specification, you should expose the port 5432 not 3306.
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
If 5432 port in your host is already in use and if you want to use 3306 instead , then you can do port forwarding like below:
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
UPDATE - 1
Reason why prisma can access postgres
ports section is meant only to make our services accessible in host level. But in the container level, If a port is open in a container, any other running container can access that port with the help of networks or links section.
By default, docker-compose will create a network per docker-compose.yml file and joins all the services in that file into that network.
That's the reason we could use the <service name> as the hostname and compose will resolve that name to the ip-address of the respective (in your case postgres) container.