Getting started with Oracle Database running on Docker

In this post I will guide you through the steps necessary to run an Oracle Database on Docker. I will also guide through the steps to install the HR sample schema and access the database from outside the container.

Install Docker Desktop

If you have not done so already install Docker Desktop At the time of writing I was using version 4.22.1 (118664). Once successfully installed, start Docker Desktop.

Download and installation of the Oracle Database Image

From the command line run the following command

docker pull container-registry.oracle.com/database/free:latest

This should take a few minutes when using a good connection. I have noticed occasionally an error is seen, if this happens the easiest solution I found was to run the command again.

Once downloaded run the following command:

docker run -d -p 1521:1521 --name <<NAME OF CONTAINER>> container-registry.oracle.com/database/free:latest

Change <<NAME OF CONTAINER>> to a more meaningful name. As example I have named my container oracle so the command became

docker run -d -p 1521:1521 --name oracle container-registry.oracle.com/database/free:latest

The other thing to note is the use of the -p argument followed by 1521:1521 This exposes the port 1521 and allows tools such as SQL Developer running outside the container to connect to the Oracle Database which is running within the container. If you are not planning to connect from outside the container you can omit the argument and the values.

If you now switch back to Docker Desktop and select the Containers icon you should see something similar to:

You can continue to interact with Docker via the command line, starting and stopping the container using commands docker start <<NAME OF CONTAINER>> and docker stop <<NAME OF CONTAINER>> but I find it just as intuitive using the controls available via Docker Desktop.

Installation of the sample schemas

The sample database schemas provided by Oracle can be downloaded from GitHub. In this section the HR schema will be installed. The same steps can then be reused to install the other sample schemas.

Once the files have been downloaded and unzipped, use the command line copy the following files

docker cp <<PATH TO FILE>>hr_code.sql oracle:/home/oracle
docker cp <<PATH TO FILE>>hr_create.sql oracle:/home/oracle
docker cp <<PATH TO FILE>>hr_install.sql oracle:/home/oracle
docker cp <<PATH TO FILE>>hr_populate.sql oracle:/home/oracle 

Inside the Docker Terminal run the following command:

sqlplus / as sysdba

The screenshot below shows the Docker Terminal and the results of the above command having been run

The next step is to change from the container database to the pluggable database using the the following command. If you want to know more about Container and Pluggable Databases I recommend this article by Tim Hall.

ALTER SESSION SET CONTAINER=FREEPDB1;

At the time of writing the name of the pluggable database that shipped with the image was named FREEPDB1, there are many ways to find the name, one of which is to run this query

SELECT name, pdb
FROM   v$services
ORDER BY name;

Now the HR schema can be installed using the command

@hr_install.sql

Follow the on screen prompts to add a password, the tablespace to be used (the default USERS is fine) Once successfully installed the hr_*.sql scripts that were copied to the Oracle directory earlier are no longer required and can be deleted.

Connecting as the HR user within the Container

Inside the Docker Terminal run the following, entering the password when prompted.

sqlplus hr@freepdb1 

Connecting as the HR user from outside the Container – SQL Developer

Start SQL Developer and create a new Database Connection using the following details

Name:A name that makes sense to you
Authentication TypeDefault
Usernamehr
PasswordThe HR users password
Connection TypeBasic
Hostnamelocalhost
Port1521
SIDleave blank
Service namefreepdb1

Test and save the connection.

Connecting as the HR user from outside the Container – VS Code

To use VS Code to interact with the Oracle Database you will first need the install the Oracle Developer Tools for VS Code (SQL and PLSQL) extension.

Once installed you can create a new connection using the following details

Connection TypeLeave as default of Basic
Database host namelocalhost
Port Number1521
Service Namefreepdb1
RoleDefault
User nameHR
PasswordPassword for the HR user
Connection NameA name that makes sense to you

These details are shown in the screen shot below

Where to go from here

By this point you have an Oracle Database running in a Docker Container which has one or more sample schemas installed that you can interact with using tools from outside the Container. If you are interested in finding out more or want a deep dive into some of the options that have not been touched on in this post the Oracle Container Registry is a good place to start.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.