无法连接到 docker 容器外的 oracle

Can not connect to oracle outside docker container

我已经为 oracle 19c 创建了一个镜像。我已经用下面的命令启动了我的容器。

docker run --name oracledb -d -p 1527:1521 -p 5700:5500 -e ORACLE_PWD=password1 -e ORACLE_CHARACTERSET=AL32UTF8 -v /d/docker-code/oracle-data oracle/database:19.3.0-ee

创建容器后,我可以登录容器。我可以在容器内使用以下命令连接。

sqlplus system/password1@172.17.0.2:1527/ORCLCDB

在容器外部,从 cmd,我无法连接到那个 oracle 实例。

注意:我已经在 windows 机器上的 1521 端口安装了 oracle,这是默认端口。

listener.ora

LISTENER =
(DESCRIPTION_LIST =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
    (ADDRESS = (PROTOCOL = TCP)(HOST = 172.17.0.2)(PORT = 1527))
  )
)

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = ORCLCDB)
      (ORACLE_HOME = /opt/oracle/product/19c/dbhome_1)
      (SID_NAME = ORCLCDB)
    )
   )


DEDICATED_THROUGH_BROKER_LISTENER=OFF
DIAG_ADR_ENABLED = off

tnsnames.ora

ORCLCDB=172.17.0.2:1527/ORCLCDB
ORCLPDB1=
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 172.17.0.2)(PORT = 1527))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLPDB1)
    )
  )

我无法从 SQL 开发人员或 CMD 进行连接。当我尝试访问 docker 容器外部时,我能够得到超时错误。

ERROR:
ORA-12170: TNS:Connect timeout occurred

请指导我解决这个问题。

提前致谢。

确保所有容器网络接口都在侦听数据库流量,因此 0.0.0.0。不要硬编码 docker 网桥网络地址 (172..),因为该地址将在容器启动时分配。只需坚持使用容器本地的默认端口 1521。此端口 - 1521 - 是容器的本地端口,不会暴露给主机 OS。您将此端口发布到您决定使用哪个端口的主机 OS,因此 -p 1522:1521

LISTENER =
(DESCRIPTION_LIST =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
    (ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521))
  )
)

现在您可以通过发出以下命令在主机 OS 上启动容器:

docker run --name oracledb -d -p 1522:1521 -p 5700:5500 -e ORACLE_PWD=password1 -e ORACLE_CHARACTERSET=AL32UTF8 -v /d/docker-code/oracle-data oracle/database:19.3.0-ee

现在应该可以了

sqlplus system/password1@localhost:1522/ORCLCDB

提示。您现在可以从 Docker Hub 中提取 Oracle XE 18c。如果您仍然无法使用您构建的图像连接,我建议您测试此图像。

docker run -d -p 1522:1521 --rm -e ORACLE_PASSWORD=Welcome_1 -v oracle-volume:/opt/oracle/oradata gvenzl/oracle-xe

docker container ls
CONTAINER ID   IMAGE              COMMAND                  CREATED             STATUS             PORTS                    NAMES
97ce7cd3d7ca   gvenzl/oracle-xe   "container-entrypoin…"   About an hour ago   Up About an hour   0.0.0.0:1522->1521/tcp   elegant_borg

sqlplus system/Welcome_1@localhost:1522/XE

SQL*Plus: Release 19.0.0.0.0 - Production on Mon May 31 10:21:18 2021
Version 19.8.0.0.0

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

Last Successful login time: Mon May 31 2021 10:20:19 +02:00

Connected to:
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0

system@XE> set echo on
system@XE> @pdbs
system@XE>
system@XE> col con_id format 999
system@XE> col dbid format 99999999999999999
system@XE> col name format a30
system@XE> col open_mode format a10
system@XE> col open_time format a38
system@XE>
system@XE> select p1.con_id, p1.dbid, p1.name, p2.status, p1.open_mode, p1.open_time
  2  from
  3    v$pdbs p1 join dba_pdbs p2 on (p1.con_id = p2.con_id);

CON_ID               DBID NAME                           STATUS     OPEN_MODE  OPEN_TIME
------ ------------------ ------------------------------ ---------- ---------- --------------------------------------
     2         1957965804 PDB$SEED                       NORMAL     READ ONLY  31.05.2021 06:54:00474 AM +00:00
     3         4267393270 XEPDB1                         NORMAL     READ WRITE 31.05.2021 06:54:01229 AM +00:00

2 rows selected.

system@XE>

祝你好运!