How to Run an Oracle Database Container

How to Run an Oracle Database Container

With Docker and Docker-Compose

Hi Argonauts, in college I had a dilemma in one of my classes and it was to use Oracle dataBase on my Arch based operating system and I didn't want to mess up my system with a database manager so I decided to use Docker for this process but on Dockerhub I didn't find a very clear documentation on how to create a container using the image provided by oracle.

Until in a gits I found a solution and today I bring you a guide so you can build your own OracleDB image plus I will also show you an image in Dockerhub that serves this purpose very well and is very well documented in case you don't want to do this process yourself.

Pre-Requisites

  • Docker & Docker-compose
  • Unix-like OS
  • Git

Build Oracle Docker Image

For the creation of our image we will use a repository provided by the official Oracle github account. What we are interested in is in the oracle database directory in single instances.

So the first thing we will do is to clone this repository using the following command:

git clone git@github.com:oracle/docker-images.git

Once cloned we will move to the following directory:

cd docker-images/OracleDatabase/SingleInstance/dockerfiles

Within this path we will find different versions of oracle database xe now we have to download through the official Oracle website the XE version we need.

For this case I will use version 21c although this is agnostic to any other version of oracleDatabase.

Note: Versions 11 and 12.1 are deprecated so I would not recommend their use because they do not currently support JDBC so they could not be used with a current driver.

Once we have downloaded the file we will see it inside the folder to the version that matches.

Then we will position again in OracleDatabase/SingleInstance/dockerfiles

sudo ./buildContainerImage.sh -v 21.3.0 -x

Please note that this process may take some time.

When the process is finished we will verify the creation of the image using the command:

docker images

And we will get an output very similar to the following:

REPOSITORY        TAG           IMAGE ID       CREATED         SIZE
oracle/database   21.3.0-xe     f38d45b4b7b4   1 minutes ago   6.53GB

Creating a Container Using The Built Image

Now that we have our image ready to be used we will create a directory '/templates' in Documents or wherever you prefer.

Inside we will create another directory called '/oracle-21' where we will create two files docker-compose.yml and .env and we will put the following:

docker-compose.yml

version: '3.9'

services:
  oracle:
    image: oracle/database:21.3.0-xe
    container_name: oracle-21-build
    restart: always
    environment:
      ORACLE_PWD: ${ORACLE_PWD}
    ports:
      - 1521:1521
      - 5500:5500
    volumes:
      - data-data:/opt/oracle/oradata

volumes:
    data-data:
        driver: local

.env

ORACLE_PWD=Admin12345

Once we have our files created, we will open a terminal inside the directory and proceed to create our container using docker-compose.

docker-compose up -d

This process takes a while so I recommend to check the logs to verify that it has been created correctly and the service is already running.

docker-compose logs
docker-compose ps

When all this process is finished we will be able to access inside the container and perform some test queries with:

docker-compose exec oracle /bin/bash

sqlplus sys as sysdba

SELECT NAME FROM v$database;

Now we will connect to a client like Dbeaver to be able to use our database manager for this we must pass it the following connection parameters :

  • Host: localhost
  • Database: XE -> SID
  • Authentication:
    • User: sys
    • Password: Admin12345
    • Role: SYSDBA

Screenshot_20220709_111652.png

And with this we can now manage our manager and start working.

Creating a Container Using a Dockerhub Image

Of course, not everyone likes the idea of creating from scratch the whole process of building an image so here I bring an alternative to streamline this process with a DockerHub image that is very well documented and using the database that brings by default the creation of the container is fast enough.

So in our '/templates' folder we will create a new directory that we will call "oracle-dh", we will create our two files docker-compose.yml and our .env and we will write the following:

docker-compose.yml

version: '3.9'

services:
  oracle:
    image: gvenzl/oracle-xe:21.3.0
    container_name: oracle-21
    restart: always
    environment:
      #APP_USER: ${APP_USER}
      #APP_USER_PASSWORD: ${APP_USER_PASSWORD}
      ORACLE_PASSWORD: ${ORACLE_PASSWORD}
      #ORACLE_DATABASE: ${ORACLE_DATABASE}
    ports:
      - 1521:1521
    volumes:
      - data-or:/opt/oracle/oradata

volumes:
    data-or:
        driver: local

.env

APP_USER=galex
APP_USER_PASSWORD=12345
ORACLE_PASSWORD=admin
ORACLE_DATABASE=XE

As explained in the documentation now to be able to use the oracle manager with the default database are the following parameters and to be able to visualize it better I will use Dbeaver as client.

  • Host: localhost
  • Database: XEPDB1 -> Services Name
  • Authentication:
    • User: sys
    • Password: admin
    • Role: SYSDBA

Screenshot_20220709_061525.png

We must not forget to stop our container when we finish working so that the service does not remain in the background and we must not worry about losing information since we have a volume where everything we do will be stored.

docker-compose down

Conclusion

Docker provides us with an easy and clean way for the installation of our database managers also we avoid messing up our operating system and saving computer resources.

Â