FB

In this guide, you’ll learn:

  • What Docker is (briefly)
  • Why you might want to use it for your database
  • How to set it up
  • How to get an Oracle database running using docker – with screenshots and explanations

Let’s get started!

What is Docker and Why Use it for a Database?

Docker is a tool that allows you to run applications in their own areas of an operating system (inside a “container”).

Using Docker is common for application development teams to run the applications in, from what I’ve seen. But running databases inside them is not as common.

It’s a similar concept to a virtual machine. For our purposes, for running the database, we just need to know how to set up the database inside the Docker container and how to connect to it from outside the Docker container.

 

Why Use Docker for a Database?

Why would we use Docker for our database? There are several reasons.

  • Fast setup. It’s pretty fast to set up a new database using this Docker process, compared to installing a database on your operating system.
  • Different versions. It’s easier to have different versions of the same database running in different containers (e.g. Oracle 12c and 18c) if you need to test different features.
  • Runs on more operating systems. Oracle only runs on Windows and Linux, so if you’re using a Mac, you’ll have to use a virtual machine. Or you can use Docker.

There are probably many more advantages related to deployments and automation which I won’t cover here.

The other thing you should know is that the data in the Oracle database is not persisted (saved permanently). Unlike a Virtual Machine, containers can be stopped and started without the data being saved. So, be aware of this when working with your database.

It’s possible to persist data, and it’s possible that certain settings or commands here can show the data is persisted and I may be wrong, but it’s something to keep in mind.

Let’s get started with setting up the database.

 

High-Level Steps

At a high level, here are the steps we’ll take in this guide to get an Oracle database up and running.:

  1. Download the Docker application.
  2. Set up an account on Docker Hub
  3. Run Docker and download the Oracle image
  4. Run the Oracle image
  5. Connect to the database and run some SQL

The screenshots in this guide are taken on a Mac, but the same process should be possible on Windows too.

Video

If you’d like to watch a video of this process, check it out here:

 

Download Docker

To get started, we need to download the Docker application. This will allow us to run the container which contains the database.

Step 1: Visit the Docker website at www.docker.com.

Step 2: Click on Get Started.

docker homepage

Step 3: Select the right version for your computer. This would be either of the two Mac versions or a Windows version.

docker versions

The Docker setup file will then begin downloading to your computer. It’s about 580 MB so it may take a few minutes to download.

Step 4: Install the Docker application once it has downloaded. This can be done by opening the file on macOS and dragging it to the Applications folder, or following the installation steps in Windows.

It takes a couple of minutes to install.

Step 5: Run Docker. This will cause the Docker service to start.

You can run this from the Applications folder, or from the Spotlight Search, or from the terminal.

docker spotlight

Now we’ve got Docker installed and runnings, it’s time to get our database files.

 

Set Up Account on Docker Hub

The files that Docker uses to run applications are called images. An image is a pre-built collection of files.

These images are available to download on a website called Docker Hub. There is a whole range of images there for different technologies. The one we’re looking for is for the Oracle database.

We’ll need to visit the Docker Hub website, set up an account, find the image, and download it.

Step 1: Visit the Docker Hub website at hub.docker.com.

docker hub 1

Step 2: Click on the Sign Up button. You’ll need an account to be able to download the images, but it’s free and easy to set up.

docker hub 2

Step 3: Enter your details on the Sign Up page.

Remember your password here as you’ll need it later.

Step 4: On the Choose Your Plan page, select Free. This is enough for our purposes.

docker hub 3

Step 5: Verify your email address by clicking the link in the email you receive.

You should end up at the Docker Hub homepage and should be logged in.

It’s time to find the image for the Oracle database. We’re going to get the URL for the image, and use this URL as part of a command we’ll run on the command line.

Step 6: Enter the term “oracle database” into the search bar at the top of the page.

oracle docker search

We enter this because there is a range of Oracle products, such as WebLogic and Fusion MIddleware, and we only want the database.

Step 7: Click on the Oracle Database Enterprise Edition in the search results.

oracle docker results

This page shows the details of the Oracle database image.

oracle docker page

At the moment, the only database version here is 12c. It’s an old version, but I understand Oracle is working on making more recent versions available as Docker images for the developer community.

Fortunately, if you’re getting started with Oracle, the basic features are pretty much the same in recent versions.

Step 8: Click Proceed to Checkout.

Step 9: Accept the Terms and Conditions in order to use the image, then click Get Content.

docker oracle terms

You’ll be presented with a panel that shows a command in the black box on the right. It says “Copy and paste to pull this image”.

oracle docker command

It shows a command of “docker pull” then a URL. We’ll use this to download the image shortly.

 

Download Oracle Image

We have an account on Docker Hub and have found the Oracle Database image to download.

The next steps are to download the image. We’ll do this on the command line.

Step 1: Open the Terminal (macOS) or Command Line (Windows).

macos terminal

Step 2: Enter “docker login” at the command line and press Enter.

MacBook-Pro:~ BB$ docker login
Authenticating with existing credentials
Login Succeeded

We need to login to Docker before downloading the image, and this is the command to do it.

Step 3: Enter your username and password, which is what you entered when you set up your Docker Hub account.

Step 4: Enter the docker pull command.

The docker pull command we saw earlier (in the black box on Docker Hub) can be used to download the Oracle database. This will download the full 12c version of Oracle.

docker pull store/oracle/database-enterprise:12.2.0.1

 

Before you run that, there’s another version that you may want. The version is called “slim”. It has a smaller file size and faster startup time, but does not come with some features such as Application Express and Analytics.

If you only want the database (which we do for this tutorial) you can use the Slim version.

So, add two dashes and the word “slim” to the end of the image name, without a space.

Here’s the command:

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

 

Step 5: Run this command.

MacBook-Pro:~ BB$ docker pull store/oracle/database-enterprise:12.2.0.1--slim
12.2.0.1-sli: Pulling from store/oracle/database-enterprise
Digest: sha256:dbd87...
Status: Image is up to date for store/oracle/database-enterprise:12.2.0.1--slim
docker.io/store/oracle/database-enterprise:12.2.0.1-slim

The image will then start downloading. It’s a few gigabytes so it may take some time.

At this point, you may get this error:

Error response from daemon: pull access denied for store/oracle/database-enterprise, repository does not exist or may require ‘docker login’: denied: requested access to the resource is denied

To resolve this error, you’ll need to log in to your Docker Hub account and accept the agreement for the Oracle Database.

After the image is downloaded, it’s time to run it.

 

Run Oracle Image

To start our Oracle database, we need to run the image.

Step 1: Run this docker run command at the command line.

docker run -d -p 1521:1521 –name oracle store/oracle/database-enterprise:12.2.0.1–slim

 

This command contains a few things:

  • docker run is the command to run a container that has been downloaded.
  • -d stands for “detached”, which means the container runs the background. Without this, the container and any output will take over the use of the terminal window, and you’ll have to open a new terminal to run more commands.
  • The “-p 1521:1521” stands for port, which goes with both these numbers. One number is the port outside the container, and the other is the port inside the container. We’ll use port 1521 for both.
  • The –name allows us to name our container. We’ve called our “oracle”. We can refer to this container in other commands by this name.
  • Finally, we specify the path to the image which we just downloaded.

Once you run the command, the image will start. This can take a minute or two.

MacBook-Pro:~ BB$ docker run -d -p 1521:1521 --name oracle
store/oracle/database-enterprise:12.2.0.1--slim

If you get an error about the name already being used, then it’s likely because you have run this command in the past and the container name of “oracle” is already created. If so, you can start the existing container. To do this, type “docker start oracle”.

Step 2: Check the status by running docker ps.

You can check the status of your Docker containers by running docker ps.

This shows a list of all containers that are running.

MacBook-Pro:~ BB$ docker ps
CONTAINER ID   IMAGE                                            COMMAND                  CREATED   
STATUS                            PORTS                              NAMES
d29364816b73   store/oracle/database-enterprise:12.2.0.1--slim  "/bin/sh -c '/bin/..."   3 days ago  
Up 7 seconds (health: starting)   0.0.0.0:1521->1521/tcp, 5500/tcp   oracle

In the Status column, you’ll see something like “starting” that indicates the container is starting up.

Run the command in a minute or two and you should see a status of “healthy”. This means the container is running.

We now have a Docker container running with our Oracle database!

 

Connect to Database and Run SQL

Now the container is running, we can connect to it.

Many tutorials online specify you can login to the container itself using the command line. But we want to use an IDE on our computer to connect, as it’s easier to do and easier to work with.

We’ll use SQL Developer in this example, but you can use any IDE you like that can connect to Oracle (see the full list here).

Step 1: Open SQL Developer.

Step 2: Click Create Connection. You’ll see this New Connection window here.

macos terminal

Step 3: Enter the connection details.

These details are available on the Docker Hub page for the Oracle Database, but there are a few things missing there that you’ll need to know.

  • Host: localhost
  • Service Name: ORCLPDB1.localdomain
  • User: SYS
  • Role: AS SYSDBA
  • Password: Oradoc_db1

If you want to connect to the Container Database instead (which is the overall Oracle database, nothing to do with the Docker container), then:

  • Select SID instead of Service Name
  • Enter ORCLCDB for the SID

Step 4: Click Test Connection. This should be successful.

If you get an error, take a look at this article for some common connection issues and solutions.

Step 5: Click Save to save your connection.

Step 6: On the new SQL Worksheet tab that appears, enter a simple SQL query to test.

sql dev docker 07

For example:

SELECT SYSDATE
FROM dual;

Step 7: Run the query and see the results.

You should see the results of the query at the bottom of the screen.

Congratulations! You’ve successfully set up an Oracle database inside a Docker container and connected to it!

If you have any errors, let me know in the comments below.

 

Stopping the Database

If you want to stop the database, you can use the docker stop command.

Step 1: Return to the terminal.

Step 2: Run docker stop oracle.

docker stop oracle

 

This command will stop the container called Oracle.

To start it again, use the docker run command we saw earlier.

 

Conclusion

Using Docker is an easy way to get a database up and running on your computer. This can be faster than downloading and installing the regular database using the wizard and can be run on macOS, unlike the standard Oracle database.

Share via
Copy link
Powered by Social Snap