Skip to content

Instantly share code, notes, and snippets.

@kodaka
Last active September 8, 2019 13:06
Show Gist options
  • Save kodaka/c37ecad4711132eb50a029b8eff53dd4 to your computer and use it in GitHub Desktop.
Save kodaka/c37ecad4711132eb50a029b8eff53dd4 to your computer and use it in GitHub Desktop.
title date tags
Perl DBD::Oracle in Docker
2019-09-05 21:00:00 +0900
oracle
docker
perl

Let's use Perl DBD::Oracle in Docker.

At first, build Docker image of Oracle Instant Client (in this case we create Oracle Instant Client 19 with ghq)

ghq get https://github.com/oracle/docker-images
cd $(ghq root)/github.com/oracle/docker-images
cd OracleInstantClient/dockerfiles/19
docker build --pull -t oracle/instantclient:19 .

And then, build more with DBD::Oracle.

#
# Dockerfile my-awsome-image
#
FROM oracle/instantclient:19

ENV ORACLE_HOME /usr/lib/oracle/19.3/client64
ENV LD_LIBRARY_PATH /usr/lib/oracle/19.3/client64/lib
ENV PATH $PATH:/usr/lib/oracle/19.3/client64/bin

# set yours
ENV NLS_LANG Japanese_Japan.AL32UTF8

RUN yum update -y && \
    yum install -y gcc gzip tar perl && \
    yum install -y perl-App-cpanminus perl-DBI && \
    yum clean all && \
    rm -rf /var/cache/yum/*
RUN cpanm DBD::Oracle
RUN mkdir -p /usr/src/myapp

WORKDIR /usr/src/myapp
CMD ["perl", "-v"]
docker build -t my-awsome-image .

Now we can execute Perl script and Oracle SQL*Plus like this:

docker run --rm -it -v $(pwd):/usr/src/myapp my-awsome-image perl test.pl
docker run --rm -it my-awsome-image /bin/bash
sqlplus user/pswd@db.example.com:1521/servicename

The Perl script may be:

#!perl
use strict;
use warnings;
use utf8;
use DBI;

my %Config = (
    dsn      => 'dbi:Oracle://db.example.com:1521/servicename',
    user     => 'user',
    password => 'pswd',
    attrs    => {
        AutoCommit => 0,
        RaiseError => 1,
    },
);

do_task();

sub do_task {
    my $dbh = DBI->connect($Config{dsn}, $Config{user}, $Config{password}, $Config{attrs})
        or die $DBI::errstr;

    my $sth = $dbh->prepare(q{
        SELECT TABLE_NAME FROM USER_TABLES
    }) or die $dbh->errstr;

    $sth->execute() or die $sth->errstr;
    while (my $row = $sth->fetchrow_arrayref) {
        print join(",", @$row), "\n";
    }
    $sth->finish;

    $dbh->rollback;
    $dbh->disconnect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment