要给开发人员搭建 Oracle 测试环境,准备基于 Docker 构建,同时建立一个 Docker 私有仓库,谁需要就让他装一个 Docker,才从这私有仓库 pull 镜像即可。

环境如下:
CentOS 7
docker-ce-18.03.1.ce-1.el7.centos.x86_64
Oracle 12c

安装Docker

# step 1: 安装必要的一些系统工具 
yum install -y yum-utils device-mapper-persistent-data lvm2

 # Step 2: 添加软件源信息    
 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
 # Step 3: 更新并安装Docker-CE    
 yum makecache && yum -y install docker-ce
# Step 4: 开启Docker服务     
systemctl start docker
配置加速器请参考阿里云官方文档。
https://yq.aliyun.com/articles/29941

搜索并下载Oracle镜像

列出收藏数大于100的镜像
[root@Docker ~]# docker search -s 100 oracle
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
oraclelinux                      Official Docker builds of Oracle Linux.         451                 [OK]                
frolvlad/alpine-oraclejdk8       The smallest Docker image with OracleJDK 8 (…   303                                     [OK]
sath89/oracle-12c                Oracle Standard Edition 12c Release 1 with d…   296                                     [OK]
alexeiled/docker-oracle-xe-11g   This is a working (hopefully) Oracle XE 11.2…   252                                     [OK]
sath89/oracle-xe-11g             Oracle xe 11g with database files mount supp…   185                                     [OK]

# pull第三个镜像
# docker pull sath89/oracle-12c

运行镜像

#-P, 在容器中任何需要的网络端口都映射到主机。即将Docker镜像中的Oracle 1521端口映射到宿主机。
#-v 把docker 容器中某目录的数据 加载到 宿主机的某个目录。即将docker Oracle镜像的数据存储到/data/db 下
docker run -d -p 8080:8080 -p 1521:1521 -v /data/db/:/u01/app/oracle sath89/oracle-12c
启动速度较慢,我们可以去查看日志
# docker logs -f 032d6b2097e8cf3d4dfc3bf173a229bda0a5c8a8a51434299274c652698964a7
Database not initialized. Initializing database.
Starting tnslsnr
Copying database files
1% complete
3% complete
11% complete
18% complete
26% complete
37% complete
Creating and starting Oracle instance
40% complete
45% complete
50% complete
55% complete
56% complete
60% complete
62% complete
Completing Database Creation
66% complete
70% complete
73% complete
85% complete
96% complete
100% complete
Look at the log file "/u01/app/oracle/cfgtoollogs/dbca/xe/xe.log" for further details.
Configuring Apex console
Database initialized. Please visit http://#containeer:8080/em http://#containeer:8080/apex for extra configuration if needed
Starting web management console

PL/SQL procedure successfully completed.

Starting import from '/docker-entrypoint-initdb.d':
found file /docker-entrypoint-initdb.d//docker-entrypoint-initdb.d/*
[IMPORT] /entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

Import finished

Database ready to use. Enjoy! ;)

查看进程
[root@Docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
3ceb1ae8637e        sath89/oracle-12c   "/entrypoint.sh "   10 seconds ago      Up 4 seconds        0.0.0.0:1521->1521/tcp, 0.0.0.0:8080->8080/tcp   unruffled_kare

配置Oracle

接下来进入镜像的 bash 做进一步操作。
[root@Docker ~]# docker exec -it 3ceb1ae8637e /bin/bash 
root@3ceb1ae8637e:/# 
root@3ceb1ae8637e:/# ss -nlpt
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port 
LISTEN     0      128                       *:42983                    *:*     
LISTEN     0      128                       *:8080                     *:*     
LISTEN     0      128                       *:1521                     *:*     
接下来登陆数据库
# sqlplus system/oracle@//localhost:1521/xe

SQL*Plus: Release 12.1.0.2.0 Production on Fri May 18 09:27:11 2018

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Fri May 18 2018 09:26:42 +00:00

Connected to:
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production

SQL> show parameter service_name

NAME                     TYPE    VALUE
------------------------------------ ----------- ------------------------------
service_names                string  xe
SQL> 

pl/sql 连接 Oracle

下载一个plsql development,还有一个Oracle客户端

新建下面3个环境变量

NLS_LANG
AMERICAN_AMERICA.AL32UTF8

ORACLE_HOME
D:\instantclient_11_2

TNS_ADMIN
d:\instantclient_11_2\network\ADMIN

解压oracle客户端以后,新建  network\ADMIN 文件夹
新建文件 tnsnames.ora

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.153.32)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl.site)
    )
  )