Backend Entity hostname when using docker compose

I’m using the official docker image and the docker-compose.yml configuration suggested in the docs.

When I do docker compose up, it creates a new backend entity with a hostname that is equivalent to the container id.

If I do docker compose down and then docker compose up again, an additional backend entity is created, and the previous backend entity still remains. The new backend entity has the name and hostname of the new container ID.

I’m looking for some way to change to way it is run so that I don’t end up with multiple backend entities from bringing the containers up and down. I’ve tried including a backend.yml file and specifying the name and hostname, but it seems like it has taken no effect.

Here is an example of a backend.yml file I tried.

edit: I've removed the example backend.yml file because it was nonsense and unhelpful, and I don't want to confuse anyone who stumbles over this thread.

I know it is nonsense, but I made this more in an effort to see if it took effect rather than trying to make something that actually worked.

The backend.yml file is being put into the docker container at /etc/sensu/backend.yml and I am just using sensu-backend start as the command, since the documentation says that it should read from /etc/sensu/backend.yml in the event that no command line flags are added on execution.

I’d also settle for a way to have the old backend entities just removed automatically or something.

Any ideas?

1 Like

I feel like maybe I’ve mixed up docs from different versions of Sensu or something maybe?

I’ve tried overwriting the SENSU_HOSTNAME environment variable and it doesn’t seem to have taken any affect.

I got it!

The backend entity name is derived from the machine’s hostname.

In your docker-compose file, you can specify that hostname you want for the container.

version: "3"
services:
  sensu-backend:
    hostname: sensu-docker-backend
    ports:
    - 3000:3000
    - 8080:8080
    - 8081:8081
    volumes:
    - "sensu-backend-data:/var/lib/sensu/sensu-backend/etcd"
    command: "sensu-backend start --state-dir /var/lib/sensu/sensu-backend --log-level debug"
    environment:
    - SENSU_BACKEND_CLUSTER_ADMIN_USERNAME=sensu-dev
    - SENSU_BACKEND_CLUSTER_ADMIN_PASSWORD=secretp4ss!
    image: sensu/sensu:latest

volumes:
  sensu-backend-data:
    driver: local

With the above docker-compose.yml file, it will create a backend entity with the hostname “sensu-docker-backend”

The environment variable you were looking for is SENSU_NAME, but as you also discovered, the agent will use the host OS hostname as the default entity name. See the Sensu Agent reference documentation under the “name” attribute for more information:

In fact, the Sensu Agent and backend support quite robust configuration via environment variables:

Instead of using configuration flags, you can use environment variables to configure your Sensu agent. Each agent configuration flag has an associated environment variable. […] All environment variables that control Sensu agent configuration begin with SENSU_. To rename a configuration flag you wish to specify as an environment variable, prepend SENSU_, convert dashes to underscores, and capitalize all letters. For example, the environment variable for the flag api-host is SENSU_API_HOST.

tl;dr: agent names can be configured via the CLI, environment variable, or config file:

  • the sensu-agent --name sensu-docker-backend flag is the same as…

  • the SENSU_NAME=sensu-docker-backend environment variable, which is the same as…

  • the following /etc/sensu/agent.yml contents.

    ---
    name: sensu-docker-backend
    

The same rule of thumb applies to all sensu-agent and sensu-backend configuration.

I hope this helps! :blush:

1 Like