Sign inSign up

framontb/perl

By framontb

Updated about 6 years ago

Images for perl containers with modules preinstaled, such as: dbi, mariadb, etc

Image
Integration & delivery
0

197

framontb/perl repository overview

framontb/perl

This is a perl image ready to execute perl scripts that connects to MySQL or MariaDB database.

The tag name comes from the software packets inside: alpine, perl, dbi, and mysql driver.

Pull the image

docker pull framontb/perl

Sw Packets

Based in the Alpine image with installed Perl 5.30.3, the DBI module, and the DBD::mysql driver.

Other packets included are: perl-dev, gcc, g++, make, mariadb-dev, bash

This other packets are included so you can add new (compiled) Perl modules at will.

The size of this image is 336 MB witch makes it significantly smaller that the official Perl image (859 MB).

Getting Started

Verify Perl 5.30.3 version

docker run -it --rm framontb/perl perl --version

Run a single Perl script

Here explained, are the steps you need to accomplish in order to run a single script inside a container with the image framontb/perl:alpine-perl-dbi-mysql.

Create a Perl script in your host machine

For example you can create this hello_wold.pl script with this single line:

print "Hello World !\n";
Open shell and move to current script's directory

Open a Linux shell in your host machine and move to the directory where your script actually is

$ cd <your_working_directory>
Run your Perl script inside the container
$ docker run -it --rm --name perl_hello_world -v "$PWD":/usr/src/myapp -w /usr/src/myapp framontb/perl perl hello_world.pl

Hello World !

The options in docker run explained:

Connect Perl script to a MariaDB database

The code here is designed to execute the TutorialsPoint - Perl - Database Access examples, with little change.

What this Perl script example does is writing a register in a table (TEST_TABLE) of the MariaDB database (TESTDB). Both, the MariaDB server and the Perl script runs inside their own containers (mariadb and perl-mysql-cx) connected with a docker network (mariadb).

Create the network
$ docker network create mariadb
Run the container with the MariaDB server

This container is connected to the mariadb network and set up the following users (testuser, root) and database (TESTDB):

  • MYSQL_ROOT_PASSWORD=root_password
  • MYSQL_USER=testuser
  • MYSQL_PASSWORD=test123@
  • MYSQL_DATABASE=TESTDB
$ mkdir -p ~/docker/mariadb
$ docker run -e MYSQL_ROOT_PASSWORD=root_password -e MYSQL_USER=testuser -e MYSQL_PASSWORD=test123@ -e MYSQL_DATABASE=TESTDB -v ~/docker/mariadb:/var/lib/mysql --name mariadb -d mariadb
Add container mariadb to the network mariadb
$ docker network connect mariadb mariadb
Check the container is up and running
$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3a2f32973190        mariadb             "docker-entrypoint.s…"   13 minutes ago      Up 13 minutes       3306/tcp            mariadb
Create the TEST_TABLE
Get mariadb container IP
$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' mariadb
172.17.0.2

Note that your IP could be different from that (172.17.0.2)

Connect to the mariadb container

NOTE: May be you need to install the mariadb-client in order to connect (apt-get install mariadb-client).

Open other terminal and connect to the database with the MariaDB client

$ mysql -u testuser -p -h 172.17.0.2
PASS: test123@

mysql> use TESTDB;

mysql> create table TEST_TABLE
(   FIRST_NAME  char(100),
    LAST_NAME   char(100),
    AGE         tinyint unsigned,
    SEX         char(1),
    INCOME      int unsigned
);
Verify the table creation
mysql> select * from TEST_TABLE;
Empty set (0.001 sec)
Create the 01_connect.plPerl script

Note we added the $dbserver host variable that is not present in TutorialsPoint code.

use DBI;
use strict;

print "--- Inserting data in TESTDB:TEST_TABLE ---\n";

my $driver = "mysql"; 
my $database = "TESTDB";
my $dbserver = "mariadb";
my $dsn = "DBI:$driver:database=$database:host=$dbserver";
my $userid = 'testuser';
my $password = "test123@";
#my $db_table = "TEST_TABLE";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;

my $sth = $dbh->prepare("INSERT INTO TEST_TABLE
                       (FIRST_NAME, LAST_NAME, SEX, AGE, INCOME )
                         values
                       ('RAM', 'ZAR', 'M', 48, 54321)");
$sth->execute() or die $DBI::errstr;
$sth->finish();
Run 01_connect.plPerl script in framontb/perl container
docker run -it --rm --name perl-mysql-cx --network mariadb -v "$PWD":/usr/src/myapp -w /usr/src/myapp framontb/perl perl 01_connect.pl

--- Inserting data in TESTDB:TEST_TABLE ---
Verify a new register was added to the database

Come back to the shell with mariadb-client connected to database and check there is a new register.

mysql> select * from TEST_TABLE;
+------------+-----------+------+------+--------+
| FIRST_NAME | LAST_NAME | AGE  | SEX  | INCOME |
+------------+-----------+------+------+--------+
| RAM        | ZAR       |   21 | M    |  54321 |
+------------+-----------+------+------+--------+
1 rows in set (0.00 sec)

mysql> exit;

Tag summary

Content type

Image

Digest

Size

108.2 MB

Last updated

about 6 years ago

docker pull framontb/perl