Interacting with applications

When your application is prepared in containers you need to provide a starting
point or set of actions that the application is going to process whilst our
metric providers measure and read the metrics during this lifecycle.

In our terminology we call this a flow.

In the simplest case this is just a start command that will kick of some workload
in one or more of the containers.

In a more complex form it is some kind of representation of a standard usage scenario
that the application will experience in production.

Here we will guide you through one simple example with
only one container that executes a terminal command.

Then we will show you one more complex example with an orchestration of
three containers, where we simulate a user that accesses a web application.

The result will be a usage_scenario.yml file. (Specification →)

Command line app

We will make the most basic example our tool can handle:

  • Base off of alpine base container.
  • Add the package stress-ng from apk
  • Just run a 5 seconds stress run with default metrics providers

This is the resulting usage_scenario.yml file:

name: Stress Container One Core 5 Seconds
author: Arne Tarara
description: Stresses one core for 5 seconds
architecture: linux
services:
  simple-load-container:
    image: alpine
    setup-commands:
      - command: apk add stress-ng

flow:
  - name: Stress
    container: simple-load-container
    commands:
      - type: console
        command: stress-ng -c 1 -t 5
        note: Starting Stress

The orchestration in the services part uses the standard alpine image and
just installs an additional package into it: stress-ng

In the flow part we tell the container in the commands array that it shall
run a command of type console and then provide the same string that we would type
in directly if we were running a bash shell (or similar).

This example is representative of machine learning applications where you typically
fire off one command (either train or infer) and then the applications runs one time
until it finishes.

Web app

In the sample for the web app we are reusing many parts of the compose.yml file from:
Containerizing applications →

We then add a flow part where we utilize a headless browser, controlled via Playwright,
to browse some sample subsites of the web application.

The browser container does not need to be defined by you. GMT ships a ready-made helper
that you include with compose-file: !include-gmt-helper gmt-playwright.yml. It provides the
gmt-playwright-nodejs container with a running browser. See Measuring Websites →
for all details and the available helper variants (e.g. with a caching reverse proxy).

It is important to note that the image names we use here in this example are
local docker images on our system, therefore the names can be different
on your system and application.

The names in this example follow the naming in the example from containerizing applications →

name: Wordpress Example Flow
author: Arne Tarara
description: Browses some sample subsites of the Wordpress demo application

compose-file: !include-gmt-helper gmt-playwright.yml

networks:
  example-network:
services:
  db-container:
    image: demo_app_db
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    networks:
      - example-network
  wordpress-container:
    image: demo_app_wordpress
    ports:
      - 9875:9875
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
    networks:
      - example-network
  # The gmt-playwright-nodejs service comes from the GMT helper included above.
  # We only need to attach it to our network so it can reach the wordpress container.
  gmt-playwright-nodejs:
    networks:
      - example-network

flow:
  - name: Check Website
    container: gmt-playwright-nodejs
    commands:
      - type: playwright
        command: await page.goto("http://wordpress-container:9875", { waitUntil: "networkidle" })
        note: Opening Home Page
      - type: playwright
        command: await page.click("a[href='http://wordpress-container:9875/?page_id=34']")
        note: Opening Contact Page
      - type: console
        command: sleep 30
        note: Idling
      - type: playwright
        command: await page.goto("http://wordpress-container:9875", { waitUntil: "networkidle" })
        note: Opening Home Page again

Let’s drill down on what is happening in this usage_scenario.yml:

  • First the GMT Playwright helper is included via compose-file. It adds the browser container and starts the browser before the flow begins
  • Then the network is set up
  • Then all services are defined and on which network they can communicate
    • This results in three services:
      • db-container: Our mariadb database based off a local image (Server side)
      • wordpress-container: Our Wordpress based off a local image (Server side)
      • gmt-playwright-nodejs: The headless browser provided by the GMT helper (client side). We only add the network to it
  • Then a flow is defined which sends Playwright commands to the headless browser
    • The pages are browsed one time, then a 30 second sleep occurs and then the home page is opened again
      • The reason being that this simulates a user more typically. You browse, you read, your browse again … etc.

If you already have Playwright end-to-end tests you can directly reuse the commands from them
as type: playwright steps in the flow.

Communicating with containers inside the network

It is important that inside of your commands you use the names of the containers as hostnames,
because otherwise they cannot be found through the network.

The hostname to find the Wordpress container in this example would be wordpress-container. In our example we also let it listen on port 9875 and it only accepts HTTP traffic.

A connect call would therefore be needed to be issued to: http://wordpress-container:9875

For more on Playwright commands, the helper variants and caching see Measuring Websites →