How to use sqlplus on Oracle database inside a docker container?

23.7k views Asked by At

I installed oracle db version 12c in my docker environment. I used the following command:

docker run -d --name oracle -p 8080:8080 -p 1521:1521 quay.io/maksymbilenko/oracle-12c

I connected to the DB and everything went well but I wanted to enable unified audit. In order to do that, at first you must shutdown the Database and in all the instructions that I found it says to use sqlplus as following:

sqlplus / as sysoper
SQL> shutdown immediate
SQL> exit

I connected successfully to the DB using the next command:

docker exec -it oracle "bash"

and then I ran the sqlplus command and I received "command not found"

[root@f30cc670f85f /]#   sqlplus / as sysoper
bash: sqlplus: command not found

Am I doing it wrong? What should I do in order to have sqlplus on my oracle DB? I looked for it and didn't find anything that helped me.

I have mac if its relevant

7

There are 7 answers

2
APC On BEST ANSWER

I think that Docker image is just the database and enough of the OS to run the database. I don't think it includes client software such as SQL*Plus.

You need to have SQL*Plus installed on your Mac. If you haven't already, download the Oracle Instant Client for MacOS including the SQL*Plus extension. Or why not treat yourself and install the new-fangled sqlCL tool? It is easier to install and has all the SQL*Plus capabilities and a whole bunch more features. Find it here.

Whatever client you choose, once it's installed on your Mac you run it like any other app: when prompted for connection you give the string Maksym provides:

sqlplus user/password@ip:port/service-name
sqlplus system/oracle@//localhost:1521/xe 

If you need to connect as sys that would look like this:

sqlplus sys/oracle@//localhost:1521/xe as sysdba
0
Filip Jonckers On

When using the docker image store/oracle/database-enterprise:12.2.0.1-slim sqlplus and sqlldr tools are only available after the container has started. You can't do the following in a Dockerfile:

RUN sqlplus sys/password AS SYSDBA @create_database.sql

The container images can be configured to run scripts after setup and on startup. Currently sh and sql extensions are supported.

In your Dockerfile, copy the SQL script into the startup directory:

COPY create_database.sql  /opt/oracle/scripts/setup/01_create_database.sql

The database will be created on first startup of the container.

1
Guillermo Solana On

with this, you enter the image:

docker exec -it oracle /bin/bash

after that, you can use:

sqlplus sys as sysdba
3
EdStevens On

I don't have any experience with docker, but it looks for all the world like you are getting to a bash environment, so there we are on solid ground. The returned error ("bash: sqlplus: command not found") simply means that the executable (sqlplus) was not found in any directory listed in your PATH environment variable, as it exists within your shell environment. You actually need to set three variables: ORACLE_SID needs to be set to the value of your database name. ORACLE_HOME needs to be set to the value of the directory where your oracle binaries are installed. And PATH needs to have $ORACLE_HOME/bin added to it:

export PATH=$ORACLE_HOME/bin:$PATH

Obviously, since you are using the value of ORACLE_HOME in setting PATH, ORACLE_HOME needs to be set first.

2
szachMati On

For Windows OS:

Type docker ps in command line to show running containers and check container id.

Type docker exec -it container_id //bin/bash

Login via sqlplus command

Or the simplest way

docker exec -it container_id bash -c "source /home/oracle/.bashrc; sqlplus sys/Oradoc_db1@ORCLCDB as sysdba;"

More info is here: https://docs.oracle.com/en/database/oracle/oracle-database/21/deeck/index.html#DEEDK-GUID-EDA557B2-B0D6-45E1-8FBD-C1D756803982

0
Mohsen Taheri On

You should connect with oracle account

su -oracle
sqlplus / as sysdba
0
JayDP123 On

Sourcing the .bashrc should work to connect to sqlplus as sysdba. docker-compose exec db bash -c "source /home/oracle/.bashrc; sqlplus sys/Oradoc_db1@ORCLCDB as sysdba;"