Java and JDBC for Oracle DBAs – Part 1: Oracle Database in Docker

Working together with Java developers I sometimes have to test aspects of the JDBC programming. When cooperating with the Java developers it is also very convenient to know some Java. In this blog series I’ll describe how you can build your own Java test application, and how you can get a little more acquainted with the Java world. The blog series will not be a tutorial in Java. You can find several Java tutorials on internet (for instance – https://docs.oracle.com/javase/tutorial/).

Part 1: Oracle Database in Docker

Before Docker I used VirtualBox for testing Oracle stuff on my laptop. Together with Vagrant I could startup a new Oracle database on my laptop (or drop one) in matter of a few minutes. The downside was the size of the VMs and images. In addition to the Oracle software and database files, the Virtualbox vm (or image) also had to include an operating system (I prefered Linux). When Docker came around, working with containers, we could suddenly focus on the Oracle software, and leave out the OS installation. Soon after, when Oracle released their official docker images (https://github.com/oracle/docker-images), Docker became my obvious visualization platform.

This might be rather known by many Oracle DBAs, but I do a quick description of how to setup an Oracle Database in an Docker environment.

Installing the Docker software

Download

You will find the installation files for the docker community edition and your environment at https://hub.docker.com/search?q=&type=edition&offering=community. To complete the  installation of Docker, follow the description for your environment:

  • Mac
  • Windows
  • Linux (Find your distro in linked page: CentOS, Debian, Fedora, Raspbian or Ubuntu)

Verify that Docker is running

Windows:
The docker image (Whale) will show the notification area. Right click to get the docker menu. For instance you can open Kitematic, which gives you an graphical user interface on top of Docker.

Note! The image is copied from https://store.docker.com/editions/community/docker-ce-desktop-windows

Mac:
(Picture from https://store.docker.com/editions/community/docker-ce-desktop-mac)
When docker is started and ready to be accessed from the console, the docker image will show in the top status bar on you Mac. Clicking on the icon will give you the docker menu, where you for instance could start Kitamatic (a graphical user interface on top of docker).
Linux:

The easiest way to check if docker is running in linux is to run the docker command. When I used to use Linux (I’m on Mac now), I used to run:

~$ docker info
Client:
 Debug Mode: false

Server:
 Containers: 3
  Running: 1
  Paused: 0
  Stopped: 2
 Images: 116
...

If the command returns an error message then you know Docker is not running. If you see something like the output above, Docker is up and running.

Genaral

You can also verify the docker installation by running the following in a console window:

~$ docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:13:02 2018
OS/Arch: darwin/amd64
Experimental: false
Orchestrator: swarm

Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:22:38 2018
OS/Arch: linux/amd64
Experimental: false
Running your first container in Docker

The last test is to start a simple docker container:

~$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

To try something more ambitious, you can run an Ubuntu container with:

$ docker run -it ubuntu bash
root@6362896bd64f:/#

Installing Git, a distributed version control system

Java Developers have used version control systems (also called source control systems) for decades. Before Git came along in 2005 the most common systems were CVS and Subversion (also known as SVN). The last years Git have become very popular, and we will be using it for this blog series. We’ll only be using Git in a very simple matter, but I include it in these series because it’s a very important tool for Java developers, and actually is very convenient to use also for Oracle DBAs.

Linus Torvalds once said:

“When I say I hate CVS with a passion, I have to also say that if there any SVN users in the audience, you might want to leave. Because my hatred of CVS has meant that I see Subversion as being the most pointless project ever started, because the whole slogan for the Subversion for a while was “CVS done right” or something like that. And if you start with that kind of slogan, there is nowhere you can go. It’s like, there is no way to do CVS right”

Well … we’ll listen to Linus and use “git” as our source control solution. Many Java developers are using this instead of cvn, subversion or other version control systems.

Download and install

You can download the installation files from https://git-scm.com/downloads. If you are using Mac, then download the installation file, and follow the installation wizard when running. If you are using Windows, then choose the 32-bits or 64-bits setup file, and follow the installation wizard when running. If your are using Linux, follow the installation description for your distribution (using apt, yum and etc).

To complete the installation , please follow the installation description for your environment at https://git-scm.com/downloads.

To check your git installation run the following in your console:

~$ git version
git version 2.15.2 (Apple Git-101.1)

For the documentation and a tutorial in Git, take a look at the following links:

GitHub

GitHub is a place where developers can store their projects and share with other people. You can also have a private repository at GitHub, but this costs money. As long as you share your projects with everyone else on GitHub, the use of this centralized public repository is free. I will share all code generated and used in this blog series in GitHub. Later I’ll post all the example in this blog series on GitHub.

Creating and starting a new Oracle database in Docker

We’ll use the official git repository “oracle-images” found on github: https://github.com/oracle/docker-images

Create a new directory for you git projects

Run the following command in your console to create a directory for you local git repository:

~$ mkdir -p /User/lassejenssen/git-local

Download the “Oracle-images” from git-bug

Run the following command in your console to download the needed files from github:

~$ cd /User/lassejenssen/git-local
git-local$ git clone https://github.com/oracle/docker-images.git
Cloning into 'docker-images'...
remote: Counting objects: 8536, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8536 (delta 1), reused 4 (delta 1), pack-reused 8527
Receiving objects: 100% (8536/8536), 9.85 MiB | 584.00 KiB/s, done.
Resolving deltas: 100% (4837/4837), done.

git-local$ ls -l
total 0
drwxr-xr-x 31 lassejenssen staff 992 Jul 14 19:01 docker-images

git-local$ cd docker-images; ls -l
total 40
-rw-r--r--  1 lassejenssen staff 871  Jul 14 19:01 CODEOWNERS
-rw-r--r--  1 lassejenssen staff 5323 Jul 14 19:01 CONTRIBUTING.md
drwxr-xr-x  6 lassejenssen staff 192  Jul 14 19:01 ContainerCloud
drwxr-xr-x 10 lassejenssen staff 320  Jul 14 19:01 GlassFish
-rw-r--r--  1 lassejenssen staff 1850 Jul 14 19:01 LICENSE
drwxr-xr-x 12 lassejenssen staff 384  Jul 14 19:01 NoSQL
drwxr-xr-x  9 lassejenssen staff 288  Jul 14 19:01 OpenJDK
drwxr-xr-x  6 lassejenssen staff 192  Jul 14 19:01 OracleBI
drwxr-xr-x  4 lassejenssen staff 128  Jul 14 19:01 OracleCloudInfrastructure
drwxr-xr-x  7 lassejenssen staff 224  Jul 14 19:01 OracleCoherence
drwxr-xr-x  5 lassejenssen staff 160  Jul 14 19:01 OracleDataIntegrator
drwxr-xr-x  6 lassejenssen staff 192  Jul 14 19:01 OracleDatabase
drwxr-xr-x  6 lassejenssen staff 192  Jul 14 19:01 OracleEDQ
drwxr-xr-x  6 lassejenssen staff 192  Jul 14 19:01 OracleFMWInfrastructure
drwxr-xr-x  8 lassejenssen staff 256  Jul 14 19:01 OracleGoldenGate
drwxr-xr-x  6 lassejenssen staff 192  Jul 14 19:01 OracleHTTPServer
drwxr-xr-x  4 lassejenssen staff 128  Jul 14 19:01 OracleInstantClient
drwxr-xr-x  5 lassejenssen staff 160  Jul 14 19:01 OracleJava
drwxr-xr-x  7 lassejenssen staff 224  Jul 14 19:01 OracleRestDataServices
drwxr-xr-x  8 lassejenssen staff 256  Jul 14 19:01 OracleSOASuite
drwxr-xr-x 15 lassejenssen staff 480  Jul 14 19:01 OracleTuxedo
drwxr-xr-x  5 lassejenssen staff 160  Jul 14 19:01 OracleUnifiedDirectory
drwxr-xr-x  6 lassejenssen staff 192  Jul 14 19:01 OracleWebCenterSites
drwxr-xr-x  8 lassejenssen staff 256  Jul 14 19:01 OracleWebLogic
-rw-r--r--  1 lassejenssen staff 2341 Jul 14 19:01 README.md

We’ll be using the “OracleDatabase” directory, so if you want you can delete the rest. The following command will work both on Mac and Linux:

docker-images$ ls | grep -v OracleDatabase | xargs rm -rf

Creating an Oracle database docker image

Now we are ready to create (or “build” which is the docker terminology) a docker image for hosting our Oracle 12c database.

Download

First you’ll need to download the Oracle database installation files and put these in the  correct version directory. We’ll use the Oracle version 12.2.0.1, and you’ll find the download files at: https://www.oracle.com/technetwork/database/enterprise-edition/downloads/oracle12c-linux-12201-3608234.html.

Check the “Accept License Agreement” and download the ‘linuxx64_12201_database.zip’ file. Place the file in the correct version directory:

git-local$ mv ~/Downloads/linuxx64_12201_database.zip docker-images/OracleDatabase/SingleInstance/dockerfiles/12.2.0.1/.
Building your oracle/database docker image

Run the following to build the new image for your Oracle database:

git-local$ cd docker-images/OracleDatabase/SingleInstance/dockerfiles
dockerfiles$ sh buildDockerImage.sh -v 12.2.0.1 -e -i 

When the build completed you can verify that the build finished successfully by listing your docker images (Note! I have also created other images):

$ docker images
REPOSITORY          TAG          IMAGE ID      CREATED        SIZE
jetty               latest       356d3c58830   2 weeks ago    320.6 MB
oraclelinux         latest       1988eb5b3fc6  3 months ago   278.2 MB 
oracle/database     12.1.0.2-ee  20d33fcd7ea8  5 days ago     6.45 GB
oracle/database     12.2.0.1-ee  f3e98ecd1de1  1 hours ago    14.8 GB
Starting container and creating you Oracle database

We’ll now create a new Oracle database for our Java tests. We’ll create a PDB named “orcl”, and make the database available through port 1521 on our host machine.

To be able to persist the database through container shutdowns we map the container directory “/opt/oracle/oradata” to a local directory on the host machine (“/Users/lassejenssen/docker-oradata/ora12c-db01”).

Run the following command to start the database:

~$ mkdir -p /Users/lassejenssen/docker-oradata/ora12c-db01
~$ docker run --name ora12c-db01 -p 1521:1521 -e ORACLE_SID=CDB -e ORACLE_PDB=ORCL -v /Users/lassejenssen/docker-oradata/ora12c-db01/:/opt/oracle/oradata  oracle/database:12.2.0.1-ee

The creation of the new database will take a while. When you see the following, the database will be ready for access:

#########################
DATABASE IS READY TO USE!
#########################

Now you need to reset the password for your system and SYS user. For simplicity I’ll set the password to “manager”. To do this run the following command on another console:

~# docker exec ora12c-db01 ./setPassword.sh manager

Now you are ready to access your database from you local client (for instance SQL Developer):

Start a (bash) shell towards your running container

Sometimes it very convenient to have a shell towards your running container. For instance – if you need to login as SYSDBA with SqlPlus. To open a new shell run the following in a console:

~# docker exec -it ora12c-db01 /bin/bash
~# ps -ef | grep smon
oracle      65     1  0 Jul15 ?        00:00:17 ora_smon_CDBORCL
oracle    9635  9590  0 20:01 pts/3    00:00:00 grep --color=auto smon

~# sqlplus "/as sysdba"
SQL*Plus: Release 12.2.0.1.0 Production on Wed Jul 18 20:02:08 2018

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL>

As you see, we now have a shell inside the container, where our database are running. Now we are ready to step on to part 2.

Post a Comment

Your email is never published nor shared. Required fields are marked *