Skip to content

Running Docker Commands with Administration Scripts

Video Lecture

Running Docker Commands with Administration Scripts

Description

Note

This lesson was from the Zabbix 5 version of my course, which I've now made this video available to view for free. For Zabbix 6 and 7 you will also need to manage the AllowKey option in the agent configuration.

Running Docker Commands with Zabbix Administration Scripts

In this video, I demonstrate how to use administration scripts to manage docker containers.

During the configuration I encounter several issues in which I solve them all.

  1. It's important that the proxy or server process that you want to use to run the script from, needs EnableRemoteCommands=1 set in its config file. Then restart the proxy or server service.

  2. If the command you execute needs privileged permissions, then run it with the sudo prefix.

  3. To allow the Zabbix user to use the sudo prefix, add a new line to the sudoers file.

sudo visudo

Add

zabbix ALL=(ALL) NOPASSWD: /usr/bin/docker

With these 3 things, I was able to execute docker commands on my host using the administration scripts option.

Those commands were,

sudo docker ps -a
sudo docker stats -a --no-stream
sudo docker stop nginx2
sudo docker start nginx2

Example Images

To install docker on Ubuntu 20.04.

sudo apt install docker.io

To list any docker containers,

sudo docker ps -a

To list any docker images that were downloaded to create the containers,

sudo docker images

The example containers I created used in the video were from https://hub.docker.com/r/nginxdemos/hello/

sudo docker run -P -d --name nginx1 nginxdemos/hello
sudo docker run -P -d --name nginx1 nginxdemos/hello
sudo docker run -P -d --name nginx1 nginxdemos/hello

After experimenting with docker containers and images, and you no longer want them, you can clean up your system and delete them using these commands.

sudo docker rm -f $(sudo docker ps -a -q)
sudo docker rmi -f $(sudo docker images -q)

Troubleshooting

Ensure EnableRemoteCommands=1 is set in your server or proxy config. And you have restarted your server or proxy after making the change.

If you get the error,

Unsupported item key.
Cannot execute script

It could be many things. It may also be because your agent is connected to the server with only Active checks. The server is unable to find your agent, possibly due to it being on a different network with no firewall rule configuration to forward the request from the server. If your agent is behind a firewall, and is using a Zabbix Proxy, then you won't need to configure the firewall as long as your Proxy is also running in Active mode (default).

Comments