Images for perl containers with modules preinstaled, such as: dbi, mariadb, etc
197
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.
docker pull framontb/perl
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).
docker run -it --rm framontb/perl perl --version
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.
For example you can create this hello_wold.pl script with this single line:
print "Hello World !\n";
Open a Linux shell in your host machine and move to the directory where your script actually is
$ cd <your_working_directory>
$ 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:
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).
$ docker network create mariadb
This container is connected to the mariadb network and set up the following users (testuser, root) and 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
$ docker network connect mariadb mariadb
$ 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
$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' mariadb
172.17.0.2
Note that your IP could be different from that (172.17.0.2)
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
);
mysql> select * from TEST_TABLE;
Empty set (0.001 sec)
01_connect.plPerl scriptNote 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();
01_connect.plPerl script in framontb/perl containerdocker 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 ---
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;
Content type
Image
Digest
Size
108.2 MB
Last updated
about 6 years ago
docker pull framontb/perl