Oracle Database Free 23ai Docker版を導入してみる(セットアップ~接続~停止)

Oracleアカウント無しで利用可能なので誰でも手軽に使えます。
https://www.oracle.com/jp/database/free/get-started/
Docker、Podmanどちらでも利用可。
3ステップで利用開始できてお手軽でした。

### Step1. docker pull
```
$ docker pull container-registry.oracle.com/database/free:latestlatest: Pulling from database/free
80575aa2b656: Pull complete
c7487a59920d: Pull complete
3625c76a81cd: Pull complete
e92ef858c35a: Pull complete
4cdc2f359645: Pull complete
27f2cd251147: Pull complete
e1d2be5b6f4d: Pull complete
f43e00abc1ec: Pull complete
c24ea8b8aede: Pull complete
59d4f3d589a7: Pull complete
d37d5c7cf70e: Pull complete
7a721ebefa23: Pull complete
56d5b2028629: Pull complete
d58fe703cdfd: Pull complete
60b3a84703cd: Pull complete
ef5e04f20d92: Pull complete
8d44d63eca2b: Pull complete
d6d374046fa9: Pull complete
91af3ce82719: Pull complete
4d5be8b20b6b: Pull complete
64c6d1a50ca5: Pull complete
Digest: sha256:c4c3c5caaef020a6f45cbb19098d9442eee3ac48f038b6c0fcf8a5aa323d4ecd
Status: Downloaded newer image for container-registry.oracle.com/database/free:latest
container-registry.oracle.com/database/free:latest
$

```

### Step2. イメージ確認
```
$ docker images | grep -i oracle
container-registry.oracle.com/database/free   latest                            2d0ea0353ff3   8 weeks ago     9.49GB

```
OracleDBのコンテナイメージ、昔からですがサイズが大きい！
RDB製品だから大きいというものではないです。例えばDb2だと3GB弱。


### Step3. docker run
```
$ docker run --name myora23 container-registry.oracle.com/database/free
Starting Oracle Net Listener.
Oracle Net Listener started.
Starting Oracle Database instance FREE.
Oracle Database instance FREE started.

The Oracle base remains unchanged with value /opt/oracle
#########################
DATABASE IS READY TO USE!
#########################
The following output is now a tail of the alert.log:
2025-03-24T02:19:19.533315+00:00
PDB$SEED(2):Opening pdb with Resource Manager plan: DEFAULT_PLAN
(3):--ATTENTION--
(3):PARALLEL_MAX_SERVERS (with value 1) is insufficient. This may affect transaction recovery performance.
Modify PARALLEL_MAX_SERVERS parameter to a value > 4 (= parallel servers count computed from parameter FAST_START_PARALLEL_ROLLBACK) in PDB ID 3
FREEPDB1(3):Autotune of undo retention is turned on.
2025-03-24T02:19:20.308629+00:00
FREEPDB1(3):Opening pdb with Resource Manager plan: DEFAULT_PLAN
Completed: Pluggable database FREEPDB1 opened read write
Completed: ALTER DATABASE OPEN
```

この画面ではsqlplusなどのコマンドは入力できません。
Ctrl + P + Q も効きませんでしたが自分の環境だけの問題なのかはわかりません。



### Step4. PDBに接続
もうひとつターミナルを起動して、docker exec -it でログインします。

#### Step4-1 コンテナ稼働状況を確認
```
$ docker ps | grep -i myora
1087e570cb43   container-registry.oracle.com/database/free   "/bin/bash -c $ORACL…"   3 days ago      Up 28 seconds (healthy)   1521/tcp 
```
Up / (healty) と出ているので問題ありません

#### Step4-2 コンテナにログイン
```
$ docker exec -it myora23 bash
bash-4.4$ 
```
ログイン成功。

#### Step4-3 パスワード設定
sys, systemユーザー用のパスワードを設定（参考記事：[Link](https://qiita.com/poruki1010/items/3777035097c069c59656))
```
bash-4.4$ ./setPassword.sh xxxxxxxx
The Oracle base remains unchanged with value /opt/oracle

SQL*Plus: Release 23.0.0.0.0 - Production on Thu Mar 27 11:26:38 2025
Version 23.6.0.24.10

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


Connected to:
Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.6.0.24.10

SQL>
User altered.

SQL>
User altered.

SQL>
Session altered.

SQL>
User altered.

SQL> Disconnected from Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.6.0.24.10
bash-4.4$

```

#### Step4-4 PDBに接続

```
bash-4.4$ sqlplus sys@localhost:1521/FREEPDB1 as sysdba

SQL*Plus: Release 23.0.0.0.0 - Production on Thu Mar 27 11:27:53 2025
Version 23.6.0.24.10

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

Enter password:

Connected to:
Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.6.0.24.10

SQL>

```

#### Step4-5 SQL実行

**ユーザー作成/最低限の権限付与**
```
SQL> create user user1 identified by "password";

User created.

SQL> grant create session, create table to user1;

Grant succeeded.

SQL> alter user user1 quota unlimited on users;

User altered.


```

**接続**
```
bash-4.4$ sqlplus user1@localhost:1521/FREEPDB1

SQL*Plus: Release 23.0.0.0.0 - Production on Thu Mar 27 11:35:20 2025
Version 23.6.0.24.10

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

Enter password:

Connected to:
Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.6.0.24.10


```


**テーブル作成**
```
SQL> create table t1 (c1 number, c2 char(32));

Table created.

SQL> 
```

**データ行挿入**
```
SQL> insert into t1 values (1,'A');

1 row created.

SQL> insert into t1 values(2,'B'),(3,'C');

2 rows created.

SQL>

```

**Selectステートメント**
```
SQL> select * from t1;

        C1 C2
---------- --------------------------------
         1 A
         2 B
         3 C

SQL>
```


Oracle、昔は複数行INSERTできなかった記憶があるんですが、今はできるんですね(嬉)


#### Step5.コンテナの終了
docker run を実行したのとは別の画面で、docker stopを実行すると終了できます。

**コンテナの停止**

```
$ docker stop myora23
myora23
$
```

同じ名前で起動すればまた同じコンテナを利用可能。

**コンテナの起動**
```
$ docker start myora23
myora23
$
```