Installing Oracle Database using Docker

Oracle databases are very popular in production environments, after Postgres and mysql, because of its robustness.

It's also popular for being notoriously difficult to install in non-production environments, like the ones students and small scale applications use.

The traditional installation process can be tiresome, often having to choose compatibility modes. Uninstalling the database once it's not needed is even more difficult.

That's why I decided to share my experience of installing Oracle DB using Docker. As a student, I find this method the most comfortable in getting the database up and running sio that I could focus on the development rather than configuration.

Why use Docker for Oracle DB setup?

Docker can be used to deploy lightweight containers of the oracle database. These are disposable too, hence removing them will not be a hassle.

Also, docker is available for all operating systems, hence these installation instructions can even be followed even on Windows!

Before getting into the tutorial, make sure you completely set up docker for your distribution. Ubuntu users can start here:

How to Install Docker on Ubuntu Linux [Beginner Tutorial]
In the first of Docker tutorial series, you’ll learn to install the latest version of Docker Engine Community Edition on Ubuntu Linux.

Obtaining Oracle Database image

Once you have installed docker, it's time to fetch the relevant image for your setup. For this tutorial, I am making use of the latest express edition database (it's 21c during the time of writing).

Start with pulling the docker image, using the following command:

docker pull container-registry.oracle.com/database/express:latest
Download the required docker image
💡
If you are unable to run docker commands normally, try running them as sudo, or add your user to the docker group.

Once it has finished downloading, you can check for the downloaded images with the command

docker image ls
List of all downloaded images

Your downloaded image should show up there.

Setting up the Docker container

Docker compose scripts are simple YAML files used to write the configuration of the docker container. This config file comes in very handy, since it makes it far easier to change settings.

The setup also involves creating a docker volume for the persistent storage of data.

Creating a volume: persistent storage

Docker containers are temporary. Data stored in them maybe lost during a container restart. A docker volume is a persistent storage that exists outside the container, in the host storage. This is used to prevent loss of data even if the container is removed or restarted.

The database requires one such volume, which I'll be creating as:

docker volume create oracledb-volume
Create a docker volume

You can use the 'inspect' command to check the configuration in JSON format. Note that the volume is located by default at /var/lib/docker/volumes/.

Configuring docker compose

I have my docker compose scripts within a directory in my system. You can follow a similar setup:

mkdir oracle_database_express
cd oracle_databae_express

Within a file named docker-compose.yml, add the following lines:

# oracle database: express edition container configuration
services:
  oracle_database_express:
    image: container-registry.oracle.com/database/express:latest
    container_name: oracle_database_express
    env_file:
      - ./.env
    volumes:
      - oracledb-volume:/opt/oracle/oradata:rw
    hostname: oracledbhost
    restart: always
    init: true
    tty: true
    ports:
      - 1521:1521

volumes:
  oracledb-volume:
    external: true

Let me explain what each part means:

  • image refers to the docker image that I pulled already
  • container_name will be the name/alias of the container, which I can refer in any operation (rather than the hash)
  • env_file locates a file for the environment variables of the database instance
  • volumes: <volume-name>:/internal/path/in/container maps the volume storage to some internal path, which should be stored permanently (hence a volume is used)
  • init: true, tty: true is the equivalent of running docker run -it <image>
  • restart: always will restart the container at all times, even during errors or a system restart
  • ports: host_port:internal_port allocates a port in the host to the internal port; the database running at port 1521 has to be exposed outside of the container.

Setting up environment variables

You can configure various environment variables relevant to oracle database in a .env file like this:

# environment variables (without spaces)

ORACLE_PWD=mysecurepassword

For instance, the variable ORACLE_PWD sets the password for inbuilt accounts like sys, system etc of the database.

Starting up the database

Once all configurations are complete, you can start the container with the following command (-d starts it in the background):

docker compose up -d
Start the docker container

Running a docker ps gives you the status of the container. If the status is healthy, then the container is ready to be used.

Status of the container

To access your database via the sqlplus command line tool (that's included with the container), you can run a command inside the container with:

docker exec -it <container_name> <command>
Access the database by running commands inside container

Stopping the database

To stop the container from execution, run

docker compose stop

which temporarily stops the container. To start again, you can run

docker compose start

Finally, to completely remove the container (during updating configurations and stuff), you can run

docker compose down

Changing the configuration

One of the main advantages of using docker compose is that the configuration is editable whenever possible.

If the database is still running, terminate it by running

docker compose down

Then, make any changes to the docker-compose.yml file. Here, I am changing the exposed port of the host for demonstration purposes.

Change configuration of database (docker compose)

Once the changes are done, you can once again run

docker compose up -d

Real use case: Using DB via an IDE

Undeniably, VSCode has been a popular text editor. It also provides an Oracle SQL Developer Extension to work with oracle databases.

You can connect to this database by creating a new connection, like this:

Create a new connection in VSCode
Test the configuration and save

You should see a success message as a notification, like below, and then you can save the connection for later usage.

Success message from connection test

You can now use this database to run SQL scripts.

0:00
/0:16

Run SQL Scripts via an IDE

Removing the entire database

Uninstalling this instance is as easy as removing one container.

To remove your current database instance, you should remove the container as well as the persistent volume associated with it.

docker compose down
docker container rm <container_name>
docker volume rm <volume_name>

Finally, since you don't need the DB anymore, you can also remove the Docker image by referencing either the name or the hash.

docker image rm container-registry.oracle.com/database/express:latest

Conclusion

This journey was fueled by looking for a quick installation of oracle database, that does not directly affect the host system.

This tutorial takes you through the final revision of my attempt, that also enables changes in the configuration and the ability to dispose the container once the work is done.

Please let me know in the comments if you face any difficulties with the installation process.