- 基于Linux系统,宿主主机要设置如下环境变量,oracle为64位版本
dockerfile中需要的数据库安装包可从csdn下载内找到
#!/bin/bash
# 在宿主机上运行以设置Oracle所需的内核参数
# 这些命令需要root权限cat > /etc/sysctl.d/99-oracle.conf << EOF
fs.file-max = 6815744
kernel.sem = 250 32000 100 128
kernel.shmmni = 4096
kernel.shmall = 1073741824
kernel.shmmax = 4398046511104
kernel.panic_on_oops = 1
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.default.rp_filter = 2
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
EOF# 应用设置
sysctl -p /etc/sysctl.d/99-oracle.conf# 设置Docker守护程序启动参数,允许容器使用特定的内核功能
cat > /etc/docker/daemon.json << EOF
{"default-ulimits": {"nofile": {"name": "nofile","hard": 65536,"soft": 1024},"memlock": {"name": "memlock","hard": -1,"soft": -1}}
}
EOF# 重启Docker服务以应用设置
systemctl restart docker
- 制作Dockerfile
FROM dockerproxy.net/library/oraclelinux:7-slim# 安装必要的包
RUN yum -y install oracle-rdbms-server-11gR2-preinstall unzip# 设置环境变量
ENV ORACLE_BASE=/u01/app/oracle \ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe \ORACLE_SID=ORCL \ORACLE_DATA=/u01/app/oracle/oradata \PATH=$PATH:/u01/app/oracle/product/11.2.0/xe/bin# 创建必要的目录并设置正确的所有权
# 在创建用户后设置oracle用户密码
RUN set -e \&& mkdir -p $ORACLE_HOME \&& mkdir -p $ORACLE_DATA/ORCL \&& mkdir -p /u01/app/oraInventory \&& groupadd -g 54321 oinstall || true \&& groupadd -g 54322 dba || true \&& useradd -u 54321 -g oinstall -G dba oracle || true \&& echo "oracle:oracle" | chpasswd \&& chown -R oracle:dba /u01 \&& chmod -R 775 /u01 \&& echo "Directory and user setup completed successfully"# 创建响应文件
RUN echo "ORACLE_HTTPS_PORT=8080" > /tmp/xe.rsp && \echo "ORACLE_HTTP_PORT=8090" >> /tmp/xe.rsp && \echo "ORACLE_PASSWORD=oracle" >> /tmp/xe.rsp && \echo "ORACLE_CONFIRM_PASSWORD=oracle" >> /tmp/xe.rsp && \echo "ORACLE_DBENABLE=y" >> /tmp/xe.rsp# 假设您已经下载了Oracle 11g的安装文件并放在当前目录
COPY oracle-xe-11.2.0-1.0.x86_64.rpm.zip /tmp/# 解压安装文件并安装
USER oracle
RUN unzip /tmp/oracle-xe-11.2.0-1.0.x86_64.rpm.zip -d /tmpUSER root
RUN rpm -ivh --nodeps --force /tmp/Disk1/oracle-xe-11.2.0-1.0.x86_64.rpm && \rm -rf /tmp/Disk1 /tmp/oracle-xe-11.2.0-1.0.x86_64.rpm.zip# 配置Oracle
RUN /etc/init.d/oracle-xe configure responseFile=/tmp/xe.rsp || true && \# 确保配置文件存在mkdir -p $ORACLE_HOME/network/admin && \# 强制修改监听器配置sed -i 's/HOST=buildkitsandbox/HOST=0.0.0.0/g' $ORACLE_HOME/network/admin/listener.ora && \# 如果替换失败,直接创建正确的配置echo "LISTENER = \(DESCRIPTION_LIST = \(DESCRIPTION = \(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE)) \(ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521)) \) \) \" > $ORACLE_HOME/network/admin/listener.ora || true# 修改监听器配置,使用0.0.0.0代替默认主机名
RUN sed -i 's/HOST=buildkitsandbox/HOST=0.0.0.0/g' $ORACLE_HOME/network/admin/listener.ora || true# 创建初始化参数文件
USER oracle
RUN echo "db_name=ORCL" > $ORACLE_HOME/dbs/initORCL.ora && \echo "processes=150" >> $ORACLE_HOME/dbs/initORCL.ora && \echo "db_block_size=8192" >> $ORACLE_HOME/dbs/initORCL.ora && \echo "sga_target=800M" >> $ORACLE_HOME/dbs/initORCL.ora && \echo "pga_aggregate_target=200M" >> $ORACLE_HOME/dbs/initORCL.ora && \echo "remote_login_passwordfile=EXCLUSIVE" >> $ORACLE_HOME/dbs/initORCL.ora && \echo "control_files=('/u01/app/oracle/oradata/ORCL/control01.ctl', '/u01/app/oracle/oradata/ORCL/control02.ctl')" >> $ORACLE_HOME/dbs/initORCL.ora# 创建数据库创建脚本
RUN echo "CREATE DATABASE ORCL" > /tmp/create_db.sql && \echo "USER SYS IDENTIFIED BY oracle" >> /tmp/create_db.sql && \echo "USER SYSTEM IDENTIFIED BY oracle" >> /tmp/create_db.sql && \echo "LOGFILE GROUP 1 ('/u01/app/oracle/oradata/ORCL/redo01.log') SIZE 100M," >> /tmp/create_db.sql && \echo " GROUP 2 ('/u01/app/oracle/oradata/ORCL/redo02.log') SIZE 100M," >> /tmp/create_db.sql && \echo " GROUP 3 ('/u01/app/oracle/oradata/ORCL/redo03.log') SIZE 100M" >> /tmp/create_db.sql && \echo "MAXLOGFILES 5" >> /tmp/create_db.sql && \echo "MAXLOGMEMBERS 5" >> /tmp/create_db.sql && \echo "MAXLOGHISTORY 1" >> /tmp/create_db.sql && \echo "MAXDATAFILES 100" >> /tmp/create_db.sql && \echo "CHARACTER SET AL32UTF8" >> /tmp/create_db.sql && \echo "NATIONAL CHARACTER SET AL16UTF16" >> /tmp/create_db.sql && \echo "EXTENT MANAGEMENT LOCAL" >> /tmp/create_db.sql && \echo "DATAFILE '/u01/app/oracle/oradata/ORCL/system01.dbf'" >> /tmp/create_db.sql && \echo " SIZE 325M REUSE AUTOEXTEND ON NEXT 10240K MAXSIZE UNLIMITED" >> /tmp/create_db.sql && \echo "SYSAUX DATAFILE '/u01/app/oracle/oradata/ORCL/sysaux01.dbf'" >> /tmp/create_db.sql && \echo " SIZE 325M REUSE AUTOEXTEND ON NEXT 10240K MAXSIZE UNLIMITED" >> /tmp/create_db.sql && \echo "DEFAULT TABLESPACE users" >> /tmp/create_db.sql && \echo " DATAFILE '/u01/app/oracle/oradata/ORCL/users01.dbf'" >> /tmp/create_db.sql && \echo " SIZE 500M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED" >> /tmp/create_db.sql && \echo "DEFAULT TEMPORARY TABLESPACE temp" >> /tmp/create_db.sql && \echo " TEMPFILE '/u01/app/oracle/oradata/ORCL/temp01.dbf'" >> /tmp/create_db.sql && \echo " SIZE 20M REUSE AUTOEXTEND ON NEXT 640K MAXSIZE UNLIMITED" >> /tmp/create_db.sql && \echo "UNDO TABLESPACE undotbs1" >> /tmp/create_db.sql && \echo " DATAFILE '/u01/app/oracle/oradata/ORCL/undotbs01.dbf'" >> /tmp/create_db.sql && \echo " SIZE 200M REUSE AUTOEXTEND ON NEXT 5120K MAXSIZE UNLIMITED;" >> /tmp/create_db.sql# 创建启动脚本
RUN echo '#!/bin/bash' > /tmp/start_oracle.sh && \echo 'export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe' >> /tmp/start_oracle.sh && \echo 'export ORACLE_SID=ORCL' >> /tmp/start_oracle.sh && \echo 'export PATH=$PATH:$ORACLE_HOME/bin' >> /tmp/start_oracle.sh && \# 添加这行在启动监听器之前修复配置文件echo '# 修复监听器配置' >> /tmp/start_oracle.sh && \echo 'sed -i "s/HOST=buildkitsandbox/HOST=0.0.0.0/g" $ORACLE_HOME/network/admin/listener.ora' >> /tmp/start_oracle.sh && \echo '' >> /tmp/start_oracle.sh && \echo '# 启动监听器' >> /tmp/start_oracle.sh && \echo '$ORACLE_HOME/bin/lsnrctl start' >> /tmp/start_oracle.sh && \echo '' >> /tmp/start_oracle.sh && \echo '# 确认数据库是否已存在' >> /tmp/start_oracle.sh && \echo 'if [ ! -f /u01/app/oracle/oradata/ORCL/system01.dbf ] || [ ! -f /u01/app/oracle/oradata/ORCL/control01.ctl ]; then' >> /tmp/start_oracle.sh && \echo ' echo "数据库文件不存在,开始创建新数据库..."' >> /tmp/start_oracle.sh && \echo ' $ORACLE_HOME/bin/sqlplus / as sysdba << EOF' >> /tmp/start_oracle.sh && \echo ' STARTUP NOMOUNT PFILE=$ORACLE_HOME/dbs/initORCL.ora;' >> /tmp/start_oracle.sh && \echo ' @/tmp/create_db.sql' >> /tmp/start_oracle.sh && \echo ' @?/rdbms/admin/catalog.sql' >> /tmp/start_oracle.sh && \echo ' @?/rdbms/admin/catproc.sql' >> /tmp/start_oracle.sh && \echo ' @?/rdbms/admin/utlrp.sql' >> /tmp/start_oracle.sh && \echo ' ALTER SYSTEM REGISTER;' >> /tmp/start_oracle.sh && \echo ' EXIT;' >> /tmp/start_oracle.sh && \echo 'EOF' >> /tmp/start_oracle.sh && \echo 'else' >> /tmp/start_oracle.sh && \echo ' echo "数据库文件已存在,启动现有数据库..."' >> /tmp/start_oracle.sh && \echo ' $ORACLE_HOME/bin/sqlplus / as sysdba << EOF' >> /tmp/start_oracle.sh && \echo ' STARTUP PFILE=$ORACLE_HOME/dbs/initORCL.ora;' >> /tmp/start_oracle.sh && \echo ' ALTER SYSTEM REGISTER;' >> /tmp/start_oracle.sh && \echo ' EXIT;' >> /tmp/start_oracle.sh && \echo 'EOF' >> /tmp/start_oracle.sh && \echo 'fi' >> /tmp/start_oracle.sh && \echo '' >> /tmp/start_oracle.sh && \# 替换原来的tail命令为改进版本echo '# 保持容器运行' >> /tmp/start_oracle.sh && \echo 'echo "数据库启动完成,监听中..."' >> /tmp/start_oracle.sh && \echo '# 查找正确的警报日志文件' >> /tmp/start_oracle.sh && \echo 'ALERT_LOG=$(find $ORACLE_HOME/diag -name "alert_*.log" 2>/dev/null | head -1)' >> /tmp/start_oracle.sh && \echo 'if [ -f "$ALERT_LOG" ]; then' >> /tmp/start_oracle.sh && \echo ' # 如果找到警报日志,则跟踪它' >> /tmp/start_oracle.sh && \echo ' echo "找到警报日志: $ALERT_LOG"' >> /tmp/start_oracle.sh && \echo ' tail -f $ALERT_LOG' >> /tmp/start_oracle.sh && \echo 'else' >> /tmp/start_oracle.sh && \echo ' # 如果没有找到警报日志,使用简单的睡眠循环保持容器运行' >> /tmp/start_oracle.sh && \echo ' echo "警报日志文件未找到,使用睡眠循环保持容器运行..."' >> /tmp/start_oracle.sh && \echo ' while true; do' >> /tmp/start_oracle.sh && \echo ' sleep 60' >> /tmp/start_oracle.sh && \echo ' done' >> /tmp/start_oracle.sh && \echo 'fi' >> /tmp/start_oracle.shUSER root
RUN cp /tmp/start_oracle.sh / && \chmod +x /start_oracle.sh# 声明持久化卷
VOLUME ["$ORACLE_DATA"]# 健康检查
HEALTHCHECK --interval=60s --timeout=30s --start-period=5m --retries=3 \CMD su - oracle -c "$ORACLE_HOME/bin/sqlplus -s system/oracle@localhost:1521/ORCL <<< \"select 1 from dual;\"" || exit 1EXPOSE 1521 8080
USER oracle
CMD ["/start_oracle.sh"]
说明:
- 系统oracle账户密码为:oracle
- 创建默认数据库普通账户
- 账户名system
- 密码 oracle
- 生成镜像及发布容器流程:
# 新建镜像重新发布流程
# 停止正在运行的容器, 如果之前存在失败安装可运行此步骤
docker stop oracle11g# 删除容器, 如果之前存在失败安装可运行此步骤
docker rm oracle11g# 列出所有镜像,找到镜像ID, 如果之前存在失败安装可运行此步骤
docker images# 删除镜像(使用镜像名称或ID), 如果之前存在失败安装可运行此步骤
docker rmi oracle11g-xe# 在包含Dockerfile的目录中执行
docker build -t oracle11g-xe .# 启动容器
docker run -d \--name oracle11g \--privileged \--shm-size=1g \-p 1521:1521 \-p 8080:8080 \-v oracle_data:/u01/app/oracle/oradata \oracle11g-xe# 查看容器日志
docker logs -f oracle11g # 在容器内连接
docker exec -it oracle11g sqlplus system/oracle@localhost:1521/ORCL# 从宿主机连接
sqlplus system/oracle@localhost:1521/ORCL# 导出oracle镜像
docker save -o oracle11g.tar oracle11g-xe