making a docker for running perl
With the helps of docker, we can make an image to have all needed perl packages installed, and run this image from everywhere.
Here I give a basic sample for building a docker for perl development.
Say the project name is "gmbox", which uses a special package Gmail::Mailbox::Validate to work.
First we create a dir named as "gmbox", and put Dockerfile under this dir.
$ mkdir gmbox
$ touch gmbox/Dockerfile
The next step we want to edit Dockerfile and put the following content into it.
FROM perl
LABEL maintainer=@pyh
RUN apt update && apt install -y cpanminus && cpanm Gmail::Mailbox::Validate
Here are the comments to Dockerfile above.
- FROM - define the base image from Dockerhub, here is perl.
- LABEL - define some labels to identify this image.
- RUN - run the special commands to make the image, i.e, install some packages.
In the RUN section, we run apt to install cpanminus firstly, then install Gmail::Mailbox::Validate via cpanminus.
The next step we want to run "docker build" for buiding that image.
$ docker build -t gmbox gmbox
Here -t specify the name for docker image, the last argument points to the dir which has Dockerfile included.
After successful building we will see the output:
...
---> 111b725bb261
Successfully built 111b725bb261
Successfully tagged gmbox:latest
Hence we can list the image just built:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
gmbox latest 111b725bb261 About a minute ago 921MB
How to run this docker image? Let's make a script and put it into "bin" dir under current dir.
$ cat bin/gmbox.pl
use Gmail::Mailbox::Validate;
use strict;
my $username = shift || die "$0 username\n";
my $v = Gmail::Mailbox::Validate->new();
print $v->validate($username) ? "$username exists\n" : "$username not exists\n";
Finally we run this script using the docker image.
$ docker run -v `pwd`:/home gmbox perl /home/bin/gmbox.pl johnsmart
johnsmart exists
$ docker run -v `pwd`:/home gmbox perl /home/bin/gmbox.pl johndoe
johndoe not exists
The -v option maps current dir (`pwd`) to docker's /home dir. So the script's path in docker is "/home/bin/gmbox.pl".
And the running results are printed out to screen.
All done with this way.