Creating an Oracle Database Docker image
By: Gerald Venzl
Tags: Oracle Database, Docker

Building the Oracle Database Docker image

Now that Docker is up and running I can start building the image. First I need to get the Docker build files and the Oracle install binaries, both are easy to obtain as shown below. Note that I use the oracle Linux user for all the following steps, which I have enabled previously to communicate with the Docker daemon:

Obtaining the required files

Github build files

First I have to download the Docker build files. There are various ways to do this. I can for example clone the Git repository directly. But for simplicity and for the people who aren’t familiar with git I will just use the download option on Github itself. If you go to the main repository URL https://github.com/oracle/docker-images/ you will see a green button saying “ Clone or download ” and by clicking on it you will have the option “ Download ZIP “. Alternatively you can also just download the repository directly via the static URL: https://github.com/oracle/docker-images/archive/master.zip

[oracle@localhost ~]$ wget https://github.com/oracle/docker-images/archive/master.zip --2017-08-20 14:31:32-- https://github.com/oracle/docker-images/archive/master.zip Resolving github.com (github.com)... 192.30.255.113, 192.30.255.112 Connecting to github.com (github.com)|192.30.255.113|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://codeload.github.com/oracle/docker-images/zip/master [following] --2017-08-20 14:31:33-- https://codeload.github.com/oracle/docker-images/zip/master Resolving codeload.github.com (codeload.github.com)... 192.30.255.120, 192.30.255.121 Connecting to codeload.github.com (codeload.github.com)|192.30.255.120|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/zip] Saving to: ‘master.zip’ [ ] 4,411,616 3.37MB/s in 1.2s 2017-08-20 14:31:34 (3.37 MB/s) - ‘master.zip’ saved [4411616] [oracle@localhost ~]$ unzip master.zip Archive: master.zip 21041a743e4b0a910b0e51e17793bb7b0b18efef creating: docker-images-master/ extracting: docker-images-master/.gitattributes inflating: docker-images-master/.gitignore inflating: docker-images-master/.gitmodules inflating: docker-images-master/CODEOWNERS inflating: docker-images-master/CONTRIBUTING.md ... ... ... creating: docker-images-master/OracleDatabase/ extracting: docker-images-master/OracleDatabase/.gitignore inflating: docker-images-master/OracleDatabase/COPYRIGHT inflating: docker-images-master/OracleDatabase/LICENSE inflating: docker-images-master/OracleDatabase/README.md creating: docker-images-master/OracleDatabase/dockerfiles/ ... ... ... inflating: docker-images-master/README.md [oracle@localhost ~]$

Oracle installation binaries

For the Oracle binaries just download them from where you usually would download them. Oracle Technology Network is probably the place that most people go to. Once you have downloaded them you can proceed with building the image:

[oracle@localhost ~]$ ls -al *database*zip -rw-r--r--. 1 oracle oracle 1354301440 Aug 20 14:40 linuxx64_12201_database.zip

Building the image

Now that I have all the files it’s time to build the Docker image. You will find a separate README.md in the docker-images-master/OracleDatabase/SingleInstance directory which explains the build process in more details. Make sure that you always read that file as it will always reflect the latest changes in the build files! You will also find a buildDockerImage.sh shell script in the docker-images-master/OracleDatabase/SingleInstance/dockerfiles directory that does the legwork of the build for you. For the build it is essential that I copy the install files into the correct version directory. As I’m going to create an Oracle Database 12.2.0.1 image I need to copy the install zip file into docker-images-master/OracleDatabase/SingleInstance/dockerfiles/12.2.0.1 :

[oracle@localhost ~]$ cd docker-images-master/OracleDatabase/SingleInstance/dockerfiles/12.2.0.1/ [oracle@localhost 12.2.0.1]$ cp ~/linuxx64_12201_database.zip . [oracle@localhost 12.2.0.1]$ ls -al total 3372832 drwxrwxr-x. 2 oracle oracle 4096 Aug 20 14:44 . drwxrwxr-x. 5 oracle oracle 77 Aug 19 00:35 .. -rwxr-xr-x. 1 oracle oracle 1259 Aug 19 00:35 checkDBStatus.sh -rwxr-xr-x. 1 oracle oracle 909 Aug 19 00:35 checkSpace.sh -rw-rw-r--. 1 oracle oracle 62 Aug 19 00:35 Checksum.ee -rw-rw-r--. 1 oracle oracle 62 Aug 19 00:35 Checksum.se2 -rwxr-xr-x. 1 oracle oracle 2964 Aug 19 00:35 createDB.sh -rw-rw-r--. 1 oracle oracle 9203 Aug 19 00:35 dbca.rsp.tmpl -rw-rw-r--. 1 oracle oracle 6878 Aug 19 00:35 db_inst.rsp -rw-rw-r--. 1 oracle oracle 2550 Aug 19 00:35 Dockerfile.ee -rw-rw-r--. 1 oracle oracle 2552 Aug 19 00:35 Dockerfile.se2 -rwxr-xr-x. 1 oracle oracle 2261 Aug 19 00:35 installDBBinaries.sh -rw-r--r--. 1 oracle oracle 3453696911 Aug 20 14:45 linuxx64_12201_database.zip -rwxr-xr-x. 1 oracle oracle 6151 Aug 19 00:35 runOracle.sh -rwxr-xr-x. 1 oracle oracle 1026 Aug 19 00:35 runUserScripts.sh -rwxr-xr-x. 1 oracle oracle 769 Aug 19 00:35 setPassword.sh -rwxr-xr-x. 1 oracle oracle 879 Aug 19 00:35 setupLinuxEnv.sh -rwxr-xr-x. 1 oracle oracle 689 Aug 19 00:35 startDB.sh [oracle@localhost 12.2.0.1]$

Now that the zip file is in place I am ready to invoke the buildDockerImage.sh shell script in the dockerfiles folder. The script takes a couple of parameters, -v for the version and -e for telling it that I want Enterprise Edition. Note: The build of the image will pull the Oracle Linux slim base image and execute a yum install as well as a yum upgrade inside the container. For it to success to have to have internet connectivity:

[oracle@localhost 12.2.0.1]$ cd .. [oracle@localhost dockerfiles]$ ./buildDockerImage.sh -v 12.2.0.1 -e Checking if required packages are present and valid... linuxx64_12201_database.zip: OK ========================== DOCKER info: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 17.03.1-ce Storage Driver: devicemapper Pool Name: docker-249:0-202132724-pool Pool Blocksize: 65.54 kB Base Device Size: 26.84 GB Backing Filesystem: xfs Data file: /dev/loop0 Metadata file: /dev/loop1 Data Space Used: 14.42 MB Data Space Total: 107.4 GB Data Space Available: 47.98 GB Metadata Space Used: 581.6 kB Metadata Space Total: 2.147 GB Metadata Space Available: 2.147 GB Thin Pool Minimum Free Space: 10.74 GB Udev Sync Supported: true Deferred Removal Enabled: false Deferred Deletion Enabled: false Deferred Deleted Device Count: 0 Data loop file: /var/lib/docker/devicemapper/devicemapper/data WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device. Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata Library Version: 1.02.135-RHEL7 (2016-11-16) Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe init version: 949e6fa Security Options: seccomp Profile: default selinux Kernel Version: 4.1.12-94.3.8.el7uek.x86_64 Operating System: Oracle Linux Server 7.3 OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 7.795 GiB Name: localhost.localdomain ID: D7CR:3DGV:QUGO:X7EB:AVX3:DWWW:RJIA:QVVT:I2YR:KJXV:ALR4:WLBV Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false ========================== Building image 'oracle/database:12.2.0.1-ee' ... Sending build context to Docker daemon 3.454 GB Step 1/16 : FROM oraclelinux:7-slim 7-slim: Pulling from library/oraclelinux 3152c71f8d80: Pull complete Digest: sha256:e464042b724d41350fb3ac2c2f84bd9d28d98302c9ebe66048a5367682e5fad2 Status: Downloaded newer image for oraclelinux:7-slim ---> c0feb50f7527 Step 2/16 : MAINTAINER Gerald Venzl ---> Running in e442cae35367 ---> 08f875cea39d ... ... ... Step 15/16 : EXPOSE 1521 5500 ---> Running in 4476c1c236e1 ---> d01d39e39920 Removing intermediate container 4476c1c236e1 Step 16/16 : CMD exec $ORACLE_BASE/$RUN_FILE ---> Running in 8757674cc3d5 ---> 98129834d5ad Removing intermediate container 8757674cc3d5 Successfully built 98129834d5ad Oracle Database Docker Image for 'ee' version 12.2.0.1 is ready to be extended: --> oracle/database:12.2.0.1-ee Build completed in 802 seconds.

Starting and connecting to the Oracle Database inside a Docker container

Once the build was successful I can now start and run the Oracle Database inside a Docker container. All I have to do is to issue the docker run command and pass in the appropriate parameters. One important parameter is the -p for the mapping of ports inside the container to the outside world. This is required so that I can also connect to the database from outside the Docker container. Another important parameter is the -v parameter which allows me to keep the data files of the database in a location outside the Docker container. This is important as it will allow me to preserve my data even when the container is thrown away. You should always use the -v parameter or create a named Docker volume! The last useful parameter that I’m going to use is the --name parameter which specifies the name of the Docker container itself. If omitted a random name will be generated. However, passing on a name will allow me to refer to the container via that name later on:

[oracle@localhost dockerfiles]$ cd ~ [oracle@localhost ~]$ mkdir oradata [oracle@localhost ~]$ chmod a+w oradata [oracle@localhost ~]$ docker run --name oracle-ee -p 1521:1521 -v /home/oracle/oradata:/opt/oracle/oradata oracle/database:12.2.0.1-ee ORACLE PASSWORD FOR SYS, SYSTEM AND PDBADMIN: 3y4RL1K7org=1 LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 20-AUG-2017 19:07:55 Copyright (c) 1991, 2016, Oracle. All rights reserved. Starting /opt/oracle/product/12.2.0.1/dbhome_1/bin/tnslsnr: please wait... TNSLSNR for Linux: Version 12.2.0.1.0 - Production System parameter file is /opt/oracle/product/12.2.0.1/dbhome_1/network/admin/listener.ora Log messages written to /opt/oracle/diag/tnslsnr/e3d1a2314421/listener/alert/log.xml Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1))) Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.0.0)(PORT=1521))) Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1))) STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 12.2.0.1.0 - Production Start Date 20-AUG-2017 19:07:56 Uptime 0 days 0 hr. 0 min. 0 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /opt/oracle/product/12.2.0.1/dbhome_1/network/admin/listener.ora Listener Log File /opt/oracle/diag/tnslsnr/e3d1a2314421/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1))) (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.0.0)(PORT=1521))) The listener supports no services The command completed successfully [WARNING] [DBT-10102] The listener configuration is not selected for the database. EM DB Express URL will not be accessible. CAUSE: The database should be registered with a listener in order to access the EM DB Express URL. ACTION: Select a listener to be registered or created with the database. Copying database files 1% complete 13% complete 25% complete Creating and starting Oracle instance 26% complete 30% complete 31% complete 35% complete 38% complete 39% complete 41% complete Completing Database Creation 42% complete 43% complete 44% complete 46% complete 47% complete 50% complete Creating Pluggable Databases 55% complete 75% complete Executing Post Configuration Actions 100% complete Look at the log file "/opt/oracle/cfgtoollogs/dbca/ORCLCDB/ORCLCDB.log" for further details. SQL*Plus: Release 12.2.0.1.0 Production on Sun Aug 20 19:16:01 2017 Copyright (c) 1982, 2016, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production SQL> System altered. SQL> Pluggable database altered. SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production ######################### DATABASE IS READY TO USE! ######################### The following output is now a tail of the alert.log: Completed: alter pluggable database ORCLPDB1 open 2017-08-20T19:16:01.025829+00:00 ORCLPDB1(3):CREATE SMALLFILE TABLESPACE "USERS" LOGGING DATAFILE '/opt/oracle/oradata/ORCLCDB/ORCLPDB1/users01.dbf' SIZE 5M REUSE AUTOEXTEND ON NEXT 1280K MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ORCLPDB1(3):Completed: CREATE SMALLFILE TABLESPACE "USERS" LOGGING DATAFILE '/opt/oracle/oradata/ORCLCDB/ORCLPDB1/users01.dbf' SIZE 5M REUSE AUTOEXTEND ON NEXT 1280K MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ORCLPDB1(3):ALTER DATABASE DEFAULT TABLESPACE "USERS" ORCLPDB1(3):Completed: ALTER DATABASE DEFAULT TABLESPACE "USERS" 2017-08-20T19:16:01.889003+00:00 ALTER SYSTEM SET control_files='/opt/oracle/oradata/ORCLCDB/control01.ctl' SCOPE=SPFILE; ALTER PLUGGABLE DATABASE ORCLPDB1 SAVE STATE Completed: ALTER PLUGGABLE DATABASE ORCLPDB1 SAVE STATE

On the very first startup of the container a new database is being created. Subsequent startups of the same container or newly created containers pointing to the same volume will just start up the database again. Once the database is created and or started the container will run a tail -f on the Oracle Database alert.log file. This is done for convenience so that issuing a docker logs command will actually print the logs of the database running inside that container. Once the database is created or started up you will see the line DATABASE IS READY TO USE! in the output. After that you can connect to the database.

Resetting the database admin accounts passwords

The startup script also generated a password for the database admin accounts. You can find the password next to the line ORACLE PASSWORD FOR SYS, SYSTEM AND PDBADMIN: in the output. You can either use that password going forward or you can reset it to a password of your choice. The container provides a script called setPassword.sh for resetting the password. In a new shell just execute following command against the running container:

[oracle@localhost ~]$ docker exec oracle-ee ./setPassword.sh LetsDocker The Oracle base remains unchanged with value /opt/oracle SQL*Plus: Release 12.2.0.1.0 Production on Sun Aug 20 19:17:08 2017 Copyright (c) 1982, 2016, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production SQL> User altered. SQL> User altered. SQL> Session altered. SQL> User altered. SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

Connecting to the Oracle Database

Now that the container is running and the port 1521 mapped to the outside world I can connect to the database inside the container:

[oracle@localhost ~]$ sql system/LetsDocker@//localhost:1521/ORCLPDB1 SQLcl: Release 4.2.0 Production on Sun Aug 20 19:56:43 2017 Copyright (c) 1982, 2017, Oracle. All rights reserved. Last Successful login time: Sun Aug 20 2017 12:21:42 -07:00 Connected to: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production SQL> grant connect, resource to gvenzl identified by supersecretpwd; Grant succeeded. SQL> conn gvenzl/supersecretpwd@//localhost:1521/ORCLPDB1 Connected. SQL>

Stopping the Oracle Database Docker container

If you wish to stop the Docker container you can just do so via the docker stop command. All you will have to do is to issue the command and pass on the container name or id. This will trigger the container to issue a shutdown immediate for the database inside the container. By default Docker will only allow 10 seconds for the container to shutdown before killing it. For applications that may be fine but for persistent containers such as the Oracle Database container you may want to give the container a bit more time to shutdown the database appropriately. You can do that via the -t option that allows you to pass on a new timeout in seconds for the container to shutdown successfully. I will give the database 30 seconds to shutdown but it’s important to point out that it doesn’t really matter how long you give the container to shutdown. Once the database is shutdown the container will exit normal. It will not wait all the seconds that you have specified until returning control. So even if you give it 10 minutes (600 seconds) it will still return as soon as the database is shutdown. Just keep that in mind when specifying a timeout for busy database containers:

[oracle@localhost ~]$ docker stop -t 30 oracle-ee oracle-ee

Restarting the Oracle Database Docker container

A stopped container can always be restarted via the docker start command:

[oracle@localhost ~]$ docker start oracle-ee oracle-ee

The docker start command will put the container into background and return control immediately. You can check the status of the container via the docker logs command which should print the same DATABASE IS READY TO USE! line. You will also see that this time the database was just restarted rather than created. Note, a docker logs -f will follow the log output, i.e. keep on printing new lines:

[oracle@localhost ~]$ docker logs oracle-ee ... ... ... SQL*Plus: Release 12.2.0.1.0 Production on Sun Aug 20 19:30:31 2017 Copyright (c) 1982, 2016, Oracle. All rights reserved. Connected to an idle instance. SQL> ORACLE instance started. Total System Global Area 1610612736 bytes Fixed Size 8793304 bytes Variable Size 520094504 bytes Database Buffers 1073741824 bytes Redo Buffers 7983104 bytes Database mounted. Database opened. SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production ######################### DATABASE IS READY TO USE! ######################### The following output is now a tail of the alert.log: ORCLPDB1(3):Undo initialization finished serial:0 start:6800170 end:6800239 diff:69 ms (0.1 seconds) ORCLPDB1(3):Database Characterset for ORCLPDB1 is AL32UTF8 ORCLPDB1(3):Opatch validation is skipped for PDB ORCLPDB1 (con_id=0) ORCLPDB1(3):Opening pdb with no Resource Manager plan active 2017-08-20T19:30:43.703897+00:00 Pluggable database ORCLPDB1 opened read write

Now that the database is up and running again I can connect once more to the database inside:

[oracle@localhost ~]$ sql gvenzl/supersecretpwd@//localhost:1521/ORCLPDB1 SQLcl: Release 4.2.0 Production on Sun Aug 20 20:10:28 2017 Copyright (c) 1982, 2017, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production SQL> select sysdate from dual; SYSDATE --------- 20-AUG-17 SQL> exit Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

Summary

This concludes how to containerize the Oracle Database using Docker. Note that Oracle has also provided build files for other Oracle Database versions and editions. The steps described above are largely the same but you should always refer to the README.md that comes with the build files. In there you will also find more options for how to run your Oracle Database containers.

Github repository: https://github.com/oracle/docker-images/tree/master/OracleDatabase

Original article: https://geraldonit.com/2017/08/21/creating-an-oracle-database-docker-image/