Markus Oberlehner

Leveraging Docker to Run CLI Tools in Languages Like Java, Ruby, and PHP Without Local Installation


We often find ourselves needing to use various command-line interface (CLI) tools written in different programming languages. Sometimes, we don’t have the required programming environment set up on our local machines or may not want to install it for various reasons. Our hesitance could be due to concerns about system clutter, potential conflicts with other software, or simply wanting to maintain a clean development environment.

One possible solution to this problem is Docker, a tool that packages applications into containers—enabling us to run code in any environment without affecting the system we run it on.

In this article, we explore how to use Docker to run CLI tools written in languages like Java, PHP, or Ruby without installing these languages on our local machine. The idea for this approach stems from the CLI tool provided by mrsk.

How to run CLI tools in docker

Running CLI tools in Docker can be a lifesaver, especially when you don’t want to install a specific language environment on your computer. Docker allows us to create a container with everything we need to run a CLI command without affecting our local system.

The Dockerfile

A Dockerfile is a text file that contains all the commands needed to build a Docker image. The image, in turn, can be used to run Docker containers. Let’s take a closer look at a Dockerfile for running a Java CLI tool:

FROM amazoncorretto:21-alpine

WORKDIR /specmatic

ADD https://github.com/znsio/specmatic/releases/download/0.72.0/specmatic.jar /specmatic/specmatic.jar
RUN chmod +x /specmatic/specmatic.jar

WORKDIR /app

ENTRYPOINT ["java", "-jar", "/specmatic/specmatic.jar"]

Here’s what each line does:

Next, we need to build the Docker image based on the above Dockerfile:

# Run inside the directory you created the above Dockerfile.
docker build -t specmatic .

specmatic is the name of the Docker image. You can choose the name freely, but you must use whatever you specify here at the end of the alias in the next chapter.

Creating an alias to run the Docker command

The Docker command used to run the image can be long and complicated, so creating an alias is handy. Here’s an example:

alias specmatic='docker run --rm -it -v $HOME/.ssh:/root/.ssh -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}/:/app -p 9000:9000 specmatic'

Let’s break down the parameters in this command:

Once we’ve defined the alias, using it is as simple as typing specmatic followed by the command we want to execute in our terminal. That way, we run the Docker container and execute the specified command within the container environment.

For instance, if we want to test a service using a specmatic command, we can do so by navigating to the directory containing the project we want to contract test and run:

specmatic test service.yaml

In this example, test is the specmatic command, and service.yaml is the argument to that command. The alias will run the Docker container and execute the specmatic test on service.yaml within the container. This approach allows us to use Java CLI tools like Specmatic without installing their respective language environments on our local machine.

Wrapping it up

Docker offers an elegant solution for running CLI tools written in languages you don’t want to install on your computer. Creating a Docker image with all the necessary dependencies allows you to run your tools in an isolated environment, keeping your local system clean and uncluttered.

We’ve seen how to create a Dockerfile for a Java CLI tool and how to define a handy alias for running Docker commands. This approach is not only limited to Java; you can use it for any language or tool that can run inside a Docker container.

So, the next time you encounter a useful CLI tool written in a language you don’t have installed locally, don’t let that stop you. With Docker, you can run any tool without installing anything extra.