Feather Background Waves Background
Skip to main content
Feather Background Waves Background
Feather Background Waves Background
  1. JMeter in English/

JMeter Docker (containers)

advanced - This article is part of a series.
Part 1: This Article

docker

In this article, we’ll cover running JMeter inside a container using the Docker platform. This post is essential to understand the final step of distributing JMeter using containers. Otherwise, this post would be quite long, and I prefer to simplify the content. Without further ado, let’s begin.

What is a Container?>

What is a Container? #

Basically, it’s a canned product where the software necessary to run one or more processes is installed. The great advantage of containers is that they are portable, and you can isolate entire execution environments in a single file. In our particular case, we’re going to create a container with everything we need to run the JMeter scripts. We need:

  • Operating System: Linux Alpine (the king of compact Linux)
  • Java: JDK or JRE 1.8+
  • JMeter: JMeter 5.2.1

The JMeter JMX script and/or support files will be outside the container for now, but they may also be inside. For more information, I recommend visiting this link: What is a container?.

What is Docker?>

What is Docker? #

Docker is a software platform that allows us to manage, create, delete, and run our container image. Docker, like JMeter, is open source, so we don’t need to pay a license fee to use it. However, it’s always a good idea to make donations to free software foundations to continue promoting their development. You can find a good explanation of What is Docker? here.

How does it work?>

How does it work? #

Let’s create a container image from a Dockerfile file, this file will contain the information and configuration needed to download Alpine Linux, install Java JRE 1.8+, download and/or unzip JMeter. Afterwards we’ll run some shell scripts to initialize the container and run our test. At the end I’ll mention how we can do the same but including JMeter plugins.

Resumen del Proceso>

Resumen del Proceso #

1.- Install Docker>

1.- Install Docker #

First, we must install Docker desktop on our computer. I invite you to download and install the latest available version, then wait for the relevant processes to start to enable the CLI. You can validate that you have Docker installed in the command line like this:

docker --version
Docker version 19.03.8, build afacb8b
2.- Clone the repository>

2.- Clone the repository #

I think I forgot to mention it, but we also need GIT to clone the following project:

git clone https://github.com/daeep/JMeter_Docker.git
cd JMeter_Docker
3.- Compile the image>

3.- Compile the image #

Let’s compile the Dockerfile file, which is located in our JMeter_Docker folder, by simply running the shell script ./build.sh.

docker-start

docker-end

4.- Validation>

4.- Validation #

Also from this same folder, we’ll run the example to validate that everything is working correctly by invoking the shell script ./example.sh. If it asks you for the password, it’s because the Docker commands stop, remove, and start are being executed using sudo, and this requires administrator permissions. The shell script will generate output like this:

docker-run

If you get similar output, congratulations!

5.- Execution>

5.- Execution #

Let’s copy our JMX file to the JMeter_Docker/test folder, but first I invite you to parameterize your JMX script following the instructions described here, let’s assume that your script is charismatically called antonio.jmx and that we want to execute it with 50 threads, 50 seconds of ramp and with a duration of 10 minutes or 600 seconds. We run it using the shell script ./run.sh in the JMeter_Docker folder as follows:

./run.sh -n -t test/antonio.jmx -l test/antonio-50.jtl -j test/antonio-50.log -Jthreads=50 -Jrampup=50 -Jduration=600

You should have an output similar to the one above. If so, you’ve done it.

JMeter with Plugins, starting from step 2>

JMeter with Plugins, starting from step 2 #

6.- Clone the repository>

6.- Clone the repository #

If your script requires JMeter plugins, these can also be downloaded from the cloud, but we can’t cover this endless number of variants for each plugin. The best option is to use our local version of JMeter, since it already works for the script we built, right? Let’s assume this is true and that we already have the plugins downloaded locally. First, let’s clone a different project:

git clone https://github.com/daeep/JMeter_Local_Docker.git
cd JMeter_Local_Docker
7.- Install plugins and zip>

7.- Install plugins and zip #

Then follow the instructions to install the JMeter plugins and inside the JMeter_Local_Docker/test folder is the JMeter_Docker_Script_Plugins.jmx file. Open it and the plugins will automatically download the Dummy Sampler plugin, which is necessary to run this JMX. Let’s compress our JMeter folder. This can be done using a tarball or zip. Here’s an example of both:

tar -czvf apache-jmeter-5.2.1.tgz apache-jmeter-5.2.1

zip -r apache-jmeter-5.2.1.zip apache-jmeter-5.2.1 //alternatively
8.- Copy the binaries to the image>

8.- Copy the binaries to the image #

Now we proceed to copy the compressed file to the JMeter_Local_Docker folder. If you performed step #C-2 with the name as indicated, you don’t have to do anything other than build it; skip to step #C-4.

If you changed the file and/or folder name, you’ll need to do the same in section #3 of the Dockerfile. The value of the ARG JMETER_FILE argument must correspond to the name of the apache-jmeter-5.2.1.tgz or apache-jmeter-5.2.1.zip file, as appropriate. The value of the ARG JMETER_FOLDER argument must also correspond to the name of the folder. Finally, if you used the zip utility, we must also modify the decompression line used by the tar -zvxf command to unzip.

9.- Compile the image>

9.- Compile the image #

Now let’s use shell scripting again ./build.sh to build our image. If everything goes correctly, we can run the sample test with the command ./example.sh.

docker-plugin-execution

Now you can do the same as step #5 with your plugin script. Congratulations!

Conclusion>

Conclusion #

There are actually few differences between the two Dockerfiles, whether they are projects with or without plugins. I’ve included the lines with the differences below. For this article, I based my work on the Git project by Just van den Broecke, which is available here. I made some modifications to the original version, but I still invite you to check out his project. As an additional note, Docker assigns 2 GB of memory to containers by definition. If you want to run tests for educational purposes with more threads, I recommend increasing this value.

JMeter Docker Image in YAML: Without Plugins vs With Plugins>

JMeter Docker Image in YAML: Without Plugins vs With Plugins #

-----------------------------------------------------------------------------

 # 3 Copy local JMeter to the image and set environment variables
 ARG JMETER_FILE="apache-jmeter-5.2.1.tgz"   //Definimos el archivo comprimido
 ARG JMETER_FOLDER="apache-jmeter-5.2.1"     //Definimos la carpeta
 COPY ${JMETER_FILE} /tmp/

 # 4 Update & Upgrade, then decompress local JMeter and delete tar ball file
 RUN apk update \
  && apk upgrade \
  && apk add ca-certificates \
  && update-ca-certificates \
  && apk add --update openjdk8-jre tzdata curl unzip bash \
  && apk add --no-cache nss \
  && rm -rf /var/cache/apk/* \
  && mkdir -p ${JMETER_HOME} \
  && tar -zvxf /tmp/${JMETER_FILE} -C /home/jmeter/ \  //Copiamos los binarios a la imagen
  && rm -f /tmp/${JMETER_FILE}

 -----------------------------------------------------------------------------

 # 3 Set JMeter version 5.2.1 for downloading and some ENV variables
 ENV JMETER_VERSION 5.2.1
 ENV JMETER_FOLDER /home/jmeter/
 ENV JMETER_DOWNLOAD https://archive.apache.org/dist/jmeter/binaries/apache-jmeter-${JMETER_VERSION}.tgz
  //Definimos la ruta donde estan los binarios comprimidos de JMeter en la nube
 # 4 Update, upgrade, download Java JRE and JMeter then decompress it
 RUN apk update \
  && apk upgrade \
  && apk add ca-certificates \
  && update-ca-certificates \
  && apk add --update openjdk8-jre tzdata curl unzip bash \
  && apk add --no-cache nss \
  && rm -rf /var/cache/apk/* \
  && mkdir -p /tmp/jmeter/ \
  && curl -L --silent ${JMETER_DOWNLOAD} > /tmp/jmeter/apache-jmeter-${JMETER_VERSION}.tgz \ //Descargamos JMeter
  && mkdir -p ${JMETER_FOLDER} \
  && tar -xzf /tmp/jmeter/apache-jmeter-${JMETER_VERSION}.tgz -C ${JMETER_FOLDER}  \ //Descomprimir JMeter
  && rm -rf /tmp/jmeter
  
 -----------------------------------------------------------------------------


advanced - This article is part of a series.
Part 1: This Article