Docker Community Forums

Share and learn in the Docker community.

Connecting to an OracleDB in a docker container from another java docker container

docker

(Abrahamjaison) #1

Hello there,

I am now working on a docker project with two docker containers - one for the oracle db and the other with a java application.

The container for oracle db is working ok. I used the already built image for oracle and created my tablespaces and users in it.

Commands I used to pull and use the oracle db container is as given below:

docker pull oracle11g

docker run -d -p 49160:22 -p 49161:1521 -e ORACLE_ALLOW_REMOTE=true oracle11g

Now I have my own Java application that interacts with the oracle db and I run it using the command given below:

docker run -it --name mypgm myrepo/oracletesting

It runs an interactive java program that asks for the Oracle DB details and allows users to interact with the DB.

However I could not figure out how I have to specify details such as
Driver Name, Connection URL, Username, and Password

The values I gave are as given below:
Driver Name: oracle.jdbc.OracleDriver Connection URL: jdbc:oracle:thin:@**localhost:1521**:orcl11g

I think I am getting the Connection URL wrong and the hence my program is not working.
I tried giving different inputs for Connection URL after inspecting the docker container ip address as well:

Connection URL: jdbc:oracle:thin:@ContainerIPAddress:1521:orcl11g

Am I giving the Connection URL and/or the port number correct? Can someone help me out to correctly connect these two containers and to get the project moving?

Thanks for your kind help…


(Richardpayne) #2

Two things stand out:

  1. You’re mapping the server’s 1521 port to port 49160 on the host but you’re still trying to use 1521 in the connection strnig.

  2. You’re trying to use localhost in the connection string when the testing app is running in it’s own container.

There’s a few ways around this, in order of preference (in my opinion):

  1. Create a docker network using “docker network create” and then use the --net option on the run commands to hook both the server and the client into the network. If you also use the --name option on the server container then you can then reference it from the client with “my_chosen_name:1521”

  2. Use the older --link option on the client run command.

  3. Carry on using the host port mapping but change the connection string to “hosts_ip:49161”.


(Abrahamjaison) #3

Thanks @richardpayne for your kind help… It worked…! :slight_smile:

Thanks a lot.

AJ