{"id":15118,"date":"2023-01-10T23:55:25","date_gmt":"2023-01-10T20:55:25","guid":{"rendered":"https:\/\/kifarunix.com\/?p=15118"},"modified":"2024-03-10T08:05:27","modified_gmt":"2024-03-10T05:05:27","slug":"deploy-nagios-as-a-docker-container","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/deploy-nagios-as-a-docker-container\/","title":{"rendered":"Deploy Nagios as a Docker Container"},"content":{"rendered":"\n<p>How to install Nagios core in Docker? In this tutorial, you will learn how to deploy Nagios as a Docker container. Nagios Core is an open-source IT infrastructure monitoring and alerting engine.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#deploying-nagios-core-as-a-docker-container\">Deploying Nagios Core as a Docker Container<\/a><ul><li><a href=\"#install-docker-engine-on-your-linux-host\">Install Docker Engine on your Linux host<\/a><\/li><li><a href=\"#create-and-build-nagios-core-docker-image\">Create and Build Nagios Core Docker Image<\/a><\/li><li><a href=\"#create-nagios-core-docker-container\">Create Nagios Core Docker Container<\/a><\/li><li><a href=\"#accessing-nagios-core-web-interface\">Accessing Nagios Core Web Interface<\/a><\/li><li><a href=\"#configure-nagios-docker-container-to-monitor-your-infrastructure\">Configure Nagios Docker Container to Monitor your Infrastructure<\/a><\/li><li><a href=\"#add-hosts-to-monitor\">Add Hosts to Monitor<\/a><\/li><li><a href=\"#other-tutorials\">Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"deploying-nagios-core-as-a-docker-container\">Deploying Nagios Core as a Docker Container<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-docker-engine-on-your-linux-host\">Install Docker Engine on your Linux host<\/h3>\n\n\n\n<p>You need to have installed Docker engine on your Linux host before you can proceed. We will be using Docker CE in this tutorial.<\/p>\n\n\n\n<p>You can check our various guides on how to install Docker on various Linux distros;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-docker-on-rocky-linux-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Docker on Rocky Linux 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-docker-ce-on-ubuntu-20-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Docker CE on Ubuntu 20.04<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-use-docker-on-debian-10-buster\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install and Use Docker on Debian 10 Buster<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"create-and-build-nagios-core-docker-image\">Create and Build Nagios Core Docker Image<\/h3>\n\n\n\n<p>There could be Nagios docker images out there that have already been created by the community. However, we will build our own image in this setup, just to learn about the whole process.<\/p>\n\n\n\n<p>Thus, create a directory, say under \/opt, where you can store all necessary files that we will create for the purposes of creating Nagios core Docker image. The name of the directory could be anything you like!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/opt\/nagios-core-docker<\/code><\/pre>\n\n\n\n<p>Navigate to the directory above;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/opt\/nagios-core-docker\/<\/code><\/pre>\n\n\n\n<p>Download latest Nagios Core Archive File. As of this writing, <a href=\"https:\/\/github.com\/NagiosEnterprises\/nagioscore\/releases\" target=\"_blank\" rel=\"noreferrer noopener\">Nagios 4.4.9 is the current release version<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/assets.nagios.com\/downloads\/nagioscore\/releases\/nagios-4.4.9.tar.gz<\/code><\/pre>\n\n\n\n<p>Extract the source archive;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar xzf nagios-4.4.9.tar.gz<\/code><\/pre>\n\n\n\n<p>Download latest <a href=\"https:\/\/github.com\/nagios-plugins\/nagios-plugins\/releases\" target=\"_blank\" rel=\"noreferrer noopener\">Nagios monitoring plugins<\/a>;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/github.com\/nagios-plugins\/nagios-plugins\/releases\/download\/release-2.4.2\/nagios-plugins-2.4.2.tar.gz<\/code><\/pre>\n\n\n\n<p>Extract the plugins;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar xzf nagios-plugins-2.4.2.tar.gz<\/code><\/pre>\n\n\n\n<p>You may also need NRPE plugins (<strong><code>check_nrpe<\/code><\/strong>) on the server. Thus, download the latest release version from the <a href=\"https:\/\/github.com\/NagiosEnterprises\/nrpe\/releases\" target=\"_blank\" rel=\"noreferrer noopener\">releases page<\/a>;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/github.com\/NagiosEnterprises\/nrpe\/releases\/download\/nrpe-4.1.0\/nrpe-4.1.0.tar.gz<\/code><\/pre>\n\n\n\n<p>Extract the NRPE archive;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar xzf nrpe-4.1.0.tar.gz<\/code><\/pre>\n\n\n\n<p>You should now have Nagios and plugins source directory in the current working directory;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -d1 *\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nagios-4.4.9\/\nnagios-plugins-2.4.2\/\nnrpe-4.1.0\/<\/code><\/pre>\n\n\n\n<p>Next, create Nagios core installation Dockerfile. <strong><code>Dockerfile<\/code><\/strong> <em>is a text document that contains all the commands a user could call on the command line to assemble an image<\/em>.<\/p>\n\n\n\n<p>In the Dockerfile, we are need to define all the commands for the installation and setup of Nagios core server.<\/p>\n\n\n\n<p>In this guide, we will build our Nagios Core Docker image over an Ubuntu 22.04 OS. Hence, the base OS should guide the commands you use.<\/p>\n\n\n\n<p>We will base our setup commands on our Nagios core server installation guide on Ubuntu 22.04;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-setup-nagios-on-ubuntu\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install and Setup Nagios on Ubuntu 22.04<\/a><\/p>\n\n\n\n<p>Note that we will build Nagios Core from source code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim Dockerfile<\/code><\/pre>\n\n\n\n<p>Paste the content below;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>FROM ubuntu:22.04\nRUN apt update -y\nRUN echo 'debconf debconf\/frontend select Noninteractive' | debconf-set-selections\nRUN DEBIAN_FRONTEND=noninteractive \nRUN apt install -y \\\n\tautoconf \\\n\tgcc \\\n\tlibc6 \\\n\tmake \\\n\twget \\\n\tunzip \\\n\tapache2 \\\n\tapache2-utils \\\n\tphp \\\n\tlibapache2-mod-php \\\n\tlibgd-dev \\\n\tlibssl-dev \\\n\tlibmcrypt-dev \\\n\tbc \\\n\tgawk \\\n\tdc \\\n\tbuild-essential \\\n\tsnmp \\\n\tlibnet-snmp-perl \\\n\tgettext \\\n\tfping \\\n        iputils-ping \\\n\tqstat \\\n\tdnsutils \\\n\tsmbclient\n# Building Nagios Core\nCOPY nagios-4.4.9 \/nagios-4.4.9\nWORKDIR \/nagios-4.4.9\nRUN .\/configure --with-httpd-conf=\/etc\/apache2\/sites-enabled &amp;&amp; \\\n    make all &amp;&amp; \\\n    make install-groups-users &amp;&amp; \\\n    usermod -aG nagios www-data &amp;&amp; \\\n    make install &amp;&amp; \\\n    make install-init &amp;&amp; \\\n    make install-daemoninit &amp;&amp; \\\n    make install-commandmode &amp;&amp; \\\n    make install-config &amp;&amp; \\\n    make install-webconf &amp;&amp; \\\n    a2enmod rewrite cgi\n# Building Nagios Plugins\nCOPY nagios-plugins-2.4.2 \/nagios-plugins-2.4.2\nWORKDIR \/nagios-plugins-2.4.2\nRUN .\/configure --with-nagios-user=nagios --with-nagios-group=nagios &amp;&amp; \\\n    make &amp;&amp; \\\n    make install\n# Build and Install NRPE Plugins\nCOPY nrpe-4.1.0 \/nrpe-4.1.0\nWORKDIR \/nrpe-4.1.0\nRUN .\/configure &amp;&amp; \\\n    make all &amp;&amp; \\\n    make install-plugin\nWORKDIR \/root\n# Copy the Nagios basic auth credentials set in the env file;\nCOPY .env \/usr\/local\/nagios\/etc\/\n# Add Nagios and Apache Startup script\nADD start.sh \/\nRUN chmod +x \/start.sh\n\nCMD [ \"\/start.sh\" ]\n<\/code><\/pre>\n\n\n\n<p>The above will build and install Nagios and Nagios plugins, including check_nrpe plugins on an Ubuntu 22.04 image.<\/p>\n\n\n\n<p>It also copies Nagios basic authentication credentials defined in the environment variables;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim .env<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>NAGIOSADMIN_USER=nagiosadmin\nNAGIOSADMIN_PASSWORD=password<\/code><\/pre>\n\n\n\n<p>To use different credentials, update the above file accordingly. Note that <strong><code>nagiosadmin<\/code><\/strong>&nbsp;is the default admin user for accessing Nagios web interface (as specified in the cgi.cfg). You can create or use any other user name and this will be updated in the&nbsp;<code>\/usr\/local\/nagios\/etc\/cgi.cfg<\/code>&nbsp;configuration file by commands defined in the <strong><code>start.sh<\/code><\/strong> file below.<\/p>\n\n\n\n<p>When you create and run Nagios Docker container using the image we will create, the credentials will be automatically updated based on the values of the environment variables above.<\/p>\n\n\n\n<p>We have also made it in such a way that you can override the Nagios credentials using the <strong><code>NAGIOSADMIN_USER_OVERRIDE<\/code><\/strong> and <strong><code>NAGIOSADMIN_PASSWORD_OVERRIDE<\/code><\/strong> during runtime using the <strong><code>docker run<\/code><\/strong> command.<\/p>\n\n\n\n<p>At the end of the Dockerfile, there is a Nagios and Apache startup script called <strong><code>start.sh<\/code><\/strong>.<\/p>\n\n\n\n<p>The script is located in the current working directory and the below are its contents.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat start.sh<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n#!\/bin\/bash\n# Load the credentials variables\nsource \/usr\/local\/nagios\/etc\/.env\n\n# Override the environment variables if passed as arguments during docker run\nif [ -n \"$NAGIOSADMIN_USER_OVERRIDE\" ]; then\n    export NAGIOSADMIN_USER=\"$NAGIOSADMIN_USER_OVERRIDE\"\nfi\n\nif [ -n \"$NAGIOSADMIN_PASSWORD_OVERRIDE\" ]; then\n    export NAGIOSADMIN_PASSWORD=\"$NAGIOSADMIN_PASSWORD_OVERRIDE\"\nfi\n\n# Update configuration files with the variable values, considering overrides\nhtpasswd -b -c \/usr\/local\/nagios\/etc\/htpasswd.users \"${NAGIOSADMIN_USER_OVERRIDE:-$NAGIOSADMIN_USER}\" \"${NAGIOSADMIN_PASSWORD_OVERRIDE:-$NAGIOSADMIN_PASSWORD}\"\nsed -i \"s\/nagiosadmin\/${NAGIOSADMIN_USER_OVERRIDE:-$NAGIOSADMIN_USER}\/g\" \/usr\/local\/nagios\/etc\/cgi.cfg\n\n# Redirect root URL (\/) to \/nagios\necho 'RedirectMatch ^\/$ \/nagios' >> \/etc\/apache2\/apache2.conf\n\n# Start Nagios\n\/etc\/init.d\/nagios start\n\n#Start Apache\na2dissite 000-default default-ssl\nrm -rf \/run\/apache2\/apache2.pid\n. \/etc\/apache2\/envvars\n. \/etc\/default\/apache-htcacheclean\n\/usr\/sbin\/apache2 -DFOREGROUND\n<\/code><\/pre>\n\n\n\n<p>With that in place, you can now build your Nagios core 4.4.9 docker image;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker build -t nagios-core:4.4.9 .<\/code><\/pre>\n\n\n\n<p>After a short while, your Nagios core Docker image should be build.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker images<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>REPOSITORY           TAG       IMAGE ID       CREATED          SIZE\n<strong>nagios-core          4.4.9     7c4a7e95a2b0   9 minutes ago   753MB\n<\/strong>ubuntu               22.04     6b7dfa7e8fdb   4 weeks ago     77.8MB<\/code><\/pre>\n\n\n\n<p>Your image is now ready for use!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"create-nagios-core-docker-container\">Create Nagios Core Docker Container<\/h3>\n\n\n\n<p>To confirm whether our image works fine, create and run the Nagios core docker container based on the Nagios image created above for testing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --name nagios-core-4.4.9 -dp 80:80 nagios-core:4.4.9<\/code><\/pre>\n\n\n\n<p>To override credentials defined in the .env file, use the command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run \\\n-e NAGIOSADMIN_USER_OVERRIDE=monadmin \\\n-e NAGIOSADMIN_PASSWORD_OVERRIDE=password \\\n--name nagios-core-4.4.9 -dp 80:80 nagios-core:4.4.9<\/code><\/pre>\n\n\n\n<p>Check created containers thereafter;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker container ls<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>CONTAINER ID   IMAGE               COMMAND       CREATED         STATUS         PORTS                               NAMES\nafc2c2cacb9f   nagios-core:4.4.9   \"\/start.sh\"   5 seconds ago   Up 5 seconds   0.0.0.0:80-&gt;80\/tcp, :::80-&gt;80\/tcp   nagios-core-4.4.9<\/code><\/pre>\n\n\n\n<p>As you can see, all is good!<\/p>\n\n\n\n<p>You can check the logs if any issue;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker logs --tail 200 &lt;container name&gt;<\/code><\/pre>\n\n\n\n<p>e.g;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker logs --tail 200 nagios-core-4.4.9<\/code><\/pre>\n\n\n\n<p>Or just check the logs from <code>\/var\/lib\/docker\/containers\/<\/code> folder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -f \/var\/lib\/docker\/containers\/<strong>&lt;container-ID&gt;<\/strong>\/<strong>&lt;container-ID&gt;<\/strong>.log<\/code><\/pre>\n\n\n\n<p>Replace container-ID with your container ID!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"accessing-nagios-core-web-interface\">Accessing Nagios Core Web Interface<\/h3>\n\n\n\n<p>To check if all is good, you can now access Nagios Core web interface;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;docker-host-IP-or-hostname\/nagios\/<\/code><\/pre>\n\n\n\n<p>When prompted, enter the username and password defined in the environment variables.<\/p>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-basic-auth-prompt.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1413\" height=\"513\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-basic-auth-prompt.png\" alt=\"Deploy Nagios as a Docker Container\" class=\"wp-image-15144\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-basic-auth-prompt.png?v=1673369474 1413w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-basic-auth-prompt-768x279.png?v=1673369474 768w\" sizes=\"(max-width: 1413px) 100vw, 1413px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>Default Nagios Dashboard<\/p>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-default-dashboard.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1754\" height=\"916\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-default-dashboard.png\" alt=\"Deploy Nagios as a Docker Container\" class=\"wp-image-15135\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-default-dashboard.png?v=1673297754 1754w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-default-dashboard-768x401.png?v=1673297754 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-default-dashboard-1536x802.png?v=1673297754 1536w\" sizes=\"(max-width: 1754px) 100vw, 1754px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>And voila! It seems to be working.<\/p>\n\n\n\n<p>Check the host and services status;<\/p>\n\n\n\n<p>host status (showing status of the docker container itself!)<\/p>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-host-status.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1789\" height=\"507\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-host-status.png\" alt=\"\" class=\"wp-image-15146\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-host-status.png?v=1673375393 1789w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-host-status-768x218.png?v=1673375393 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-host-status-1536x435.png?v=1673375393 1536w\" sizes=\"(max-width: 1789px) 100vw, 1789px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>Service status;<\/p>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-service-status.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1898\" height=\"514\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-service-status.png\" alt=\"\" class=\"wp-image-15147\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-service-status.png?v=1673375509 1898w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-service-status-768x208.png?v=1673375509 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/nagios-service-status-1536x416.png?v=1673375509 1536w\" sizes=\"(max-width: 1898px) 100vw, 1898px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>It is almost working, right? -:)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"configure-nagios-docker-container-to-monitor-your-infrastructure\">Configure Nagios Docker Container to Monitor your Infrastructure<\/h3>\n\n\n\n<p>With the default configuration, you are good to go. But let us make it easy to configure Nagios Docker container for monitoring.<\/p>\n\n\n\n<p>To make it easy to configure Nagios Docker container for monitoring your endpoints, we will copy the main Nagios configuration files listed below into the host;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cgi.cfg<\/li>\n\n\n\n<li>nagios.cfg<\/li>\n\n\n\n<li><strong>objects<\/strong><\/li>\n\n\n\n<li>resource.cfg<\/li>\n<\/ul>\n\n\n\n<p>Next, copy the above configs from the just created Nagios docker container into the directory with the Nagios docker container files;<\/p>\n\n\n\n<p>You can copy files from the Docker container to the host using <strong>docker cp<\/strong> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker cp &#91;OPTIONS] CONTAINER:SRC_PATH DEST_PATH<\/code><\/pre>\n\n\n\n<p>e.g;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker cp nagios-core-4.4.9:\/usr\/local\/nagios\/etc \/opt\/nagios-core-docker\/<\/code><\/pre>\n\n\n\n<p>You should now have the configs files\/directories above on the docker host.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -1 etc\/<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cgi.cfg\nhtpasswd.users\nnagios.cfg\nobjects\nresource.cfg<\/code><\/pre>\n\n\n\n<p>To begin the configuration, let&#8217;s disable SSH service checks for the localhost by commenting out SSH service definition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/opt\/nagios-core-docker\/etc\/objects\/localhost.cfg<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>#define service {\n#\n#    use                     local-service           ; Name of service template to use\n#    host_name               localhost\n#    service_description     SSH\n#    check_command           check_ssh\n#    notifications_enabled   0\n#}\n<\/code><\/pre>\n\n\n\n<p>Save and exit the file.<\/p>\n\n\n\n<p>To use this configurations on the container, you need to mount them using the <code>-v\/--volume<\/code> option to <code>docker run<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --help<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>-v, --volume list                    Bind mount a volume<\/code><\/pre>\n\n\n\n<p>So, to use our new configs, remove the old container and start a new one with the new configs;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker container rm -f nagios-core-4.4.9<\/code><\/pre>\n\n\n\n<p>Check Nagios core Docker container configuration for errors before you can start the container;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --rm -v \"\/opt\/nagios-core-docker\/etc:\/usr\/local\/nagios\/etc\" \\\n-ti nagios-core:4.4.9 \/usr\/local\/nagios\/bin\/nagios -v \/usr\/local\/nagios\/etc\/nagios.cfg<\/code><\/pre>\n\n\n\n<p>Sample config check output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Nagios Core 4.4.9\nCopyright (c) 2009-present Nagios Core Development Team and Community Contributors\nCopyright (c) 1999-2009 Ethan Galstad\nLast Modified: 2022-11-16\nLicense: GPL\n\nWebsite: https:\/\/www.nagios.org\nReading configuration data...\n   Read main config file okay...\n   Read object config files okay...\n\nRunning pre-flight check on configuration data...\n\nChecking objects...\n\tChecked 7 services.\n\tChecked 1 hosts.\n\tChecked 1 host groups.\n\tChecked 0 service groups.\n\tChecked 1 contacts.\n\tChecked 1 contact groups.\n\tChecked 24 commands.\n\tChecked 5 time periods.\n\tChecked 0 host escalations.\n\tChecked 0 service escalations.\nChecking for circular paths...\n\tChecked 1 hosts\n\tChecked 0 service dependencies\n\tChecked 0 host dependencies\n\tChecked 5 timeperiods\nChecking global event handlers...\nChecking obsessive compulsive processor commands...\nChecking misc settings...\n\nTotal Warnings: 0\nTotal Errors:   0\n\nThings look okay - No serious problems were detected during the pre-flight check\n<\/code><\/pre>\n\n\n\n<p>Start new container with new configs mounted!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --name nagios-core-4.4.9 -dp 80:80 \\\n-v \"\/opt\/nagios-core-docker\/etc:\/usr\/local\/nagios\/etc\" \\\nnagios-core:4.4.9<\/code><\/pre>\n\n\n\n<p>You can also enable the container to restart on crash or system reboot using the option <strong><code>--restart unless-stopped<\/code><\/strong>;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --name nagios-core-4.4.9 --restart unless-stopped \\\n-dp 80:80 \\\n-v \"\/opt\/nagios-core-docker\/etc:\/usr\/local\/nagios\/etc\" \\\nnagios-core:4.4.9<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"add-hosts-to-monitor\">Add Hosts to Monitor<\/h3>\n\n\n\n<p>Adding hosts, creating host and service definition templates on Nagios core docker container is just the same as you would on to Nagios installed on a server except that, we are controlling the configurations via bind mounts in this guide.<\/p>\n\n\n\n<p>So take for example, I want to add another host reachable via the IP 192.168.56.146 to Nagios Core for monitoring, I could create a custom configs directory under objects directory;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/opt\/nagios-core-docker\/etc\/objects\/kifarunix<\/code><\/pre>\n\n\n\n<p>If you are using a custom configuration directory, make Nagios aware of the same!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed -i '\/OBJECT CACHE FILE\/i cfg_dir=\/usr\/local\/nagios\/etc\/objects\/kifarunix\\n' \/opt\/nagios-core-docker\/etc\/nagios.cfg<\/code><\/pre>\n\n\n\n<p>Create your hosts and hostgroups definition configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/opt\/nagios-core-docker\/etc\/objects\/kifarunix\/host-hostgroups.cfg<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>define host {\n    use                     linux-server   \n    host_name               elk-stack\n    alias                   ELK-STACK-NODE\n    address                 192.168.56.146\n}\n<\/code><\/pre>\n\n\n\n<p>Note that we use a default host definition template above. You can create your own!<\/p>\n\n\n\n<p>Save and exit the configuration file.<\/p>\n\n\n\n<p>Check the config for errors again!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --rm -v \"\/opt\/nagios-core-docker\/etc:\/usr\/local\/nagios\/etc\" \\\n-ti nagios-core:4.4.9 \/usr\/local\/nagios\/bin\/nagios -v \/usr\/local\/nagios\/etc\/nagios.cfg<\/code><\/pre>\n\n\n\n<p>Remove and start a new container;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker container rm -f nagios-core-4.4.9<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run --name nagios-core-4.4.9 -dp 80:80 -v \"\/opt\/nagios-core-docker\/etc:\/usr\/local\/nagios\/etc\" nagios-core:4.4.9<\/code><\/pre>\n\n\n\n<p>When you check on Nagios web interface, you host should now be added!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1891\" height=\"502\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/add-hosts-to-monitor.png\" alt=\"\" class=\"wp-image-15152\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/add-hosts-to-monitor.png?v=1673383379 1891w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/add-hosts-to-monitor-768x204.png?v=1673383379 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/add-hosts-to-monitor-1536x408.png?v=1673383379 1536w\" sizes=\"(max-width: 1891px) 100vw, 1891px\" \/><\/figure>\n\n\n\n<p>And there you go!<\/p>\n\n\n\n<p>You can now add services as well.<\/p>\n\n\n\n<p>That brings us to the end of our tutorial on how to deploy Nagios as a Docker container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-tutorials\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-dozzle-real-time-log-viewer-for-docker-containers-on-ubuntu\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Dozzle&nbsp;Real-Time Log Viewer for&nbsp;Docker Containers on Ubuntu<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-guacamole-as-docker-container-on-rocky-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Guacamole as Docker Container on Rocky Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to install Nagios core in Docker? In this tutorial, you will learn how to deploy Nagios as a Docker container. Nagios Core is an<\/p>\n","protected":false},"author":10,"featured_media":15154,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[72,1076,1077,121,73],"tags":[6239,6235,6329,6238,76,6240,6236,6237],"class_list":["post-15118","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-monitoring","category-containers","category-docker","category-howtos","category-nagios","tag-build-nagios-docker-container","tag-deploy-nagios-core-as-a-docker-container","tag-how-to-install-nagios-core-in-docker","tag-nagios-container-docker","tag-nagios-core","tag-nagios-core-container","tag-nagios-docker-container","tag-nagios-docker-image","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/15118"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=15118"}],"version-history":[{"count":35,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/15118\/revisions"}],"predecessor-version":[{"id":20691,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/15118\/revisions\/20691"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/15154"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=15118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=15118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=15118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}