Monday, August 21, 2017

Running Oracle in a Docker Container - Part 3

OK, my apologies - I have 2 blog posts on this and nowhere does it get to the actual point of running Oracle in a Docker Container. I got caught up with real work and although I have done it and documented it, I haven't got around to posting here until now.

So, this is what you've been waiting for!

Using a container with an Oracle database

A search for 'oracle' shows quite a few entries, including a few that have databases already installed:

I downloaded the 11g one to test:

I started it up (ignore the different sessions)

but there were some issues – vi wasn’t installed so I couldn’t edit files, plus the listener.ora pointed to a hostname that changes every time the container is restarted. 

Install vi:

I tried to force the hostname by editing /etc/hostname and /etc/hosts but it didn’t work, but I noticed that every time the container restarted it added an entry –, so I put that in the listener.ora and it worked:

I then tried starting the database:

It started fine, but was pretty slow. The Mac Docker app can change how much memory can be allocated, so I stopped the container, changed it, and restarted it.

But before that, I committed the container so that the changes would be saved:

I also uploaded it to my repository:

The upload took a while and timed out a few times, I had to retry – but it knows where it got to so continued.

So there, at last, you have Oracle XE running in a Docker container!

Connecting to the database with SQLDeveloper

A connection with SQL Developer is easy – but you need to restart the container and map a port:

Start the listener and database:

Then start SQLDeveloper and use these connection details (I created an ‘andy’ user in the database):

Even though the IP address in the container is, you need to use here.

You can then connect.

REMEMBER: Any database updates will be lost unless you commit the changes in the container.

Running MySQL and Postgres in Docker

Yes, I know this is an Oracle DBA blog, but we have to keep up and learn new things.

With this in mind, I ran up MySQL and Postgres databases in Docker containers. It took about 30 minutes or so.

This assumes you have downloaded and installed Docker.

Start a Powershell session and type:

docker pull mysql

Once downloaded (only takes a few minutes), create the container by typing this on one line.

docker run --name andy-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=a_strong-password -d mysql:latest

So that is a MySQL database running in a Docker container.

Download MySQL Workbench (you will also need to install some prerequisites – just follow the instructions.

Start MySQL Workbench and select ‘Database / Connect to database’

Click on “OK”

It should prompt for the password:

Click “OK”

It will open a query window – click on “Server Status” to see the server info

This is in the right-hand pane:

There is also a Performance Dashboard

So that's it for MySQL.

Some useful Docker commands -

Stop (kill) any running containers:

docker kill $(docker ps -a -q)

Remove stopped containers:

docker rm $(docker ps -a -q)

Note that Docker containers are not persistent - any changes like adding users, tables or loading data will be lost when the container is stopped. You need to use the Docker 'COMMIT' command:

The process is the same for Postgres:

Open a Powershell session and type

docker pull postgres

Once downloaded, start the container by typing this on one line:

docker run --name andypg -e POSTGRES_PASSWORD=a_strong-password -d -p 5432:5432 postgres

Download the latest version of PgAdmin.

Start pgadmin and connect to Postgres:

Right-click on “Servers”, then”Create / Server”

Enter the name:

Click on the “Connection” tab, enter this information:

Click on “Save”.

Should appear in the list:

Click on it, stats appear in the right-hand pane:

That's it for Postgres. All very quick and easy, so get learning.