Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
https://github.com/fuzziebrain/docker-oracle-xe
I am running an oracle express db as a docker container. From my host OS (ubuntu 19.04) I can successfully connect to the db using:
$ sqlplus sampleschema/12345@localhost:32118/XE
Connected to:
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0
Now I would like to start up another docker container and run a small java application that connects to the oracle db. The java application I am running is a slightly modified version of JDBCExample
:
https://www.mkyong.com/jdbc/connect-to-oracle-db-via-jdbc-driver-java/
package samples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
String jdbcUrl="jdbc:oracle:thin:@//localhost:32118/xe";
if (args.length > 0) {
jdbcUrl=args[0];
System.out.println ("JDBC URL is: " + jdbcUrl);
try (Connection conn = DriverManager.getConnection(
jdbcUrl, "sampleschema", "12345")) {
if (conn != null) {
System.out.println("Connected to the database!");
} else {
System.out.println("Failed to make connection!");
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
In the run.sh
script of the container image (being called in the ENTRYPOINT
in the dockerfile) for this application I have
JDBC_URL="jdbc:oracle:thin:@//172.18.0.2:32118/xe"
java -cp "/opt/sample:/opt/sample/ojdbc8.jar:/opt/sample/sample-app-1.0.1-SNAPSHOT.jar" samples.JDBCExample $JDBC_URL
where I got 172.18.0.2
by running ifconfig
in the oracle container. But I get this error:
SQL State: 08006
IO Error: The Network Adapter could not establish the connection
And I have verified that the jars listed above are in the expected location in the sample application container.
Some docker details. I start the oracle container with:
docker run \
-p 32118:1521 \
-p 35518:5500 \
--name=oracle-xe \
--network=sample-network \
oracle-xe:18c
and the sample application container with:
docker run --network=sample-network -it --name debug-app-container debug-app-image
so they are running on the same network.
Why does the connection fail to the oracle DB when its executed inside the sample java application container?
Also if I run the samples.JDBCExample
class on my host with jdbc:oracle:thin:@//localhost:32118/xe
using the exact same sample application jar and ojdc8.jar file it works fine (printing Connected to the database!
as expected).
–
You connect both of your containers to the same network so your containers will be able to communicate over this network and continer names will be DNS names resolved to their IP.
So your connection string should look like :
jdbc:oracle:thin:@//oracle-xe:1521/xe
and oracle-xe
will be resolved to database container IP over this common network. And also remember about the port. I assume your DB will be exposed at 1521
port at the DB container.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.