NashTech Insights

Scaling K6 Tests with Docker Swarm

Sujeet Kumar Srivastava
Sujeet Kumar Srivastava
Table of Contents
woman sitting while operating macbook pro

Hi Folks,
In today’s software development era, load testing has become an essential practice for ensuring the performance and scalability of product. k6 is a popular load testing tool that provides testers a powerful and easy way to conduct load tests. On the other hand when we combine it with Docker Swarm, a container orchestration tool then it becomes even easier to distribute and scale load tests across multiple machines. In this blog, we will explore how to run k6 tests using Docker Swarm, enabling you to simulate realistic loads on your application.


To follow along with this blog, you should have a basic understanding of Docker and Docker Swarm. Additionally, ensure that Docker and Docker Compose are installed on your machine.

Step 1 : Write Your k6 Test Script

Before we dive into running k6 tests with Docker Swarm, let’s start by creating a simple k6 test script. Create a new file then name it as test.js(extension should be .js) and add the following code:

import { check } from 'k6';
import http from 'k6/http';

export const options = {
    vus: 50,
    duration: '10s',

export default function () {
  const res = http.get('');
  check(res, {
    'is status 200': (r) => r.status === 200,

This script performs a GET request to and then injects 50 virtual users in duration of 10 seconds. Feel free to modify the script according to your load testing requirements.

Step 2 : Create Your Dockerfile

Now create a Dockerfile to build a Docker image for running the k6 tests. Below is the example of a Dockerfile that i will be using in this blog:

# Using the official k6 Docker image as the base image
FROM loadimpact/k6

# Copying the k6 test script(s) to the container
COPY test.js /test.js

# running the k6 test
CMD ["run", "/test.js"]

Save the Dockerfile in the same directory as the test script.

Step 3 : Build the Docker Image

Open a terminal or command prompt, navigate to the directory containing the Dockerfile and the test script, and build the Docker image using the following command:

sudo docker build -t k6-test .

This command builds the Docker image and tags it with the name “k6-test.”

Step 4 : Create a Docker Swarm Cluster

Before deploying services with Docker Swarm, we need to set up a Docker Swarm cluster. Initialize it by running the following command:

sudo docker swarm init

Step 5 : Deploy k6 test as a service in Docker Swarm

To deploy the k6 service using Docker Swarm, run the following command:

sudo docker service create --name k6-test --replicas 1 k6-test

This command creates a service named “k6-test” with 1 replica. You can adjust the number of replicas based on your requirements.

Step 6 : Monitor the test

  • Once the service is running, Docker Swarm will automatically distribute it across the nodes in the Swarm.
  • To monitor the test execution, you can view the logs of the running k6 service replicas. Use the below command for checking the service logs:
sudo docker service logs k6-test

This command displays the logs of the k6 service in real-time.

That’s it! You now have a Docker Swarm setup running with 1 replica of the k6 test script.

In this example, we used a simple k6 script and deployed 1 replica only. You can modify the test script, adjust the number of replicas, and configure additional k6 options based on your specific testing needs.


By combining the power of k6 and Docker Swarm, you can easily orchestrate and distribute load tests across multiple machines, simulating realistic workloads. In this tutorial, we walked through the process of running a k6 test script as a Docker service in a Swarm cluster and scaling the service to simulate higher loads. Additionally, we explored how to monitor the load test using the built-in k6 metrics server. Now, you can efficiently conduct load tests on your applications and ensure their performance and scalability.

Sujeet Kumar Srivastava

Sujeet Kumar Srivastava

I am a seasoned automation testing professional having sound knowledge of automation testing methodologies and tools. I have good understanding in designing and implementing test frameworks, creating test plans, and executing automated tests across multiple platforms and devices. I am always charged up for picking up & learning new things. On a personal front, I am fitness enthusiast and loves to play volleyball.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: