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

  You block advertising 😢
Would you like to buy me a ☕️ instead?

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:

  • FROM amazoncorretto:21-alpine: This line specifies the base image. In this case, we’re using an image with OpenJDK 21 installed on Alpine Linux.

  • WORKDIR /specmatic: Next, we set the working directory in the container to /specmatic.

  • ADD https://github.com/znsio/specmatic/releases/download/1.2.14/specmatic.jar /specmatic/specmatic.jar: Here we download the specmatic.jar file into the /specmatic directory in the container.

  • RUN chmod +x /specmatic/specmatic.jar: This line makes the downloaded .jar file executable.

  • WORKDIR /app: The /app directory will be the mounting point for our local file system.

  • ENTRYPOINT ["java", "-jar", "/specmatic/specmatic.jar"]: The ENTRYPOINT specifies the command that Docker will run every time a we start a container by running a CLI command. In this case, we run the specmatic.jar file, a contract testing tool.

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:

  • --rm: This option tells Docker to remove the container when it exits.

  • -it: Next, we tell Docker to run the container in interactive mode with a tty attached.

  • -v $HOME/.ssh:/root/.ssh: In case the CLI tool needs to access SSH keys, we mount the .ssh directory from our home directory to the /root/.ssh directory in the container.

  • -v /var/run/docker.sock:/var/run/docker.sock: This option allows the CLI tool to interact with the Docker daemon on your host machine.

  • -v ${PWD}/:/app: The -v or --volume option mounts the current working directory (${PWD}) on our host machine to the /app directory in the container.

  • -p 9000:9000: In this example, Specmatic runs a web server, so we expose port 9000 from the container to port 9000 on our host machine.

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.


Do you want to learn how to build advanced Vue.js applications?

Register for the Newsletter of my upcoming book: Advanced Vue.js Application Architecture.



Do you enjoy reading my blog?

You can buy me a ☕️ on Ko-fi!

☕️ Support Me on Ko-fi