Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Oracle Docker image with custom data

Tags:

docker

oracle

I'm trying to create a Oracle DB Docker image containing some predefined data. The image is to be used in a docker-compose file to automatically run some unit tests of a module connecting to Oracle DB.

I already have a MySQL and a SQL Server image prepared. Running these I've database servers providing some simple, predefined databases and tables.

With MySQL this worked like a charm since the image already provides the /docker-entrypoint-initdb.d directory. The scripts I COPY there are executed on startup and I'm fine.

With SQL Server it was a bit more complex. I'm using /opt/mssql/bin/sqlservr to start the SQL Server while building the image. When the SQL Server is up, a shell script calls /opt/mssql-tools/bin/sqlcmd to run some scripts creating the database and tables I want to provide. Also owrks fine.

With Oracle DB it seems impossible to create an image which already contains some data.

This is what I'm currently trying:

FROM store/oracle/database-enterprise:12.2.0.1-slim

ENV ORACLE_SYS_USER=sys
ENV ORACLE_SYS_PASSWORD=Oradoc_db1
ENV ORACLE_PDB=ORCLPDB1
ENV ORACLE_DATABASE=test
ENV ORACLE_USER=test
ENV ORACLE_PASSWORD=test

USER root

RUN mkdir -p /usr/local/src/oracle

WORKDIR /usr/local/src/oracle

COPY ./docker/wait-for-it.sh /usr/local/src/oracle
COPY ./docker/oracle/start-import.sh /usr/local/src/oracle
COPY ./docker/oracle/import-data.sh /usr/local/src/oracle
COPY ./docker/oracle/create-database.sql /usr/local/src/oracle
COPY ./docker/oracle/testdata.sql /usr/local/src/oracle

RUN chmod +x /usr/local/src/oracle/wait-for-it.sh
RUN chmod +x /usr/local/src/oracle/prepare-import.sh
RUN chmod +x /usr/local/src/oracle/start-import.sh
RUN chmod +x /usr/local/src/oracle/import-data.sh

RUN chown -R oracle:dba /usr/local/src/oracle

USER oracle

RUN ./start-import.sh

ENTRYPOINT /home/oracle/setup/dockerInit.sh

start-import.sh:

#!/bin/sh

# Start the server. From its source it seems that Oracle prefers bash.
/bin/bash /home/oracle/setup/dockerInit.sh &

./wait-for-it.sh "localhost:1521" -t 300 -- ./import-data.sh

kill %1
wait

import-data.sh:

#!/bin/bash

/u01/app/oracle/product/12.2.0/dbhome_1/bin/sqlplus -s $ORACLE_SYS_USER/$ORACLE_SYS_PASSWORD AS SYSDBA << SQL
@/usr/local/src/oracle/create-database.sql
exit;
SQL

/u01/app/oracle/product/12.2.0/dbhome_1/bin/sqlplus -s $ORACLE_USER/$ORACLE_PASSWORD << SQL
@/usr/local/src/oracle/testdata.sql
exit;
SQL

I'm skipping the two SQL files here. Running them inside a plain Oracle DB Docker image runs fine. And regarding the call of sqlplus, I already tried some other calls like sqlplus ... @script.sql, but none worked.

Reading the scripts I would expect that during the image creation the Oracle DB server is started and once it's up, the sqlplus calls create the database structure I would like to have. But instead I get either

  • TNS errors, if I include the host or SID in the connect string, or
  • EOF errors (ORA-03113) in the above case.
Done ! The database is ready for use .
# ===========================================================================  
# == Add below entries to your tnsnames.ora to access this database server ==  
# ====================== from external host =================================  
ORCLCDB=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<ip-address>)(PORT=<port>))
    (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCLCDB.localdomain)))     
ORCLPDB1=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<ip-address>)(PORT=<port>))
    (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCLPDB1.localdomain)))     
#                                                                              
#ip-address : IP address of the host where the container is running.           
#port       : Host Port that is mapped to the port 1521 of the container.      
#                                                                              
# The mapped port can be obtained from running "docker port <container-id>"  
# ===========================================================================  
ORCLPDB1(3):Database Characterset for ORCLPDB1 is WE8DEC
ORCLPDB1(3):Opatch validation is skipped for PDB ORCLPDB1 (con_id=0)
2019-04-15T12:44:22.591697+00:00
ORCLPDB1(3):Opening pdb with no Resource Manager plan active
Pluggable database ORCLPDB1 opened read write
Completed:     alter pluggable database ORCLPDB1 open
    alter pluggable database all save state
Completed:     alter pluggable database all save state
2019-04-15T12:44:22.844748+00:00
ALTER SYSTEM SET encrypt_new_tablespaces='DDL' SCOPE=BOTH;
wait-for-it.sh: localhost:1521 is available after 99 seconds
ERROR:
ORA-03113: end-of-file on communication channel
Process ID: 0
Session ID: 0 Serial number: 0


SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM|SYSBACKUP|SYSDG|SYSKM|SYSRAC}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM|SYSBACKUP|SYSDG|SYSKM|SYSRAC}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
ERROR:
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3701
Additional information: 1724833639

Would be great if someone had an idea. And yes, I'm going to try to run the slim image using a volume. Then create the tables and use the resulting volume in my docker-compose environment.

like image 784
Newlukai Avatar asked Apr 15 '19 13:04

Newlukai


1 Answers

Didn't get that approach to work. I decided to run a official image, modify the database and create an own image out of that modified container.

UPDATE: The official Dockerfiles are available on GitHub, I used the one for a single instance DB installation. Well, Dockerfile might be some kind of misleading. You will get a Dockerfile bundled with some scripts. Once you downloaded an official setup file from the Oracle (these seem to be correct) and put it in the right place, you can

  • start to create a Docker image (see README on linked GitHub page)
  • run your newly created image (docker run --name ora -e ORACLE_PWD=<yourpw> <image name/id, e. g. oracle/database:18.4.0-xe>)
  • use "sqlplus" in your container to create some data
    • docker exec -it ora bash
    • bash-4.2# sqlplus sys/<yourpw>@localhost:1521/XEPDB1 as sysdba
  • exit "sqlplus" and the "bash"
  • stop the container (docker stop ora)
  • commit the changes (docker commit -m "Oracle XE + custom changes" -a "<Your name>" ora <registry>/<nameForTheChangedImage>)
like image 194
Newlukai Avatar answered Oct 11 '22 17:10

Newlukai