java – How can i connect to the oracle database between docker containers?

java – How can i connect to the oracle database between docker containers?

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.

java – How can i connect to the oracle database between docker containers?

Leave a Reply

Your email address will not be published.