Search code examples
oracledockeroracle-sqldeveloperdocker-for-windowsoracle19c

Can not connect to oracle outside docker container


I have create an image for the oracle 19c. I have started my container with below command.

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

After creation of container, I am able to login in the container. I am able to connect with the below command inside the container.

sqlplus system/[email protected]:1527/ORCLCDB

Outside of the container, from the cmd, I can not connect to that oracle instance.

Note: I have already installed oracle on the windows machine at port 1521 which is default port.

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)
    )
  )

I am not able to connect from SQL Developer or from CMD. I am able to get timeout error when I am trying to access outside of the docker container.

ERROR:
ORA-12170: TNS:Connect timeout occurred

Please guide me to resolve this issue.

Thanks in Advance.


Solution

  • Make sure all container network interfaces are listening for database traffic, hence 0.0.0.0. Do not hardcode a docker bridge network address (172..) because this address will be assigned at container startup. Just stick with the default port 1521 local to the container. This port - 1521 - is local to the container and not exposed to the host OS. You publish this port to the host OS where you decide which port to use, hence -p 1522:1521

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

    Now you can fire up the container on your host OS by issuing:

    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
    

    This should now work

    sqlplus system/password1@localhost:1522/ORCLCDB
    

    Tip. You can now pull Oracle XE 18c from Docker Hub. I recommend you to test this image if you still cannot connect using the image you have built.

    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>
    

    Best of luck!