To Nha Notes | Aug. 13, 2024, 12:25 a.m.
We want to provide a few examples of tasks you may want to run in a container instead of natively on your computer.
Let’s assume you need to strip all leading whitespaces from a file and you found the following handy Perl script to do exactly that:
$ cat sample.txt | perl -lpe 's/^\s*//'
As it turns out, you don’t have Perl installed on your working machine.
What can you do? Install Perl on the machine? Well, that would certainly be an option, and it’s exactly what most developers or system admins do. But wait a second, you already have Docker installed on your machine. Can’t we use Docker to circumvent the need to install Perl? And can’t we do this on any operating system supporting Docker? Yes, we can. This is how we’re going to do it:
$ docker container run --rm -it \ -v $(pwd):/usr/src/app \ -w /usr/src/app \ perl:slim sh -c "cat sample.txt | perl -lpe 's/^\s*//'"
The preceding command runs a Perl container (perl:slim) interactively, maps the content of the current folder into the /usr/src/appfolder of the container, and sets the working folder inside the container to /usr/src/app. The command that is run inside the container is as follows:
sh -c "cat sample.txt | perl -lpe 's/^\s*//'"
It basically spawns a Bourne shell and executes our desired Perl command.
Without needing to install Perl on our machine, we were able to achieve our goal. The nice thing is that, after the script has run, the container is removed from your system without leaving any traces because we used the --rm flag in the docker container runcommand, which automatically removes a stopped container.
TIP
If that doesn’t convince you yet because, if you’re on macOS, you already have Perl installed, then consider you’re looking into running a Perl script named your-old-perl-script.pl that is old and not compatible with the newest release of Perl that you happen to have installed on your system. Do you try to install multiple versions of Perl on your machine and potentially break something? No, you just run a container with the (old) version of Perl that is compatible with your script, as in this example:
$ docker container run -it --rm \
-v $(pwd):/usr/src/app \
-w /usr/src/app \
perl:<old-version> perl your-old-perl-script.pl
Here, <old-version> corresponds to the tag of the version of Perl that you need to run your script.
In the next section, we are going to demonstrate how to run a Python script.
A lot of people use quick and dirty Python scripts or mini apps to automate tasks that are not easily coded with, say, Bash. Now, if the Python script has been written in Python 3.x and you only happen to have Python 2.7 installed or no version at all on your machine, then the easiest solution is to execute the script inside a container. Let’s assume a simple example where the Python script counts lines, words, and letters in a given file and outputs the result to the console:
$ docker container run --rm -it \ -v $(pwd):/usr/src/app \ -w /usr/src/app \ python:3-alpine python stats.py sample.txt