Development Environment Setup Using Docker
The following steps outline the configuration of Docker for the development environment of the screenly/balena-prometheus-exporter
project. This will help maintain a clean separation between development and production requirements.
Step 1: Create the Dockerfile
The Dockerfile
is essential for containerizing the application. Below is the code for the Dockerfile used in the development environment:
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" ]
Explanation:
- FROM python:3.10-alpine: The image is based on Alpine Linux with Python 3.10 installed to keep the image lightweight.
- WORKDIR /usr/src/app: Sets the working directory in the container.
- COPY requirements.txt ./: Copies the
requirements.txt
file into the container for dependency installation. - RUN pip install –no-cache-dir -r requirements.txt: Installs the required Python packages without caching.
- COPY main.py .: Copies the main application script into the container.
- USER nobody: Runs the application as a non-privileged user for security reasons.
- CMD [ “python”, “./main.py” ]: Specifies the command to run when the container starts, executing the main script.
Step 2: Define Dependencies
The requirements.txt
file should contain all necessary packages for the exporter to function. Below is the content of the requirements.txt
file:
certifi==2024.7.4
charset-normalizer==3.1.0
idna==3.4
prometheus-client==0.16.0
requests==2.31.0
urllib3==1.26.18
Step 3: Building the Docker Image
To build the Docker image, navigate to the directory containing the Dockerfile and execute the following command:
$ docker build -t balena-exporter .
Explanation:
- docker build: Command to build the Docker image.
- -t balena-exporter: Tags the image with the name
balena-exporter
. - .: Indicates the current directory as the context for the Docker build.
Step 4: Running the Docker Container
Once the image is built, the application can be run in a container with the following command:
$ docker run -d \
--name balena-exporter \
-p 8000:8000 \
-e BALENA_TOKEN= \
balena-exporter
Explanation:
- docker run -d: Runs the container in detached mode.
- –name balena-exporter: Assigns a name to the container for easier management.
- -p 8000:8000: Maps port 8000 of the container to port 8000 of the host, allowing access to the application.
- -e BALENA_TOKEN=: Sets the
BALENA_TOKEN
environment variable necessary for the application’s authentication. This token must be set appropriately. - balena-exporter: Specifies which image to run.
Step 5: Verifying the Exporter
To ensure the exporter is functioning correctly, perform a curl
command to the mapped port:
curl http://localhost:8000
This should return the metrics exposed by the exporter.
Optional Environment Variables
The application has an optional CRAWL_INTERVAL
environment variable that controls how frequently the application queries the Balena API. By default, this is set to 60 seconds. To set this variable, include it in the docker run
command as shown below:
-e CRAWL_INTERVAL=30
This example sets the crawl interval to 30 seconds instead of the default.
Source
- Dockerfile, main.py, requirements.txt from screenly/balena-prometheus-exporter.