Tag: containers

Docker Container Status as Prometheus Exporter Metrics

Reading time: 2 – 2 minutes

Tracking Docker container status in real time is a common challenge in DevOps. Popular tools like cAdvisor and the default Docker exporter for Prometheus often lack direct metrics for container states, meaning key insights—such as the number of containers that are running, stopped, or inactive—require complex workarounds. This limitation can complicate monitoring and lead to unreliable data.

Before creating docker_container_exporter, I relied on complex Prometheus queries to retrieve container statuses. This often involved calculations based on the last time a container was seen as active, but this approach had a major flaw: if the query time range didn’t match the last activity timestamp precisely, the data could be inaccurate or incomplete. Monitoring container states shouldn’t be this difficult.

With docker_container_exporter, this problem is solved. My tool captures real-time Docker container statuses, providing data on the number of running, stopped, and other container states, all in a Prometheus-compatible format. You can collect these metrics through a standard Prometheus polling process, or use agents like Grafana Alloy to push the data to Prometheus storage or compatible DB servers like Grafana Mimir or Thanos.

You can find my project in this GitHub repository: docker_container_exporter

In the README file, you’ll find details on how to use it, as well as instructions for integrating it with Grafana Alloy.

The Power of the .env File in Docker Compose

Reading time: 30 – 50 minutes

In this post, I’ll demonstrate how powerful and flexible the .env file can be when setting up a compose.yaml for Docker Compose. This approach allows for easy management of environment variables, making your Docker configurations more dynamic and manageable.

Let’s start with a simple .env file:

UBUNTU_VERSION=24.04

And a corresponding compose.yaml file:

services:
  test:
    image: ubuntu:${UBUNTU_VERSION}
    command: ["sh", "-c", "env"]

When you run the Docker Compose stack with the command docker compose up, you’ll see an output like this:

$ docker compose up
[+] Running 2/1
  Network tmp_default   Created                                                                          0.1s
  Container tmp-test-1  Created                                                                          0.1s
Attaching to test-1
test-1  | HOSTNAME=f9002b77bc79
test-1  | HOME=/root
test-1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test-1  | PWD=/
test-1 exited with code 0

However, to make the variables defined in the .env file available within the Docker container, you need to add a couple of lines to your compose.yaml file:

services:
  test:
    image: ubuntu:${UBUNTU_VERSION}
    command: ["sh", "-c", "env"]
    env_file:
      - .env

After updating the compose.yaml file, run the docker compose up command again. This time, you’ll notice that the UBUNTU_VERSION environment variable is now included in the container’s environment:

$ docker compose up
[+] Running 1/0
  Container tmp-test-1  Recreated                                                                        0.1s
Attaching to test-1
test-1  | HOSTNAME=069e3c4a4413
test-1  | HOME=/root
test-1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test-1  | UBUNTU_VERSION=24.04
test-1  | PWD=/
test-1 exited with code 0

This is incredibly convenient because maintaining the .env file allows you to easily manage environment variables across different services without modifying the compose.yaml file each time. This example clearly illustrates how powerful and useful it is to use .env files in Docker Compose configurations.

Get the IP addresses of local Docker containers

Reading time: 13 – 21 minutes

We have Docker running with containers that are connected to their own private network. To efficiently manage and monitor these containers, it’s often useful to retrieve their private IP addresses.

With the following command, you can easily obtain the private IP addresses of all running Docker containers:

sudo docker inspect $(docker ps -q) --format='{{ printf "%-50s" .Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' | sort -t. -k2,2n -k3,3n -k4,4n

Output example:

$ sudo docker inspect $(docker ps -q ) --format='{{ printf "%-50s" .Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' | sort -t. -k2,2n -k3,3n -k4,4n
/rproxy                                            10.3.10.2
/n8n                                               10.3.10.4
/semaphore                                         10.3.10.6
/code                                              10.3.10.7
/ssh                                               10.3.10.9
/nodered                                           10.3.10.11
/pihole_opendns                                    10.3.10.23
/pihole_googledns                                  10.3.10.24

Scroll to Top