Docker Community Forums

Share and learn in the Docker community.

No suhj file or directory found message when creating a nagios image

docker

(Bluemind2005) #1

Good day Dockers!

I am creating a nagios docker application from scratch and I get the error message below.

Things look okay - No serious problems were detected during the 

pre-flight check
/bin/sh: /etc/init.d/nagios: No such file or directory
The command ‘/bin/sh -c cd /tmp && tar -zxvf nagios-4.1.1.tar.gz && cd nagios* && ./configure --with-command-group=${NAGIOS_USER} && make all && make install && make install-config && make install-commandmode && make install-webconf && cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/ && chown -R ${NAGIOS_USER}:${NAGIOS_USER} /usr/local/nagios/libexec/eventhandlers && /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg && /etc/init.d/nagios start && /etc/init.d/httpd start && htpasswd -c /usr/local/nagios/etc/htpasswd.users ${NAGIOSADMIN_USER}’ returned a non-zero code: 127

Here is the list of my images:

[jim@docker1 nagios-docker]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
b60e09bdab64 28 minutes ago 453.5 MB
f0048fd8eabf 35 minutes ago 452.6 MB
9e2e0d5d3464 42 minutes ago 208 MB
docker.io/centos latest 05188b417f30 2 weeks ago 196.8 MB

And finally Dockerfile is here:
[jim@docker1 nagios-docker]$ cat Dockerfile

FROM centos

RUN yum install -y zip unzip wget httpd php gcc glibc glibc-common gd gd-devel make net-snmp

ENV NAGIOS_HOME /opt/nagios
ENV NAGIOS_USER nagios
ENV NAGIOS_GROUP nagios
ENV NAGIOS_CMDUSER nagios
ENV NAGIOS_CMDGROUP nagios
ENV NAGIOSADMIN_USER nagios
ENV NAGIOSADMIN_PASS nagios
ENV APACHE_RUN_USER nagios
ENV APACHE_RUN_GROUP nagios
ENV NAGIOS_TIMEZONE UTC

RUN ( egrep -i “^${NAGIOS_GROUP}” /etc/group || groupadd $NAGIOS_GROUP ) && ( egrep -i “^${NAGIOS_CMDGROUP}” /etc/group || groupadd $NAGIOS_CMDGROUP )
RUN ( id -u $NAGIOS_USER || useradd --system $NAGIOS_USER -g $NAGIOS_GROUP -d $NAGIOS_HOME ) && ( id -u $NAGIOS_CMDUSER || useradd --system -d $NAGIOS_HOME -g $NAGIOS_CMDGROUP $NAGIOS_CMDUSER )

ADD https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz /tmp/nagios-4.1.1.tar.gz
RUN cd /tmp && tar -zxvf nagios-4.1.1.tar.gz && cd nagios* && ./configure --with-command-group=${NAGIOS_USER} && make all && make install && make install-config && make install-commandmode && make install-webconf && cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/ && chown -R ${NAGIOS_USER}:${NAGIOS_USER} /usr/local/nagios/libexec/eventhandlers && /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg && /etc/init.d/nagios start && /etc/init.d/httpd start && htpasswd -c /usr/local/nagios/etc/htpasswd.users ${NAGIOSADMIN_USER}

ADD http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz /tmp/
RUN cd /tmp && tar -zxvf nagios-plugins-* && cd nagios-plugins* && ./configure --with-nagios-user=${NAGIOS_USER} --with-nagios-group=${NAGIOS_GROUP} && make && make install

chkconfig --add nagios
chkconfig --level 35 nagios on
chkconfig --add httpd
chkconfig --level 35 httpd on

EXPOSE 80

VOLUME /opt/nagios/var
VOLUME /opt/nagios/etc
VOLUME /opt/nagios/libexec
[jim@docker1 nagios-docker]$

Is there any way to get inside the image and run commands like ls /etc/init/d/nagios?

Considering the installation has gone thus far, I believe it might be the case that /etc/init.d directory itself may not be present.

Also please forgive me if I sound silly, Can I get inside the image(Image ID b60e09bdab64 ) and run these commands manually? It would be the same deal as creating image manually with Dockerfile.

Kindly help me clear my doubts

Thanks
Jim


(David Maze) #2

You can pass the hex image ID to docker run, e.g. docker run --rm -it b60e09 bash. That’s almost certainly the best way to debug this. If you break the very long RUN line in the Dockerfile into two, you can get an image that’s run everything up until right before the problematic command; docker build will print out its image ID; and then you can get a shell at that specific point in the build process.

I’m pretty sure CentOS 7 just doesn’t have an /etc/init.d directory.

That having been said, typical Docker containers don’t run init scripts or, usually, an init process. If you wanted to run, for instance, Apache in a container, your Dockerfile would typically end with

CMD apachectl -k start -DFOREGROUND

and the container would only run Apache; when Apache exits, the container exits.

Your Dockerfile is pretty clearly trying to install and run two separate things, and it’d be better design to run Nagios and Apache in two separate containers. If they communicate via a socket (Apache is a reverse proxy, for instance) use inter-container links or a private Docker network; if they communicate via files (Apache directly publishes a dashboard and the Nagios daemon just updates static files) use a shared volume.

Setting up a container to do more than one thing is an advanced container design. It’s usually done with a lightweight init system like supervisord. The init system in CentOS 7, systemd, tries to do a lot of things that are irrelevant to the task of just keeping two processes alive, to the point of requiring special privileges to run the container, and while there are write-ups on how to do it, it’d probably be my last choice.


(Bluemind2005) #3

Thanks mate for the quick response. It seems that CentOS 7 might have a different procedure to start nagios. Also I need to update my variables for HTTPD as APACHE is for Ubuntu. I would get back on this thread soon.


(Bluemind2005) #4

I’m back. I realized what was wrong. While CentOS7 does have /etc/init.d directory it doesn’t start up application it used to in CentOS 6. Instead it uses systemctl instead of service command.

Please find my Dockerfile(updated one).
[jim@docker1 nagios-docker]$ cat Dockerfile
FROM centos

   RUN yum -y install make httpd php php-cli gcc glibc glibc-common gd gd-devel net-snmp openssl-devel wget unzip

    RUN useradd nagios && groupadd nagcmd && usermod -a -G nagcmd nagios && usermod -a -G nagcmd apache

    ADD https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz /tmp/nagios-4.1.1.tar.gz

    RUN cd /tmp && tar -zxvf nagios-4.1.1.tar.gz && cd nagios*  && ./configure --with-command-group=nagcmd && make all && make install && make install-config && make install-commandmode && make install-webconf && /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg && htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios

    ADD http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz  /tmp/nagios-plugins-2.1.1.tar.gz
    RUN cd /tmp && tar -zxvf nagios-plugins-* && cd nagios-plugins* && ./configure --with-nagios-user=nagios --with-nagios-group=nagios && make all && make install

    RUN systemctl enable httpd && systemctl enable nagios

    EXPOSE 80

    VOLUME /opt/nagios/var
    VOLUME /opt/nagios/etc
    VOLUME /opt/nagios/libexec
    [jim@docker1 nagios-docker]$ 

Failed at the below command.

Step 9 : RUN systemctl enable httpd.service && systemctl enable nagios.service
 ---> Running in 1f4e22baf6b0
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Operation failed: No such file or directory
The command '/bin/sh -c systemctl enable httpd.service && systemctl enable nagios.service' returned a non-zero code: 1

Further to that:

Step 9 : RUN systemctl enable httpd.service && systemctl enable nagios.service
 ---> Running in 76fcf6dc2372
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Operation failed: No such file or directory
The command '/bin/sh -c systemctl enable httpd.service && systemctl enable nagios.service' returned a non-zero code: 1
[jim@docker1 nagios-docker]$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
<none>                  <none>              **6339acb73291**        8 minutes ago       566.1 MB
docker.io/centos        latest              50dae1ee8677        2 days ago          196.7 MB
docker.io/centos        centos6             cf2c3ece5e41        2 weeks ago         194.6 MB
[jim@docker1 nagios-docker]$ docker run --rm -it 6339acb73291 bash
[root@e70c643bf30b /]# systemctl start nagios
Failed to get D-Bus connection: Operation not permitted
[root@e70c643bf30b /]# id
uid=0(root) gid=0(root) groups=0(root)
[root@e70c643bf30b /]# systemctl restart httpd
Failed to get D-Bus connection: Operation not permitted
[root@e70c643bf30b /]# systemctl start httpd  
Failed to get D-Bus connection: Operation not permitted
[root@e70c643bf30b /]# systemctl enable httpd      
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@e70c643bf30b /]# systemctl enable nagios
Operation failed: No such file or directory
[root@e70c643bf30b /]# ps -ef|grep httpd
root        23     1  0 07:29 ?        00:00:00 grep --color=auto httpd

Although systemctl enable httpd has worked the httpd is not running, I am not sure how this is going to work ever…

Coming back to your suggestion on to having another container for httpd, I’m wonder how nagios would be accessible with httpd?

Can you demonstrate with some example ?


(David Maze) #5

Don’t use systemctl inside Docker images.

Your Docker container almost certainly isn’t (and shouldn’t) running the systemd daemon, and systemctl doesn’t work. Where, for instance, parts of the Nagios documentation have “init script” and “manual” directions, you’re looking for “manual”.

…Nagios is built as a bunch of CGI scripts plus a monitoring daemon, with storage in the local filesystem. I’d probably set it up by building a single Docker image with Nagios and Apache installed (as you’ve done), then running

docker run -d --name nagios-daemon me/nagios /opt/nagios/bin/nagios ...
docker run -d --name nagios-httpd --volumes-from nagios-daemon me/nagios apachectl -k start -DFOREGROUND

Since you have VOLUME /opt/nagios/var in the Dockerfile, the first docker run command will create shared storage for that directory, and the --volumes-from option in the second will reuse that shared storage, so both containers have the same underlying data.


(Bluemind2005) #6

Thanks David. Getting there,
Did you missed volumes switch in first command? I’m just confused with what are your second and third parameters after --name switch.

Maybe one of these?

  -v, --volume=[]                 Bind mount a volume
  --volume-driver                 Optional volume driver for the container
  --volumes-from=[]               Mount volumes from the specified container(s)
  -w, --workdir                   Working directory inside the container

I think it should be something like this ?
RUN docker run -d --name nagios-daemon -v /home/jim/Documents/nagio/bin /usr/local/nagios/bin

I’m just now trying it. Its fund to work with docker, I haven’t yet looked at best practices for writing docker files ! Lot more to do…


(David Maze) #7

I had written:

docker run \
  -d \                        # in the background
  --name nagios-daemon \      # the name of the container
  me/nagios \                 # the name of the image
  /opt/nagios/bin/nagios ...  # the command to run

Since the Dockerfile says VOLUME /opt/nagios/var, running this automatically causes Docker to create a volume inside Docker storage space, even though I’m not specifying any -v options. Then, when I run

docker run \
  -d \                             # in the background
  --name nagios-httpd \            # name of this container
  --volumes-from nagios-daemon \   # share volumes with other container
  me/nagios \                      # the name of the image
  apachectl -k start -DFOREGROUND  # the command to run

it copies all of those volume mounts from the nagios-daemon container, again, even though it doesn’t specify its own -v options. In both containers /opt/nagios/var point at the same unit of storage, which Docker manages inside its own filesystem space.


(Bluemind2005) #8

Thanks David. I was confused initially how come I could run that command in Dockerfile but now clear that it should be executed to spin up nagios dockerised application. With clarity comes knowledge :slight_smile:
I loved the concept of having a dockerised nagios image which one can run anywhere ! I’m getting closed but not yet there :tired_face:

I am having issue here, whenever I try to spin up it exits cleaning, but that is not what I want. Here is the command:

[jim@docker1 nagios-docker]$ docker run -d --name jim-nagios-daemon jim_nagios-docker /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
b8de5914a5bc0a6501b986bb86d808348a4778f72b167c684e8ae918e25c4615
[jim@docker1 nagios-docker]$ docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS               NAMES
b8de5914a5bc        jim_nagios-docker       "/usr/local/nagios/bi"   4 seconds ago       Exited (0) 3 seconds ago                        jim-nagios-daemon
[jim@docker1 nagios-docker]$ cat Dockerfile 
FROM centos

MAINTAINER Jim<jiminfotechnologist@gmail.com>

RUN yum -y install make httpd php php-cli gcc glibc glibc-common gd gd-devel net-snmp openssl-devel wget unzip

RUN useradd nagios && groupadd nagcmd && usermod -a -G nagcmd nagios && usermod -a -G nagcmd apache

ADD https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz /tmp/nagios-4.1.1.tar.gz

RUN cd /tmp && tar -zxvf nagios-4.1.1.tar.gz && cd nagios*  && ./configure --with-command-group=nagcmd && make all && make install && make install-config && make install-commandmode && make install-webconf && /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg && htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios

ADD http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz  /tmp/nagios-plugins-2.1.1.tar.gz
RUN cd /tmp && tar -zxvf nagios-plugins-* && cd nagios-plugins* && ./configure --with-nagios-user=nagios --with-nagios-group=nagios && make all && make install

EXPOSE 80

VOLUME /usr/local/nagios/bin
VOLUME /usr/local/nagios/etc
VOLUME /usr/local/nagios/libexec
[jim@docker1 nagios-docker]$ 

Also to access nagios, I should use IP of container? I think that is the way it would be truly PODA(Package Once Deploy Anywhere).

Another question is the files which I need to create and edit should be within base image I suppose? I would need to create two files specifically services.cfg and host.cfg under /usr/local/nagios/etc/


(Bluemind2005) #9

Anyone any update on my questions ?