skip to Main Content

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


  1. 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:

    docker container inspect free-23ai | grep '"IPAddress":'
            "IPAddress": "",
                    "IPAddress": "172.21.0.2",
    

    Now, you can build the connect string for your app.

    172.21.0.2:1521/SERVICE_NAME
    

    Tip

    It is more convenient to use docker compose where service discovery is available.

    services:
      app:
      db:
    

    Now you can just configure your app like this: db:1521/SERVICE_NAME or just db/SERVICE_NAME (1521 is default and can be ommitted)

    Best of luck!

    Login or Signup to reply.
  2. 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

    cat ~/.password | docker login container-registry.oracle.com --username [email protected] --password-stdin
    

    one-liner for verification

    docker run -ti --rm container-registry.oracle.com/database/instantclient sqlplus username@"db-hostname:1521/SERVICE_NAME"
    

    Best of luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search