简体   繁体   中英

how to run sqlplus from my laptop and connect to dockerized oracle launched with docker-compose?

I am trying to set up automated integration tests against an Oracle database and am planning on using https://www.testcontainers.org/ to launch the containers in my docker-compose.yml.

I have succeeded in getting Dockerized Oracle running on my laptop using these instructions: https://github.com/oracle/docker-images/tree/master/OracleWebLogic/samples/12212-oradb-wlsstore

The steps are:

# see https://docs.oracle.com/cd/E37670_01/E75728/html/oracle-registry-server.html
docker login container-registry.oracle.com  # supply your oracle credentials as described above

docker network create -d bridge SampleNET

cat > env.txt <<EOF
DB_SID=InfraDB
DB_PDB=InfraPDB1
DB_DOMAIN=us.oracle.com
DB_BUNDLE=basic
EOF

docker run -d --name InfraDB --network=SampleNET -p 1521:1521 -p 5500:5500 --env-file env.txt -it --shm-size="8g"  store/oracle/database-enterprise:12.2.0.1

After a minute when I issue the command "sudo docker ps -a" I see the DB is up and healthy.

CONTAINER ID        IMAGE                                       COMMAND                  
6a276a311b2e        store/oracle/database-enterprise:12.2.0.1   "/bin/sh -c '/bin/ba…"   

CREATED             STATUS                        PORTS                             
2 minutes ago       Up 2 minutes (healthy)        0.0.0.0:1521->1521/tcp, 0.0.0.0:5500->5500/tcp   

NAMES
InfraDB

And I can now run sqlplus (from a shell prompt on my laptop, not a Docker container):

sqlplus sys/Oradoc_db1@localhost:1521/InfraDB.us.oracle.com AS SYSDBA

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> 

My goal now is to create a Docker compose file that allows me to the same thing as what was accomplished with the command line invocation of Dockerized Oracle. Here is the docker-compose.yml file I tried:

version: '2'
services:
  database:
    image: store/oracle/database-enterprise:12.2.0.1
    ports:
      - 1521:1521
      - 8080:8080
      - 5500:5500
    environment:
      - DB_SID:InfraDB
      - DB_PDB:InfraPDB1
      - DB_DOMAIN:us.oracle.com
      - DB_BUNDLE:basic
networks:
  default:
    external:
      name: SampleNET

So, with this docker compose file I can launch a shell in the container running my Oracle DB and run sqlplus:

container=`sudo docker ps -a | grep oracle | sed -e's/\s.*//'`

sudo docker exec -t -i $container bash

# SESSION INSIDE DOCKER CONTAINER:
[oracle@8f26f224db03 /]$ sqlplus sys/Oradoc_db1  AS SYSDBA

                        SQL*Plus: Release 12.2.0.1.0 Production on Tue Mar 3 02:44:03 2020
                        Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Now, the million dollar question:

What is the syntax of the sqlplus command I would specify to connect FROM MY LAPTOP (ie, not launching a shell in the container running the DB server)?

Here are some of the many variants I tried (note: I tried with localhost, 127.0.0.1, and the actual IP of my laptop):

sqlplus sys/Oradoc_db1@'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=InfraDB)))'

sqlplus sys/Oradoc_db1@'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=InfraDB.us.oracle.com)))'

Diagnostic Note:

I am running Docker on Mac. After I launch Docker-ized Oracle I do see listeners on the ports that the docker compose configuration is forwarding, as shown below:

sudo lsof -i -P | grep -i "listen" | egrep "1521|8080|5500"

com.docke 6295 christopher.bedford   33u  IPv6 0xfffffffffffffff      0t0    TCP *:1521 (LISTEN)
com.docke 6295 christopher.bedford   34u  IPv6 0xfffffffffffffff      0t0    TCP *:5500 (LISTEN)
com.docke 6295 christopher.bedford   38u  IPv6 0xfffffffffffffff      0t0    TCP *:8080 (LISTEN)

Problem Solved

Here is a summary of what I did to solve the problem thanks to the suggestion of my friend Matt (Changos Muertos below).

1). creation of the bridge network 'SampleNET' is not required.

2). seems like specifying environment variables inline like this (below) does not work

  environment:
  - DB_SID:InfraDB
  - DB_PDB:InfraPDB1
  - DB_DOMAIN:us.oracle.com
  - DB_BUNDLE:basic

3). Instead it is better to define those environment settings in a file that is referenced by the 'env_file' configuration option as shown below. This file resides on /tmp/docker-compose.yml

version: '2'
services:
  database:
    image: store/oracle/database-enterprise:12.2.0.1
    ports:
      - 1521:1521
      - 8080:8080
      - 5500:5500
    env_file:
      - e2.txt

And the env file contents live in the same directory ( /tmp/e2.txt ), and has the content below:

DB_SID=InfraDB
DB_PDB=InfraPDB1
DB_DOMAIN=us.oracle.com
DB_BUNDLE=basic

To run i do

   docker-compose -f /tmp/docker-compose.yml up

and then i can connect (from my laptop as was the original goal) via:

    sqlplus sys/Oradoc_db1@0.0.0.0:1521/InfraDB.us.oracle.com as SYSDBA

This depends on a few things, like network type, and if the port is forwarded properly. Then verify this on the host (in linux 'netstat -tlpn' ), this will show you if it is listening on the port, and if so what interface and protocol. The tool may differ depending on the host OS.

Once you see it listening via tcp (not tcp6 only), and the firewall is open on the host, you can combine that info to make your connection string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM