Create an Oracle database Docker image
Tags: docker, database, oracle
Published: 2021-08-23 19:54:30

In this article, I will show how to run Oracle Database Express Edition using Docker.

## Requirements
[git](https://git-scm.com/)
[Docker](https://www.docker.com/)
[Oracle binaries](https://www.oracle.com/technical-resources/) (optional)

## Why a post dedicated to Oracle?
On DockerHub you can find images for most of the common databases such as PostgreSQL or MariaDB. But when it comes to Oracle, all the good things come to an end. 

![dinkleberg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u60sjj757rw0b7fo6m5k.jpg)

Due to (I think) copyright reasons, Oracle doesn't provide images that can be pulled and run on a Docker instance.

But strangly, they provided an alternative solution.

## Running Oracle Database in Docker

Oracle made available a suite of Docker build scripts that help set up many of its product offerings, including Oracle Database. You can find these scripts in a [GitHub repository](https://github.com/oracle/docker-images).

![i'll create my own docker image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4bk7akz7wd9k898flrl.jpg)

We'll deploy the version **18.4.0-xe** because it is the only version that doesn't requires pre-downloaded binaries. For any other version, you'll need to download the required binaries as described below.

**_Note:_** All the step marked as (Optional) can be skipped for the version **18.4.0-xe**, but are required for any other version.

### (Optional) Download Oracle binaries
Go to [Oracle Download page](https://www.oracle.com/technical-resources/). and download the db version you require, for example `oracle-database-xe-18c-1.0-1.x86_64.rpm`

### Clone Oracle Github project
```
$ git clone https://github.com/oracle/docker-images.git
```

### (Optional) Copy the database binaries into the project folder
```
$ cp /path/to/file/oracle-database-xe-18c-1.0-1.x86_64.rpm /path/to/project/docker-images/OracleDatabase/SingleInstance/dockerfiles/18.4.0
```

### Execute the installer. For the options description, see the command helper
```
$ cd docker-images/OracleDatabase/SingleInstance/dockerfiles
$ ./buildContainerImage.sh -v 18.4.0 -x
```

**_WARNING: the script can take up to 30 minutes before finishing_**

### Check the image
List the docker images:
```
$ docker images
```
and check that there's an image tagged as `oracle/database                      18.4.0-xe`

Pay attention to the size of the image: sometimes the installation does not complete completely but the script ends without reporting any problems. The image should be approximately 5.89GB in size

### Run the container
```
$ docker run --name oracle \
    -d \
    -p 51521:1521 \
    -p 55500:5500 \
    -e ORACLE_PWD=mysecurepassword \
    -e ORACLE_CHARACTERSET=AL32UTF8 \
    oracle/database:18.4.0-xe
```

Where 
* `--name`: specifies the container name
* `-d`: launch the container in detached mode
* `-p 51521:1521` and `-p 55500:5500` map a host to a container port.
* `-e ORACLE_PWD=mysecurepassword` and `-e ORACLE_CHARACTERSET=AL32UTF8` set the environment variables. Here, `ORACLE_PWD` sets the administrative password, and `ORACLE_CHARACTERSET` sets the database’s character set.

You can check the container status via the `docker run` command, looking the `STATUS` field:
```
$ d ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED              STATUS                                 PORTS                                                                                      NAMES
f7776a6dd664   oracle/database:18.4.0-xe   "/bin/sh -c 'exec $O…"   About a minute ago   Up About a minute (health: starting)   0.0.0.0:51521->1521/tcp, :::51521->1521/tcp, 0.0.0.0:55500->5500/tcp, :::55500->5500/tcp   oracle
```
In the example, the container is still starting (`health: starting`). Once the container is started, the status will change in `healty`.
Another way to check the container status is looking the container logs:
```
$ docker logs oracle
## Omitted for brevity
*******************
Prepare for db operation
7% complete
Copying database files
29% complete
Creating and starting Oracle instance
30% complete
31% complete
34% complete
38% complete
41% complete
43% complete
Completing Database Creation
47% complete
50% complete
Creating Pluggable Databases
54% complete
71% complete
Executing Post Configuration Actions
93% complete
Running Custom Scripts
100% complete
Database creation complete. For details check the logfiles at:
 /opt/oracle/cfgtoollogs/dbca/XE.
Database Information:
Global Database Name:XE
System Identifier(SID):XE
Look at the log file "/opt/oracle/cfgtoollogs/dbca/XE/XE.log" for further details.

Connect to Oracle Database using one of the connect strings:
     Pluggable database: 2540b6bf28f7/XEPDB1
     Multitenant container database: 2540b6bf28f7
Use https://localhost:5500/em to access Oracle Enterprise Manager for Oracle Database XE
The Oracle base remains unchanged with value /opt/oracle
#########################
DATABASE IS READY TO USE!
#########################

``` 

Since every time you launch the container the database instance needs to be installed, the container takes several minutes before it is ready.

### Connecting to the database
You can connect the database using any client you want. In the example I've used [dbeaver](https://dbeaver.io/):

![Connect to the db](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olubqibsihg1ufbravq8.png)

Alternatively, create a bash session within the container, using the OS user `oracle`:
```
$ docker exec -it --user=oracle oracle bash
```

Set up the required Linux environment variables for Oracle Database:
```
bash-4.2# . oraenv
ORACLE_SID = [XE] ? 
The Oracle base remains unchanged with value /opt/oracle
```

Then access SQL*Plus as you would on any Oracle Database system:
```
bash-4.2# sqlplus sys@XEPDB1 as sysdba

SQL*Plus: Release 18.0.0.0.0 - Production on Mon Aug 23 19:49:33 2021
Version 18.4.0.0.0

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

Enter password: 

Connected to:
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0

SQL> 

```

## Coming next
Currently the database is working, but running the container requires a lot of time. In next posts I'll show how to improve starting time using different methods.


## Sources
https://github.com/oracle/docker-images
https://blogs.oracle.com/oraclemagazine/deliver-oracle-database-18c-express-edition-in-containers