FROM python:3.10-alpine

WORKDIR /usr/src/app

COPY requirements.txt ./ RUN pip install –no-cache-dir -r requirements.txt

COPY main.py .

USER nobody

CMD [ “python”, “./main.py” ]


          The `Dockerfile` defines the steps for building the Docker image for the Balena Prometheus exporter. It leverages a Python 3.10 Alpine base image and sets the working directory to `/usr/src/app`.  The `requirements.txt` file, which lists Python dependencies, is copied and used for installation. The `main.py` script, containing the core logic of the exporter, is also copied. The `USER` directive sets the container user to `nobody` for security reasons. The `CMD` instruction sets the default command to run the Python script.
          
          ### Building the Docker Image
          
          To build the Docker image, use the following command:
          
          ```bash
          $ docker build -t balena-exporter .
          

This command builds the image using the Dockerfile in the current directory and tags it with the name balena-exporter.

Running the Docker Container

The following command runs the built Docker container:

$ docker run -d \
          --name balena-exporter \
          -p 8000:8000 \
          -e BALENA_TOKEN= \
          balena-exporter
          
  • -d: Runs the container in detached mode (background).
  • --name balena-exporter: Assigns the name balena-exporter to the container.
  • -p 8000:8000: Maps port 8000 from the host to port 8000 inside the container.
  • -e BALENA_TOKEN=: Sets the BALENA_TOKEN environment variable, which is required for authentication with the Balena Cloud API. Replace with your actual Balena Cloud API token.
  • balena-exporter: The name of the Docker image to run.

Environment Variables

The following environment variables can be set:

  • BALENA_TOKEN: Your Balena Cloud API token, required for authentication. Source: main.py
  • CRAWL_INTERVAL: The interval (in seconds) at which the exporter collects metrics from the Balena Cloud API. Defaults to 60 seconds.

Port Mapping

The container exposes port 8000, which is mapped to the host’s port 8000. You can use curl to verify the results:

curl localhost:8000
          

Top-Level Directory Explanations

tests/ - This directory contains all the unit and integration tests for the project. It includes the __init__.py file which makes it a package, and specific test files like test_exporter.py.