Create an Oracle Database Docker Image
A senior developer gives a tutorial on running an Oracle database inside a Docker container, and connecting the two with a Red Hat middleware, JBoss Fuse.
Join the DZone community and get the full member experience.Join For Free
We often need to create or run applications that rely on databases like Oracle or SQL Server. The big problem is that we are often unable to access a corporate server for our tests. Another problem is that third parties' actions in a shared database can affect our tests or development. For these scenarios, using a Docker image is a great ally, as we can have an exclusive bank available for our development activities.
In this tutorial, we will:
- Create a docker image for Oracle Database.
- Connect them using Red Hat JBoss Fuse.
- Perform a SELECT and INSERT.
Prepare the Container
Firstly, download or clone the files of the repository: https://github.com/oracle/docker-images/.
The second thing to do is download the binary files for the Oracle Database version of your choice. I will use version 19.3.0 for Linux. The link to download is: https://www.oracle.com/database/technologies/oracle-database-software-downloads.html
After downloading the file (in my case, LINUX.X64_193000_db_home.zip), we need to copy it to the folder referring to the oracle version in the cloned folder. In this case, 19.3.0:
The third step is to build the image. Go to the folder with the Docker files in the cloned project (one level before the versions folder). To build the image, we will use the below script:
If all goes well, we will have the image created. To confirm this, run the below command:
Now, we can run our database. For this, use the below command:
When the container I ready, you can use the Enterprise Manager to verify it. Use port 5500 to access. In this case, https://127.0.0.1:5500/em. Use the username "system" and the password entered in the ORACLE_PWD property.
Connect Them in the Container
For this step, I will use a Red Hat JBoss Fuse 7.8 application, and I will perform queries using the component camel-sql.
The application I created is available on GitHub at https://github.com/iam-roger-io/fuse-oracle-example. Download the version from the branch called
Basically, we need to create a data source.
Notice that the IP address is the Docker IP and the SID is the name used "docker run .."
We need to create the username and password using a SQL tool. The default username, "system," doesn't has privileges for insert, only SELECT queries.
Before we do this, create a table called "person" in Oracle.
After the table is created, create the user "camel" like below.
In the project POM file, I set the dependency for the JDBC driver as ojdbc8.
It is recommended to use the Oracle BOM mechanism. Notice in the pom.xml file that there are two BOM definitions. One definition for Fuse 78 and a second definition for Oracle JDBC.
In the project, one route performs
select all registers in the Oracle Database and, in sequence,
insert a new register.
And that's all folks, run the application and notice its behavior. It is the same for Oracle in Docker or traditional installations; however, for us developers, the capacity of reproducing a complex database like an Oracle database in a simple container is a great facility.
Opinions expressed by DZone contributors are their own.