1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

dockerコンテナ中のsqlplusから別のdockerコンテナのoracle databaseにアクセスする

Posted at

対象読者

dockerコンテナからoracle databaseを操作したい方

配置図

環境

名称 バージョン 説明
Windows 10 クライアントのOS
sqlplus 23.6 Oracle Databaseのコマンドラインインターフェイス
Oracle Database 23ai Free Release 23.0.0.0.0 データベース
Docker ?? コンテナ型仮想環境

フォルダ構成

フォルダorファイル名 説明
./docker/postgresql/Dockerfile Dockerイメージを作成するためのテキストファイル
./docker/postgresql/sdk oracle公式から落としたzipファイルを置くフォルダ
./docker-compose.yml コンテナ実行ファイル

手順

docker compose設定

ネットワーク関係の記述がキモですね。

docker-compose.yml
services:
  postgresql:
    # image: postgres:11.7
    container_name: postgresql
    build: ./docker/postgresql 
    environment:
      TZ: 'Asia/Tokyo'
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      # PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "5432:5432"
    volumes:
      - ./docker/postgresql/init:/docker-entrypoint-initdb.d
      - ./src:/src
      - db-data:/var/lib/postgresql/data
    # restart: always
+    networks:
+      external_11:
  oracle_db:
    image: oracle/database:23.5.0-free
    container_name: oracle_test_db
    shm_size: 1g # 共有メモリを1GB以上にしないとコンテナ起動時にエラーが発生します。
    environment:
      - TZ=Asia/Tokyo
      - LANGUAGE=ja_JP.ja
      - LANG=ja_JP.UTF-8
      - NLS_LANG=Japanese_Japan.AL32UTF8 # sqlplusの日本語化でこの環境変数が必要になります。
      - ORACLE_PWD=pass # パスワードは適宜指定します。
    ports:
      - "1521:1521"
    volumes:
      - ./src:/opt/oracle/oradata
+    networks:
+      external_11:

volumes:
  db-data:
+networks:
+  external_11:
+      driver: bridge
 

Docker DesktopでIPを調べたときの画像

image.png

instant clientをダウンロードしzipファイルを配置する

Oracle公式ページから instantClient をインストールする。
 
https://www.oracle.com/jp/database/technologies/instant-client/linux-x86-64-downloads.html

・instantclient-basic-linux.x64-23.6.0.24.10.zip
・instantclient-sdk-linux.x64-23.6.0.24.10.zip
・instantclient-sqlplus-linux.x64-23.6.0.24.10.zip

ダウンロードした3つのzipファイルはpostgresql/sdk/に配置する。

image.png

oracle instantのDockerFile

FROM postgres:14.0-bullseye

RUN apt-get update \
    && apt-get install -y build-essential git-core libv8-dev curl postgresql-server-dev-$PG_MAJOR unzip  make sudo libaio1 nmap iputils-ping net-tools\
    && rm -rf /var/lib/apt/lists/*


COPY ./sdk /tmp

USER $UNAME

# oracle instant clientの設定
# zipを解凍して 環境変数を通す
RUN unzip /tmp/instantclient-basic-linux.x64-23.6.0.24.10.zip -d /tmp
RUN unzip -o /tmp/instantclient-sdk-linux.x64-23.6.0.24.10.zip -d /tmp
RUN unzip -o /tmp/instantclient-sqlplus-linux.x64-23.6.0.24.10.zip -d /tmp

RUN sudo sh -c "echo /tmp/instantclient_23_6 > /etc/ld.so.conf.d/oracle-instantclient.conf"
RUN sudo ldconfig

ENV ORACLE_HOME=/tmp/instantclient_23_6
ENV LD_LIBRARY_PATH=/tmp/instantclient_23_6:$LD_LIBRARY_PATH
ENV PATH=/tmp/instantclient_23_6:$PATH
ENV NLS_LANG=Japanese_Japan.AL32UTF8
ENV ORACLE_SID=FREE

EXPOSE 5432


手順


docker-compose build

docker-compose up -d


# postgresqlコンテナ(oracleインスタント)に入る
docker-compose exec -it postgresql bash

# oracle_test_dbのゲートウェイを調べる

# nmapでOracleDatabase のポートが開けることを確認
nmap 192.168.0.1

#実行結果
postgres@e74e9b66e74f:/$ nmap 192.168.0.1
Starting Nmap 7.80 ( https://nmap.org ) at 2025-02-18 06:19 JST
Nmap scan report for 192.168.0.1
Host is up (0.000091s latency).
Not shown: 997 closed ports
PORT     STATE SERVICE
111/tcp  open  rpcbind
1521/tcp open  oracle
5432/tcp open  postgresql

# sqlplusでアクセスできることを確認
sqlplus system/pass@192.168.0.1:1521/FREE
# 実行結果
SQL*Plus: Release 23.0.0.0.0 - Production on 火 2月 18 06:52:07 2025
Version 23.6.0.24.10

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

最終正常ログイン時間: 月 2月  17 2025 07:27:24 +09:00


Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.6.0.24.10
に接続されました。
SQL> 

# 完

感想

とりあえず動作はしました。
IPを固定するのが課題です。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?