1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Oracle Instant Client (Docker) から Oracle Database (Docker) に接続する

Posted at

Oracle Databaseだけでなく、Oracle Instant ClientについてもDockerで提供されています。この記事では、Oracle Instant Client (Docker) から Oracle Database (Docker) に接続する方法を説明します。

なお前提として、以下の手順にしたがって、Oracle DatabseのImageを作成されている必要があります。

ここでは、Oracle Database 21c の Express Edition を利用します。

git clone https://github.com/oracle/docker-images.git
cd docker-images/OracleDatabase/SingleInstance/dockerfiles/
./buildContainerImage.sh -x -v 21.3.0

1. Oracle Databaseの起動

docker-compose.yamlを準備します。

docker-compose.yaml
version: '3'
services:
  db:
    image: oracle/database:21.3.0-xe
    environment:
      - ORACLE_PWD=passw0rd
    hostname: oracle-db-host
    ports:
      - 1521:1521
      - 5500:5500
    volumes:
      - ./startup:/opt/oracle/scripts/startup
networks:
  default:
    name: "oracle-db-network" 

./startupにはコンテナ起動時に実行するSQLファイルを格納します。ここでは、HRスキーマの作成、employeesテーブルの作成とデータ投入を行います。

init.sql
ALTER SESSION SET CONTAINER=XEPDB1;

CREATE TABLESPACE HRDATA DATAFILE '/opt/oracle/oradata/HRDATA.DBF' SIZE 10M AUTOEXTEND ON;
CREATE USER HR IDENTIFIED BY hrpassw0rd DEFAULT TABLESPACE HRDATA QUOTA UNLIMITED ON HRDATA;
GRANT CREATE SESSION TO HR;

ALTER SESSION SET CURRENT_SCHEMA=HR;

CREATE TABLE employees (id INT NOT NULL PRIMARY KEY, name VARCHAR2(100));
INSERT INTO employees VALUES (1, 'Alice');
INSERT INTO employees VALUES (2, 'Bob');
INSERT INTO employees VALUES (3, 'Carol');

以上がそろったらコンテナを起動します。起動には時間がかかるので気長に待ちます。

docker compose up -d

2. Oracle Instant Client の起動

コンテナ上のOracle Databaseに、Docker上のOracle Instant Clientから接続するには、以下のコマンドを実行します。

docker run --rm -it --net oracle-db-network \
  ghcr.io/oracle/oraclelinux8-instantclient:21 \
  sqlplus HR/hrpassw0rd@oracle-db-host:1521/XEPDB1

重要なのは、Oracle Databaseを起動した際のhostnameとnetworkを指定すること。 実際にコマンドを実行してみると、HRスキーマに接続できることが確認できるはずです。

$ docker run --rm -it --net oracle-db-network \
>   ghcr.io/oracle/oraclelinux8-instantclient:21 \
>   sqlplus HR/hrpassw0rd@oracle-db-host:1521/XEPDB1

SQL*Plus: Release 21.0.0.0.0 - Production on Mon May 27 03:54:39 2024
Version 21.14.0.0.0

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

Last Successful login time: Mon May 27 2024 03:54:22 +00:00

Connected to:
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0

SQL> SET LINESIZE WINDOW
SQL> SELECT * FROM EMPLOYEES;

        ID NAME
---------- ----------------------------------------------------------------------------------------------------
         1 Alice
         2 Bob
         3 Carol

環境情報

C:\>ver

Microsoft Windows [Version 10.0.22631.3593]

C:\>wsl --version
WSL バージョン: 2.0.14.0
カーネル バージョン: 5.15.133.1-1
WSLg バージョン: 1.0.59
MSRDC バージョン: 1.2.4677
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows バージョン: 10.0.22631.3593

C:\>docker version
Client:
 Cloud integration: v1.0.35+desktop.5
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:08:44 2023
 OS/Arch:           windows/amd64
 Context:           default

Server: Docker Desktop 4.26.1 (131620)
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:08:02 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.25
  GitCommit:        d8f198a4ed8892c764191ef7b3b06d8a2eeb5c7f
 runc:
  Version:          1.1.10
  GitCommit:        v1.1.10-0-g18a0cb0
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?