0

I have a rest api mounted on Springboot, and this is inside a docker container, the issue is that I want to connect to my local database, but it does not connect me, if someone knows what I can do, I appreciate it

docker-compose.yml

version: "3.7"
services:
  app:
    image: "bm_spring_boot:latest"
    build:
      context: ./BmApiRestV2/
      dockerfile: Dockerfile
    container_name: api_spring
    ports:
      - 8888:8080
    environment:
      - SPRING_DATASOURCE_URL=jdbc:oracle:thin:@//localhost:1521/xe
      - SPRING_DATASOURCE_USERNAME=system
      - SPRING_DATASOURCE_PASSWORD=root
    networks:
      spring-net:
        aliases:
          - spring-host
networks:
  spring-net:
    driver: bridge
    ipam:
      driver: default

exception

IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=ZXya/2jARQOjulME/+D4hA==)

DaxTter77
  • 123
  • 1
  • 11
  • Try "host.docker.internal" instead of "localhost" https://docs.docker.com/desktop/windows/networking/ Or if youre on linux, see https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal/61001152 – KeKru Jan 20 '22 at 19:36
  • It doesn't work, but thanks. I'm using windows – DaxTter77 Jan 20 '22 at 19:49
  • Maybe your oracle is configured to block connections, which do not come from 127.0.0.1 or localhost. Docker runs inside a Linux VM, so the connections do not come from localhost – KeKru Jan 21 '22 at 13:02

1 Answers1

0

"host.docker.internal" works fine on Mac

ping host.docker.internal
PING host.docker.internal (192.168.65.2): 56 data bytes
64 bytes from 192.168.65.2: seq=0 ttl=37 time=0.712 ms

But not on linux. There are different nameservers configured.

cat /etc/resolv.conf

In your case I would simply use the IP address of the host (where Oracle is running). For example, running alpine linux and connecting to Spring boot app

docker run --rm -ti alpine
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
curl http://10.0.1.24:14000/hello

Hello