前由于docker没有oracle镜像,所以要自己制作一个,在centos镜像启动容器的基础上,安装好oracle并设置开机自启,然后将容器环境再打包为镜像文件。
安装centos
拉取镜像
拉取centos镜像,或本地导入。这里使用的是centos7.6.1810版本
docker pull centos:centos7.6.1810
启动容器
启动centos容器 交互式后台运行 指定hostname 指定映射端口,注意,这里一定要指定hostname,以便于数据库初始化时配置文件里具名hostname,之后重新打包为镜像使用时,也指定此时的hostname。
docker run -itd -p 1521:1521 --restart=always --privileged=true --name myoracle --hostname oracle01 <imageID> /bin/bash
准备oracle安装环境
修改配置文件,vi /etc/pam.d/su,将第12行 session include system-auth这一行注掉,如果不注掉,会导致后面oracle自动创建用户失败。
#%PAM-1.0auth sufficient pam_rootok.so# Uncomment the following line to implicitly trust users in the "wheel" group.#auth sufficient pam_wheel.so trust use_uid# Uncomment the following line to require a user to be in the "wheel" group.#auth required pam_wheel.so use_uidauth substack system-authauth include postloginaccount sufficient pam_succeed_if.so uid = 0 use_uid quietaccount include system-authpassword include system-auth# session include system-authsession include postloginsession optional pam_xauth.so
修改root用户下配置文件,在文件尾部追加export ORACLE_DOCKER_INSTALL=true,作用是允许oracle安装
su - rootvi .bash_profileexport ORACLE_DOCKER_INSTALL=true
yum安装oracle所需依赖,如果个别依赖安装不成功,可单独下载安装。
yum install -y binutils* compat-libstdc* elfutils-libelf* gcc* glibc* ksh* libaio* libgcc* libstdc* make* sysstat* libXp* glibc-kernheaders ksh binutils compat-libstdc++-33 elfutils-libelf elfutils-libelf-devel gcc gcc-c++ glibc glibc-common glibc-devel libaio libaio-devel libgcc libstdc++ libstdc++-devel make numactl sysstat libXp unixODBC unixODBC-devel net-tools
打包当前基础环境
这一步不是必须操作,依赖安装完成后,先退出容器并停止,将容器打包为一个镜像作为可安装oracle的基础镜像备用,并将镜像保存为tar包。(不考虑centos系统备用的则不需要这一步)
# 将容器打包为镜像docker commit -m "<描述信息>" -a="<作者>" <容器ID> <镜像名称># 保存镜像为tar包docker save -o centos-for-oracle.tar centos-for-oracle:latest
安装数据库
下载上传oracle rpm安装包
下载oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm(下载地址)和oracle-database-ee-19c-1.0-1.x86_64.rpm(下载地址)文件并上传到宿主机某个目录下(例如oraclerpm)。
拷贝安装包到容器内
将上一步骤oraclerpm复制到docker容器根目录,进入容器,查看一下上传的文件
cp oraclerpm <容器ID>:/docker exec -it <容器ID> /bin/bashcd /oraclerpm
安装rpm包
安装oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm和oracle-database-ee-19c-1.0-1.x86_64.rpm,顺序安装。
export ORACLE_DOCKER_INSTALL=truerpm -ivh oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm --force --nodepsrpm -ivh oracle-database-ee-19c-1.0-1.x86_64.rpm --force --nodeps
如果没有执行export ORACLE_DOCKER_INSTALL=true,会提示以下错误。
warning: oracle-database-ee-19c-1.0-1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEYPreparing... ################################# [100%]su: cannot open session: Permission denied[SEVERE] The su command is not configured properly or the oracle user does not have the required privileges to install the Oracle database. If you are running in a Docker environment, ensure to set the environment variable ORACLE_DOCKER_INSTALL=true and try again.error: %pre(oracle-database-ee-19c-1.0-1.x86_64) scriptlet failed, exit status 1error: oracle-database-ee-19c-1.0-1.x86_64: install failed
oracle-database-ee安装过程如下,出现[INFO] Oracle home installed successfully and ready to be configured.To configure a sample Oracle Database you can execute the following service configuration script as root: /etc/init.d/oracledb_ORCLCDB-19c configure内容表示安装完成。
初始化数据库
如上图提示可以默认使用如下文件创建实例ORCLCDB,执行如下命令。
/etc/init.d/oracledb_ORCLCDB-19c configure
初始化数据库配置文件说明
oracledb_ORCLCDB-19c是创建默认数据库OECLDB的可执行文件。数据库实例初始化可执行文件,也有对应其他的配置文件。
执行文件路径:/etc/init.d/oracledb_ORCLCDB-19c
执行文件配置文件路径:/etc/sysconfig/oracledb_ORCLCDB-19c.conf
export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1export ORACLE_VERSION=19cexport ORACLE_SID=ORCLCDBexport TEMPLATE_NAME=General_Purpose.dbcexport CHARSET=AL32UTF8export PDB_NAME=ORCLPDB1export LISTENER_NAME=LISTENERexport NUMBER_OF_PDBS=1export CREATE_AS_CDB=ture# General exports and varsexport PATH=$ORACLE_HOME/bin:$PATHLSNR=$ORACLE_HOME/bin/lsnrctlSQLPLUS=$ORACLE_HOME/bin/sqlplusDBCA=$ORACLE_HOME/bin/dbcaNETCA=$ORACLE_HOME/bin/netcaORACLE_OWNER=oracleRETVAL=0CONFIG_NAME="oracledb_$ORACLE_SID-$ORACLE_VERSION.conf"CONFIGURATION="/etc/sysconfig/$CONFIG_NAME"
#This is a configuration file to setup the Oracle Database.#It is used when running '/etc/init.d/oracledb_ORCLCDB configure'.#Please use this file to modify the default listener port and the#Oracle data location.# LISTENER_PORT: Database listenerLISTENER_PORT=1521# ORACLE_DATA_LOCATION: Database oradata locationORACLE_DATA_LOCATION=/opt/oracle/oradata# EM_EXPRESS_PORT: Oracle EM Express listenerEM_EXPRESS_PORT=5500
PS:如果需要另行创建其他数据库实例,可将两个文件分别复制并根据自己需要修改内容。
初始化完成
初始化过程如下,耗时较多。
Database configuration completed successfully. The passwords were auto generated, you must change them by connecting to the database using ‘sqlplus / as sysdba’ as the oracle user.到此初始化完成且自动启动完成
设置环境变量
su - oraclevi .bash_profile
在配置文件最后追加如下内容,追加完成之后,保存退出,并且刷新配置文件source .bash_profile
export ORACLE_BASE=/opt/oracleexport ORACLE_HOME=/opt/oracle/product/19c/dbhome_1export ORACLE_SID=omsdbexport PATH=$ORACLE_HOME/bin:$PATHexport LD_LIBRARY_PATH=$ORACLE_HOME/libLANG=zh_CN.UTF-8; export LANGNLS_LANG='SIMPLIFIED CHINESE_CHINA.AL32UTF8'; export NLS_LANG
修改启动配置文件dbstart和dbshut
vi $ORACLE_HOME/bin/dbstartvi $ORACLE_HOME/bin/dbshut#修改如下ORACLE_HOME=$1 修改为 ORACLE_HOME=$ORACLE_HOMEORACLE_HOME_LISTNER=$1 修改为 ORACLE_HOME_LISTNER=$ORACLE_HOME
复制ora配置文件
如果/opt/oracle/product/19c/dbhome_1/dbs/路径下没有init.ora配置文件,可复制配置文件init.ora.3620227217到/opt/oracle/product/19c/dbhome_1/dbs/目录
cd $ORACLE_BASE/admin/omsdb/pfilecp init.ora.3620227217 /opt/oracle/product/19c/dbhome_1/dbs/initorcl.ora
启动数据库
su - oraclelsnrctl startdbstart
连接数据库
sqlplus / as sysdba
设置开机自启
修改配置文件/etc/oratab
# 修改配置文件vi /etc/oratab# 将ORCLCDB:/opt/oracle/product/19c/dbhome_1:N N改为YORCLCDB:/opt/oracle/product/19c/dbhome_1:Y
修改配置文件/etc/rc.d/rc.local
vi /etc/rc.d/rc.local# 末尾追加如下内容su - oracle -lc "/opt/oracle/product/19c/dbhome_1/bin/lsnrctl start"su - oracle -lc "/opt/oracle/product/19c/dbhome_1/bin/dbstart"su - oracle -lc "lsnrctl start"su - oracle -lc "dbstart"
修改centos系统启动脚本权限:
chmod +x /etc/rc.d/rc.local
将此容器打包为镜像
打包镜像
将安装了oracle的centos容器打包为镜像作为oracle镜像
docker commit -m "描述信息" -a ="作者名字" <容器ID> <镜像名称:版本号>
启动容器
按照如下命令启动容器,并验证访问数据库
docker run -itd -p 1521:1521 --restart=always --privileged=true --name oracle19c --hostname oracle01 oracle19c /usr/sbin/init
