LoginSignup
2
1

More than 3 years have passed since last update.

Docker上のpython3からOracleDBへ接続する方法

Posted at

Docker上のpython3からOracleDBへ接続する方法のご紹介です。

下記の3つのファイルを同じディレクトリに配置して、コマンドを下記の通り実行してください。

docker-compose build
docker-compose up

dicker-compose.yml


version: '3'
services:
  python3:
    build:
      context: .
      dockerfile: Dockerfile

Dockerfile


FROM oraclelinux:7-slim

RUN yum -y install oracle-release-el7 && \
    yum -y install oracle-instantclient19.3-basiclite && \
    yum -y install vim && \
    yum -y install python36 && \
    python3.6 -m pip install cx_Oracle && \
    rm -rf /var/cache/yum

WORKDIR /myapp

ADD query.py /myapp

CMD exec python3.6 query.py

#ENTRYPOINT ["tail", "-f", "/dev/null"]

query.py


# query.py

import cx_Oracle

connection = cx_Oracle.connect( user='username', password='password', dsn='192.168.1.103:1521/ORCL' )

cursor = connection.cursor()
cursor.execute("select systimestamp from dual")

r, = cursor.fetchone()
print(r)

時刻が表示されれば成功です。

参考にした記事

Docker for Oracle Database Applications in Node.js and Python

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