Deploying PSGI Applications using RHSCL Docker Containers
The basic idea is to combine your application code from Git tree and Red Hat’s rhscl/perl-520-rhel7 base image into an application image that will run your application in mod_perl environment. Your application can either be a simple Common Gateway Interface (CGI) script or a full-fledged Perl Web Server Gateway Interface (PSGI) application.
Following this step-by-step procedure will show you how to deploy a simple pastebin-like web service implemented as a PSGI application.
The Base Image
First we install docker package and start the docker service:
# yum install docker # systemctl start docker
Then we download the rhscl/perl-520-rhel7 Docker image:
# docker pull rhscl/perl-520-rhel7
Now you can verify that the base image really contains Perl 5.20:
# docker run --rm -ti rhscl/perl-520-rhel7 perl -v This is perl 5, version 20, subversion 1 (v5.20.1) built for x86_64-linux-thread-multi […]
The Application Source
Back to the application. We will create a simple pastebin-like application using Web::Paste::Simple Perl module. We will need git tool, so we will install it:
# yum install git
We will begin with an empty Git repository:
$ mkdir application $ cd application $ git init-db . Initialized empty Git repository in /tmp/application/.git/
Now we will write our application:
$ cat >application.psgi use Web::Paste::Simple; Web::Paste::Simple->new()->app; ^D
$ plackup application.psgi HTTP::Server::PSGI: Accepting connections at http://0:5000/ ^C
But let’s assume our code is correct and continue with building the application image.
As I wrote, the application uses Web::Paste::Simple CPAN module. Thus we need to declare this dependency. We will use a cpanfile for that:
$ cat >cpanfile requires 'Web::Paste::Simple'; ^D
If you haven’t yet configured your Git identity, do it now:
$ git config --global user.email "email@example.com" $ git config --global user.name "Developer"
And then commit the sources into Git tree:
$ git add application.psgi cpanfile $ git commit -m 'Simple application' [master (root-commit) 78d64e4] Simple application 2 files changed, 3 insertions(+) create mode 100644 application.psgi create mode 100644 cpanfile
The Application Image
Now we are ready to build our application image. We will use s2i tool for that. The tool is provided by source-to-image package, so install it:
# yum install source-to-image
The magic command to build an application from a base Docker image and a Git tree is:
# s2i build file://$PWD rhscl/perl-520-rhel7 application I1109 15:39:58.995684 06268 sti.go:406] ---> Installing application source I1109 15:39:59.031843 06268 sti.go:406] ---> PSGI application found in ./application.psgi I1109 15:39:59.053222 06268 sti.go:406] ---> Installing cpanminus 1.7102 ... […] I1109 15:41:57.491068 06268 sti.go:406] <== Installed dependencies for .. Finishing. I1109 15:41:57.491118 06268 sti.go:406] 44 distributions installed
The second argument is a URL to the Git tree with your application. We used current working directory in this example. But you can use any remote location. For example a repository on the GitHub. The third argument is base Docker image identifier. We used the Red Hat’s Perl 5.20 RHSCL image. The last argument is a tag for the resulting application image.
If you forget this incantation, don’t worry. If you run the base image without specifying a command (
docker run --rm -ti rhscl/perl-520-rhel7), you will get nice help. You can also see Using Red Hat Software Collections Container Images for more details.
As you can see, the application image was built successfully. And we can verify it in Docker images listing:
# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE application latest eed01e5e8b24 8 minutes ago 459.4 MB rhscl/perl-520-rhel7 latest f3ed77ae8490 7 weeks ago 502.7 MB
So we did it. Now we can run the application:
# docker run --rm -ti application [Mon Nov 09 09:53:58.308695 2015] [so:warn] [pid 1] AH01574: module perl_module is already loaded, skipping AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.34.4.8. Set the 'ServerName' directive globally to suppress this message
And the application will be available on the reported IP address and TCP port 8080. You can use docker’s option -p to publish the container’s port 8080 on different external port number.
Finally, we can use a web browser to connect to the application:
And play with it:
To learn more, visit our Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets (e.g. containers), books (e.g. microservices), and product downloads that can help you with your microservices and/or container application development.