I have two containers that is created from docker-compose, one has api with springboot and the other oracle database, however it does'n connect the api to the database and I already ran out options, I tried configure the connection in the aplication.properties
and also docker-compose.yml
This is docker-compose.yml
version: "3.7"
services:
app:
image: "bm_spring_boot:latest"
build:
context: ./BmApiRestV2/
dockerfile: Dockerfile
container_name: api_spring
depends_on:
- db
ports:
- 8888:8080
environment:
- SPRING_DATASOURCE_URL=jdbc:oracle:thin:@//db:49161/xe
- SPRING_DATASOURCE_USERNAME=system
- SPRING_DATASOURCE_PASSWORD=oracle
networks:
spring-net:
aliases:
- spring-host
db:
image: oracleinanutshell/oracle-xe-11g:latest
container_name: oracle_db
ports:
- 49161:1521
- 5500:5500
environment:
- ORACLE_ALLOW_REMOTE=true
networks:
spring-net:
aliases:
- db-host
networks:
spring-net:
driver: bridge
ipam:
driver: default
Dockerfile
ARG VERSION=8-jdk-slim
FROM openjdk:${VERSION}
ARG JAR_FILE=BmApiRestV2
COPY "./target/${JAR_FILE}.jar" "app.jar"
EXPOSE 8080
ENTRYPOINT [ "java","-jar","app.jar"]
aplication.properties
#spring.datasource.username=system
#spring.datasource.password=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#DOCKER
#spring.datasource.url=jdbc:oracle:thin:@ (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = db)(PORT = 49161)) ) (CONNECT_DATA = (SID = xe) ) )
(The connection was commented because in the docker-compose
it's configured)
I appreciate the help (My apologies for my english)
-------------- UPDATE --------------
Connection test
ping db
PING db (172.20.0.2) 56(84) bytes of data.
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=1 ttl=64 time=0.221 ms
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=2 ttl=64 time=0.072 ms
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=3 ttl=64 time=0.072 ms
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=4 ttl=64 time=0.072 ms
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=5 ttl=64 time=0.144 ms
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=6 ttl=64 time=0.071 ms
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=7 ttl=64 time=0.161 ms
64 bytes from oracle_db.imagenspring_spring-net (172.20.0.2): icmp_seq=8 ttl=64 time=0.063 ms
^C
--- db ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7090ms
rtt min/avg/max/mdev = 0.063/0.109/0.221/0.054 ms
root@2254b03a8ef4:/#
CodePudding user response:
TLDR
Change the port to 1521 in your connection string.
Extended version
Your services and networking are configure properly. Both containers are in the same docker network, which allows them to communicate with each other.
Also, contrary to some of the comments, all 3 ways of calling the containers are correct:
- db
- db-host
- oracle_db.
This can be easily verified using a simplified docker-compose.yml
, just to test the connectivity:
version: "3.7"
services:
app:
image: busybox
command: "sh -c 'sleep 3600;'"
networks:
spring-net:
aliases:
- app-host
db:
image: busybox
command: "sh -c 'sleep 3600;'"
container_name: oracle_db
networks:
spring-net:
aliases:
- db-host
networks:
spring-net:
driver: bridge
ipam:
driver: default
If you try to resolve the names - they all point to the same container:
/ # ping db
PING db (172.21.0.2): 56 data bytes
64 bytes from 172.21.0.2: seq=0 ttl=64 time=0.593 ms
^C
--- db ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.593/0.593/0.593 ms
/ # ping oracle_db
PING oracle_db (172.21.0.2): 56 data bytes
64 bytes from 172.21.0.2: seq=0 ttl=64 time=0.213 ms
^C
--- oracle_db ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.213/0.213/0.213 ms
/ # ping db-host
PING db-host (172.21.0.2): 56 data bytes
64 bytes from 172.21.0.2: seq=0 ttl=64 time=0.334 ms
^C
--- db-host ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.334/0.334/0.334 ms
In fact, the problem seems to be with your connection string:
- SPRING_DATASOURCE_URL=jdbc:oracle:thin:@//db:49161/xe
port 49161
is the host port, while your connecting to the container (db
). In this case you should be using the 1521
port, which the server inside the container is bound to.