Start a personal dev blog on your domain for free with Hashnode and grow your readership.
Get Started

Learning Oracle Database with Docker

Why I use Docker

As a software engineering student, I have to get familiar with multiple technologies and stacks. This means, installing a lot of softwares along with their dependencies, sometimes just for a short period of time, i.e. a lecture or a practical work session. This has lead multiple times to my computer being cluttered with files and softwares I don't use at all. This is why I now use Docker as often as I can: I can simply download an image and run a container so that I have access to the software or the stack I have to learn. When I am done, I can simply stop and remove the container, so that it does not take much computing power, and even remove the image. My computer remains clean of all the potential dependencies and files that used to clutter my drives.

Following this logic, I now run Oracle database (v12.2.0.1) in a Docker container and learn this way.

Cons of using Docker to learn Oracle database with no experience at all

To start with, setting up Docker for Oracle database, was not exactly a breeze but I acknowledge it might have been harder. In facts, it was easier than I thought to get everything up and running, the hard parts were just after.

You have to learn Oracle database administration basics to get started

You might want to stick to VMs

For the practical work sessions I had, we were given an OVA (Open Virtual Appliance) from Oracle so that I could easily set up a working, preconfigured database along with all the softwares I needed for the session all in a single virtual machine. I have to admit, this is easy and I was happy not having to install 20 different softwares for the sake of a single class. But, I ran into a problem that quickly became a nightmare to me: the virtual machine is preconfigured but setting it up to work well with my computer (keyboard layout) was NOT easy. Once done, I just ran through the other problem that would cause me leaving this solution aside: the virtual machine took between 10 and 20 seconds to register any input, which is strange, considering I gave it twice the recommended specs.

The image is clean so you have to get your hands dirty

Setting up Docker for this class took me a long time which I should have spent practicing, so if you are in a hurry or have a powerful enough computer, just give virtual machines a go, it is a lot easier. This is mainly because once the Oracle Database container is up and running, in a healthy status, you have an access to a totally new database, with little to no configuration at all. This means, you will have to create users and grant permissions one by one as a database administrator before logging out and logging in with your new users. Once again, this is NOT easy because it requires going through a bit of documentation before understanding what you are doing.

Setting up Oracle Database with Docker

Now that you are aware of the cons of using such a setup, let's dive into the process of setting everything up!

Install Docker on your system

The image you will download takes up to 4GB disk space, keep it in mind before. Doing so should lead you to creating a Dockerhub account, if not, create an account.

Find the Oracle Database image

The image (Oracle Database 12c Enterprise Edition) is accessible on the Dockerhub but requires you to accept the license agreement.

Docker hub

Click on the "Proceed to Checkout" button and tick the boxes you like. The image is free to use in its Developer Tier, so do not worry: no banking information are required.

Log into your Dockerhub account

Ensure Docker is up and running, hop into your terminal and run the following command:

docker login

This will prompt you a Dockerhub username as well as your password. Type them in and you should be logged in. If you are already logged in, you should see no difference but this is good measure to log into your account again since it might refresh some changes.

Pull the image

There exists two versions of the image that you can use:

  • "Standard Edition": the image is about 4GB and includes a lot of tools to help working with the database. For most little projects, this image isn't fully required, so unless you have some serious reasons not to, you should opt for the second option.
  • "Slim Edition": the image is about 2.7GB and includes just the required tools. It is lighter and starts faster than the standard image.

Depending on the image of your choice, you will have to execute the following command:

For the "Standard Edition":

docker pull store/oracle/database-enterprise:12.2.0.1

For the "Slim Edition":

docker pull store/oracle/database-enterprise:12.2.0.1-slim

This might take a while, since the images are a few GBs.

Create a folder to allow data persistence

If you want your database to persist over multiple reloads, or to save its state, you should create a folder that docker has the access rights to.

mkdir ~/oracle-database-data

The above command will create a oracle-database-data in your personal directory.

If you are running Docker on Windows, please ensure the drive the folder is on is shared to Docker.

Create and run the container

Now that the image of your choice is pulled, you are only one step away to having a working Oracle Database 12c Enterprise Edition running on you system.

Depending on the image you chose, run the following commands:

  • For the "Standard Edition":

    docker run -d -ti --name <name-your-container> -p <host-port-to-use>:1521 -v <path-to-folder-on-host>:/ORCL store/oracle/database-enterprise:12.2.0.1
    
  • For the "Slim Edition":

    docker run -d -ti --name <name-your-container> -p <host-port-to-use>:1521 -v <path-to-folder-on-host>:/ORCL store/oracle/database-enterprise:12.2.0.1-slim
    

Let's analyze the above commands:

  • docker run: create a Docker container
  • -d -ti: run the container in detached interactive mode
  • --name <name-your-container>: give your container a custom name to better identify and reference it afterwards
  • -p <host-port-to-use>:1521: bind the container's 1521 port to the host port of your choice, this will allow to connect to your database
  • -v <path-to-folder-on-host>:/ORCL: bind the container's /ORCL directory to the directory of your choice on the host, this allows persistence
  • store/oracle/database-enterprise:12.2.0.1(-slim): image to use to create the container

Knowing all this, I personally use the following command to create the container:

docker run -d -ti --name oracle-database -p 1521:1521 -v ~/oracle-database-data:/ORCL store/oracle/database-enterprise:12.2.0.1-slim

Ensuring the container started well

The container should take up to 5 minutes to really start (due to the software inside needing to perform lots of operations), so you should monitor how it starts:

Run docker ps on multiple occasions and you should see the state of your container: this will display something looking like the following. You shouldn't do anything until you see Up ... ... (healthy), which means your database is ready to use.

Docker ps

In case of an error, or if you want to see the logs of the container (hence of the database), run the following command:

docker logs <name-of-your-container>

Basic Database Setup

Creating a custom user

That's it! You have created your Oracle Database 12c Enterprise Edition container!

Now, you should log in to your database in the tool of your choice (Oracle SQL Developer or JetBrains DataGrip, for example):

  • Host: localhost
  • SID: ORCLCDB
  • Port: the port you chose, in my case 1521
  • User: sys as sysdba
  • Password: Oradoc_db1

This will give you an administrator access to the database and allow you to run the following script to create a user:

ALTER SESSION SET "_ORACLE_SCRIPT" = true;
CREATE USER username IDENTIFIED BY a_password;
GRANT RESOURCE TO username;
GRANT CONNECT TO username;
GRANT CREATE VIEW TO username;
GRANT CREATE SESSION TO username;
GRANT UNLIMITED TABLESPACE TO username;
GRANT SELECT ON SYS.DBA_RECYCLEBIN TO username;
GRANT CREATE ANY TRIGGER TO username;
GRANT CREATE ANY PROCEDURE TO username;
GRANT CREATE SEQUENCE TO username;
GRANT CREATE SYNONYM TO username;
GRANT CREATE TABLE TO username;

Just replace username by the name of the user you want to create and a_password by the password you want this user to log in with.

Log in to your database with your custom user

Using the same settings as above, just change the User and Password to the one you set with the script above and you should be able to work with your database!


Thanks for reading!

Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown · Publish articles on custom domain · Gain readership on day zero · Automatic GitHub backup and more

No Comments Yet