Running Nagios in a Docker Container

In past systems administration roles I used Nagios for systems and applications monitoring. I always found it flexible for my needs allowing me to write health checks for anything that the open source community had not already provided. As I am trying to pickup new DevOps skills, I completed a get started with Docker tutorial and thought, deploying Nagios at home via Docker would be an interesting project. However, it was a bit tricky and I have only created an image that monitors itself, so far.

To test things out, I ran the latest official CentOS image and installed Nagios and its dependencies. I chose CentOS because I have a background in Red Hat. Installing Nagios was easy enough. I was able to walk through the CentOS install as documented. The tricky part was Docker prevents use of systemd because Docker was designed for securely running a single service per container. Running the Nagios service and a dependency like Apache web server is not considered best practice. For experimentation at home, I looked for a solution.

I decided to create a simple shell script to start the Nagios and Apache services. The shell script is then called via the CMD instruction in my nagios Dockerfile. The Dockerfile and shell script can be found below or on GitHub. For good measure I also pushed my Nagios image up to Docker Hub.

Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Dockerfile
FROM centos:latest

# Update
RUN yum update -y

# Install prerequisites
RUN yum install -y epel-release gcc glibc glibc-common wget unzip httpd php gd gd-devel perl postfix make

# Set the working directory
WORKDIR /nagios/

# Copy into /nagios
COPY . /nagios/

# Download source
RUN wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.3.tar.gz
RUN tar xzf nagioscore.tar.gz

# Set the working directory
WORKDIR /nagios/nagioscore-nagios-4.4.3/

# Compile
RUN ./configure
RUN make all

# Create User and Group
RUN make install-groups-users
RUN usermod -a -G nagios apache

# Install Binaries
RUN make install

# Install Service-Daemon
RUN make install-daemoninit
RUN systemctl enable httpd.service

# Install Command Mode
RUN make install-commandmode

# Install Configuration Files
RUN make install-config

# Install Apache Config Files
RUN make install-webconf

# Create nagiosadmin User Account
RUN htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

# Install Nagios plugins dependencies
RUN yum install -y which gettext automake autoconf openssl-devel net-snmp net-snmp-utils
RUN yum install -y perl-Net-SNMP

# Set working directory
WORKDIR /nagios/

# Downlaod Nagios Plugins
RUN wget --no-check-certificate -O /nagios/nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz
RUN tar zxf nagios-plugins.tar.gz

# Set working directory
WORKDIR /nagios/nagios-plugins-release-2.2.1/

# Install Nagios plugins
RUN ./tools/setup
RUN ./configure
RUN make
RUN make install

# Start Apache and Nagios
CMD ["/bin/bash", "/nagios/start.sh"]
start.sh
1
2
3
4
#!/bin/bash
/etc/rc.d/init.d/nagios start
/usr/sbin/httpd -k start
tail -f /var/log/httpd/access_log /var/log/httpd/error_log

Now I have a Nagios container to play around with. My next steps are to update the configuration to monitor other containers and possibly other hosts on my home network.