Search code examples
javaspringoraclespring-bootdocker

Cannot connect to Oracle database within a Spring Boot application using docker compose


I've a Spring boot application which I'd like to connect with an Oracle database, containerised using Oracle Database Free Container / Docker images. image.

Additionally, I use docker compose to configure the database deployment and Spring Boot Docker Compose to run the deployment on application startup.

However, My application cannot connect to the database due to the following error:

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=V0w3NC+xSxaZYliy+XojdA==)
    at oracle.jdbc.driver.T4CConnection.handleLogonNetException(T4CConnection.java:892) ~[ojdbc8-21.9.0.0.jar:21.9.0.0.0]
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:697) ~[ojdbc8-21.9.0.0.jar:21.9.0.0.0]

The Oracle container looks good:

Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC_FOR_FREE)))
2023-12-18T19:40:23.357410157Z Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.0.0)(PORT=1521)))
2023-12-18T19:40:23.357437163Z 
2023-12-18T19:40:23.357462169Z Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_FREE)))
2023-12-18T19:40:23.377109429Z STATUS of the LISTENER
2023-12-18T19:40:23.377387489Z ------------------------
2023-12-18T19:40:23.377432499Z Alias                     LISTENER
2023-12-18T19:40:23.377468507Z Version                   TNSLSNR for Linux: Version 23.0.0.0.0 - Production
2023-12-18T19:40:23.377499514Z Start Date                18-DEC-2023 19:40:23
2023-12-18T19:40:23.377528520Z Uptime                    0 days 0 hr. 0 min. 0 sec
2023-12-18T19:40:23.377556526Z Trace Level               off
2023-12-18T19:40:23.381577398Z Security                  ON: Local OS Authentication
2023-12-18T19:40:23.381807448Z SNMP                      OFF
2023-12-18T19:40:23.381845456Z Default Service           FREE
2023-12-18T19:40:23.381875462Z Listener Parameter File   /opt/oracle/product/23c/dbhomeFree/network/admin/listener.ora
2023-12-18T19:40:23.381903469Z Listener Log File         /opt/oracle/diag/tnslsnr/f8cd793dc1d4/listener/alert/log.xml
2023-12-18T19:40:23.381930474Z Listening Endpoints Summary...
2023-12-18T19:40:23.383162742Z   (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC_FOR_FREE)))
2023-12-18T19:40:23.383223755Z   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.0.0)(PORT=1521)))
2023-12-18T19:40:23.383255762Z The listener supports no services
2023-12-18T19:40:23.385380222Z The command completed successfully
2023-12-18T19:40:33.630788572Z ORACLE instance started.

Also, the datasource configuration and docker compose setup are inline with gvenzl/oracle-free:latest image setup instructions

Spring application config:

spring:
  liquibase:
    enabled: false
  datasource:
    url: jdbc:oracle:thin:@localhost:1521/FREEPDB1
    username: PERSONS_DB
    password: Lider0ne

Docker compose setup

services:
  oracle:
    image: 'gvenzl/oracle-free:latest'
    environment:
      - 'ORACLE_RANDOM_PASSWORD=true'
      - 'APP_USER=PERSONS_DB'
      - 'APP_USER_PASSWORD=Lider0ne'
    ports:
      - 1521:1521

Finally, I get a similar error connecting to the database using IntelliJ Database tool window.

IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=gZO5dOjYRGKjaOL469MGIw==).

enter image description here

NOTE: I run docker on Apple M chip

According to Oracle Database Free documentation, it shouldn't be problem, I followed its instructions to run the container using colima with --arch -x86_64 option. The container started successfully as explained above.

Have you experienced the same issue? If so, how could I fix it


Solution

  • I think you don't have a problem.

    I'm experiencing the same problem

    TRY ORACLE image: 'gvenzl/oracle-xe:latest'

    I don't know why, but based on my experience over the past week, I suspect that the local port of 1521 (local port):1521 (Docker Oracle port) is cached on the computer, causing a conflict. I think so because I can connect just fine if I change the local port.

    While the XE (service name or SID) service created with the Docker Oracle image was created on the computer, I created another container with a different port and tried to connect to it with the Oracle client, but I was only able to connect to the service I created first.

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here