I have a spring boot application running as docker container on my local machine. It is connecting to Oracle database which is there on our company datacenter. I am able to connect to database directly from my local machine using sql developer. However, the spring application running as docker container is giving the below error when connecting to database. I am sure it has to do something related to port mapping or networking in docker container.
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
2
Answers
Your db-container is mapped to port 1521 on your host-network. This is why
SQL Developer
will work using something like this:localhost:1521/SERVICE_NAME
SQL Developer has only access to the host-network and port 1521 is exposed to the host-network.
Now, you want your app-container to connect to the db-container.
The app container does not have access to the host-network (unless it is configured for host-mode), it has only access to the docker-network like
172.17.0.2/16
Solution
Find the ip of the database container in the docker-network:
Now, you can build the connect string for your app.
Tip
It is more convenient to use docker compose where service discovery is available.
Now you can just configure your app like this:
db:1521/SERVICE_NAME
or justdb/SERVICE_NAME
(1521 is default and can be ommitted)Best of luck!
In scenarios like this, I would make sure sqlplus is working from inside a container. This will verify the connectivity to the database from within the docker-network.
Since it is a corporate database, I would have checked the firewall log for dropped packages.
login
one-liner for verification
Best of luck!