Containerizing a Perl Script
2015-08-29 | #containers #docker #perl
Intro
Almost every IT department has one; a Perl script written decades ago by a long-gone employee that's critical to production. It does it's job well, but no one wants to touch or take responsibility for it. You just want to upgrade your infrastructure and bring it along, but there are so many CPAN[1] modules and inconsitencies you want never want to look at it again. What to do?
Executable Containers
Most articles on containers focus on running a service, exposing ports and data volumes, but one feature frequently overlooked is the ability to run a container as an executable.
In Docker[2], the ENTRYPOINT[3] instruction makes this possible by running a command and accpeting command line arguments from `docker run`. Combining this with `-it` and `--rm` will run an interactive emphermal container, disappearing when the command is complete.
3: https://docs.docker.com/reference/builder/#entrypoint
Dockerfile
Let's walk through an example that runs a Perl script called ps.pl[4], which prints a simple process table using the CPAN module Proc::ProcessTable[5] and includes `-h` and `-v` arguments.
4: https://gist.github.com/ecliptik/9a868cbe348d87a5141a#file-ps-pl
5: http://search.cpan.org/~durist/Proc-ProcessTable-0.39/ProcessTable.pm
The following Multi-Stage Dockerfile[6] will build a container to run a simple perl script,
6: https://docs.docker.com/develop/develop-images/multistage-build/
- Debian Stable Slim base image
- Install OS packages
- Install CPAN modules
- Copy ps.pl[7] script
- Set `ps.pl` script as ENTRYPOINT[8]
- Set `-h` as default argument
7: https://gist.github.com/ecliptik/9a868cbe348d87a5141a#file-ps-pl
8: https://docs.docker.com/engine/reference/builder/#entrypoint
Here is the Dockerfile[9] in full,
9: https://gist.github.com/ecliptik/9a868cbe348d87a5141a#file-dockerfile
Dockerfile Breakdown
Standard start of a Dockerfile, using the Official Debian image[10] tagged `stable-slim` as the `base` layer and a label[11] for the image maintainer,
10: https://hub.docker.com/_/debian/
11: https://docs.docker.com/engine/reference/builder/#label
Basic environment variables for Debian packages through `apt` non-interactive and set locale to UTF-8,
Install "runtime" packages for the `base` layer that other layers will inheriet. Sets `/app` as WORKDIR[12],
12: https://docs.docker.com/engine/reference/builder/#workdir
Intermediate `build` layer which is used temporarily to install packages and modules that won't be used directly at runtime. Using an intermeiate layer will save space in the resulting image and no include packages that aren't used explicitly for runtime operation, improving security posture.
Install Perl CPAN[13] packages and dependencies with cpanminus[14] which works well within a container build environment,
14: http://search.cpan.org/~miyagawa/App-cpanminus-0.05/cpanm
Create final `run` layer for the resulting image and copy `/usr/local` from the intermediate `build` layer to include required CPAN modules,
Copy ps.pl[15] script into `WORKDIR` (aka `/app`) of the container. This is put near the end of the `Dockerfile` since it is the file most likely to change and will allow re-using of cache for previous layers, speeding up subsequent builds[16].
15: https://gist.github.com/ecliptik/9a868cbe348d87a5141a#file-ps-pl
16: https://thenewstack.io/understanding-the-docker-cache-for-faster-builds/
Set `ENTRYPOINT` to the ps.pl[17] script,
17: https://gist.github.com/ecliptik/9a868cbe348d87a5141a#file-ps-pl
Running the Containerized Perl Script
Now that the Dockerfile is complete, build a container image with the name `ps-perl`,
With a container image ready, run it with `docker run -it --rm ps-perl`, using the `-it` and `--rm` options so the container is removed when it exits,,
Command Line Arguments
Command line arguments will also pass into the container when run using the CMD[18] to pass to `ENTRYPOINT`,
18: https://docs.docker.com/engine/reference/builder/#cmd
`-h`
`-v`
Conclusions
By using the `-slim` variant of Debian for the base image and multi-stage builds, the resulting container size is just over 130MB. Using a smaller image like Alpine Linux[19] could further reduce the image size, but could introduce known caveats[20] because of musl libc and glibc differences[21].
19: https://hub.docker.com/_/alpine/
20: http://gliderlabs.viewdocs.io/docker-alpine/caveats/
21: https://wiki.musl-libc.org/functional-differences-from-glibc.html
Additional References
- Dockerising a Perl application[22]
- Downsizing Docker Containers[23]
- Proc::ProcessTable Example[24]
22: http://robn.io/docker-perl/
23: https://intercityup.com/blog/downsizing-docker-containers.html
24: http://search.cpan.org/~durist/Proc-ProcessTable-0.39/ProcessTable.pm
(Updated 9/11/2020)
Tags
____________________________________________________________________