docker.recipes

k6 Load Testing

intermediate

Modern load testing tool with JavaScript.

Overview

k6 is a modern load testing tool built by Grafana Labs that revolutionizes performance testing with JavaScript-based test scripts and developer-centric design. Unlike traditional tools like JMeter or LoadRunner, k6 emphasizes simplicity, version control compatibility, and CI/CD integration, making it the preferred choice for DevOps teams and developers who need to embed performance testing into their development workflows. Its lightweight architecture and ES6 JavaScript support enable teams to write maintainable test scripts that can be easily shared and evolved alongside application code. This stack combines k6's testing capabilities with InfluxDB's time-series data storage and Grafana's visualization power to create a comprehensive performance testing observatory. k6 generates detailed metrics during load tests and streams them directly to InfluxDB, where time-stamped performance data is efficiently stored and indexed. Grafana then transforms this raw data into interactive dashboards showing real-time response times, throughput, error rates, and custom business metrics, enabling teams to spot performance bottlenecks as they happen rather than analyzing post-test reports. Development teams building APIs, microservices, or web applications will find this combination invaluable for establishing performance baselines, regression testing, and capacity planning. The JavaScript-based approach means frontend developers can contribute to performance testing using familiar syntax, while the real-time visualization helps stakeholders understand system behavior under load without technical expertise. This stack particularly shines in environments where performance testing needs to be automated, repeatable, and integrated into continuous delivery pipelines.

Key Features

  • JavaScript ES6 test scripts with full module support and modern syntax
  • Real-time metrics streaming to InfluxDB with automatic time-series indexing
  • Grafana dashboards showing live performance data during test execution
  • Built-in HTTP, WebSocket, and gRPC protocol support for modern applications
  • Scenario-based testing with ramp-up, constant load, and spike patterns
  • Threshold definitions for automatic pass/fail criteria in CI/CD pipelines
  • InfluxDB data retention policies for managing long-term performance history
  • Grafana alerting on performance degradation with Slack, email, and webhook notifications

Common Use Cases

  • 1API performance regression testing in CI/CD pipelines before production deployment
  • 2Microservices capacity planning to determine optimal instance counts and resource allocation
  • 3E-commerce platform load testing during peak shopping seasons and flash sales
  • 4SaaS application performance monitoring across different subscription tiers and usage patterns
  • 5Game server stress testing to validate concurrent player capacity and matchmaking performance
  • 6Financial trading system latency testing to ensure sub-millisecond response requirements
  • 7IoT backend testing to simulate thousands of concurrent device connections and data streams

Prerequisites

  • Minimum 1GB RAM available for InfluxDB time-series data processing and storage
  • Port 3000 available for Grafana dashboard access and 8086 for InfluxDB API
  • Basic JavaScript knowledge for writing and modifying k6 test scripts
  • Understanding of HTTP protocols, status codes, and API testing concepts
  • Target application or service URLs accessible from the Docker host network
  • Familiarity with performance testing metrics like RPS, response time percentiles, and error rates

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 k6:
3 image: grafana/k6:latest
4 container_name: k6
5 volumes:
6 - ./scripts:/scripts
7 environment:
8 K6_OUT: influxdb=http://influxdb:8086/k6
9
10 influxdb:
11 image: influxdb:1.8
12 container_name: k6-influxdb
13 restart: unless-stopped
14 environment:
15 INFLUXDB_DB: k6
16 volumes:
17 - k6_influxdb:/var/lib/influxdb
18 ports:
19 - "8086:8086"
20
21 grafana:
22 image: grafana/grafana:latest
23 container_name: k6-grafana
24 restart: unless-stopped
25 environment:
26 GF_AUTH_ANONYMOUS_ENABLED: "true"
27 GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
28 volumes:
29 - k6_grafana:/var/lib/grafana
30 ports:
31 - "3000:3000"
32 depends_on:
33 - influxdb
34
35volumes:
36 k6_influxdb:
37 k6_grafana:

.env Template

.env
1# Create test scripts in ./scripts

Usage Notes

  1. 1Docs: https://k6.io/docs/
  2. 2Run: docker-compose run k6 run /scripts/test.js
  3. 3Write tests in JavaScript/TypeScript (ES6 modules)
  4. 4Grafana dashboard at http://localhost:3000 for real-time metrics
  5. 5Built-in checks, thresholds, and scenarios support
  6. 6Cloud integration available for distributed testing

Individual Services(3 services)

Copy individual services to mix and match with your existing compose files.

k6
k6:
  image: grafana/k6:latest
  container_name: k6
  volumes:
    - ./scripts:/scripts
  environment:
    K6_OUT: influxdb=http://influxdb:8086/k6
influxdb
influxdb:
  image: influxdb:1.8
  container_name: k6-influxdb
  restart: unless-stopped
  environment:
    INFLUXDB_DB: k6
  volumes:
    - k6_influxdb:/var/lib/influxdb
  ports:
    - "8086:8086"
grafana
grafana:
  image: grafana/grafana:latest
  container_name: k6-grafana
  restart: unless-stopped
  environment:
    GF_AUTH_ANONYMOUS_ENABLED: "true"
    GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
  volumes:
    - k6_grafana:/var/lib/grafana
  ports:
    - "3000:3000"
  depends_on:
    - influxdb

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 k6:
5 image: grafana/k6:latest
6 container_name: k6
7 volumes:
8 - ./scripts:/scripts
9 environment:
10 K6_OUT: influxdb=http://influxdb:8086/k6
11
12 influxdb:
13 image: influxdb:1.8
14 container_name: k6-influxdb
15 restart: unless-stopped
16 environment:
17 INFLUXDB_DB: k6
18 volumes:
19 - k6_influxdb:/var/lib/influxdb
20 ports:
21 - "8086:8086"
22
23 grafana:
24 image: grafana/grafana:latest
25 container_name: k6-grafana
26 restart: unless-stopped
27 environment:
28 GF_AUTH_ANONYMOUS_ENABLED: "true"
29 GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
30 volumes:
31 - k6_grafana:/var/lib/grafana
32 ports:
33 - "3000:3000"
34 depends_on:
35 - influxdb
36
37volumes:
38 k6_influxdb:
39 k6_grafana:
40EOF
41
42# 2. Create the .env file
43cat > .env << 'EOF'
44# Create test scripts in ./scripts
45EOF
46
47# 3. Start the services
48docker compose up -d
49
50# 4. View logs
51docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/k6/run | bash

Troubleshooting

  • k6 'connection refused' to target: Ensure target URLs are accessible from container network and not using localhost references
  • InfluxDB 'database does not exist' error: Verify INFLUXDB_DB environment variable matches k6 output configuration database name
  • Grafana shows no data sources: Check InfluxDB container is running and accessible on port 8086 before configuring data source
  • k6 scripts fail with module errors: Ensure ES6 import/export syntax is used correctly and modules are in accessible paths
  • High memory usage during tests: Reduce virtual user count or implement proper response body handling to prevent memory leaks
  • InfluxDB disk space issues: Configure data retention policies to automatically remove old metrics data after specified time periods

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space