How to install Docker and pull Nagios image on RHEL 7

Step 1. remove old version and install latest Docker
sudo yum remove docker docker-common docker-selinux docker-engine
sudo yum-config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce

If you got: Docker Error – Requires: container-selinux >= 2:2.74
Solution:
Go to http://mirror.centos.org/centos/7/extras/x86_64/Packages/
Find the latest version for container-selinux i.e. container-selinux-2.21-1.el7.noarch.rpm
Run the following command on your terminal: $
sudo yum install -y yum install http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.107-1.el7_6.noarch.rpm
yum install -y yum install http://mirror.centos.org/centos/7/extras/x86_64/Packages/fuse3-libs-3.6.1-4.el7.x86_64.rpm
yum install -y yum install http://mirror.centos.org/centos/7/extras/x86_64/Packages/fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm

Step 2. enable docker service and start it
$ sudo systemctl enable docker.service
$ sudo systemctl start docker.service ## <-- Start docker ##
$ sudo systemctl stop docker.service ## <-- Stop docker ##
$ sudo systemctl restart docker.service ## <-- Restart docker ##
$ sudo systemctl status docker.service ## <-- Get status of docker ##
$ ip a list docker0
$ docker run hello-world ## <– test your docker installation

Step 3. Get Nagios from https://hub.docker.com/r/jasonrivers/nagios/
docker pull jasonrivers/nagios:latest
docker run –name nagios4 -p 0.0.0.0:8080:80 jasonrivers/nagios:latest

The default credentials for the web interface is nagiosadmin / nagios
alternatively you can use external Nagios configuration & log data with the following:

docker run --name nagios4  \
  -v /path-to-nagios/etc/:/opt/nagios/etc/ \
  -v /path-to-nagios/var:/opt/nagios/var/ \
  -v /path-to-custom-plugins:/opt/Custom-Nagios-Plugins \
  -v /path-to-nagiosgraph-var:/opt/nagiosgraph/var \
  -v /path-to-nagiosgraph-etc:/opt/nagiosgraph/etc \
  -p 0.0.0.0:8080:80 jasonrivers/nagios:latest

You can open it with http://your_server_ip:8080

Extra Plugins:
Nagios nrpe [http://exchange.nagios.org/directory/Addons/Monitoring-Agents/NRPE–2D-Nagios-Remote-Plugin-Executor/details]
Nagiosgraph [http://exchange.nagios.org/directory/Addons/Graphing-and-Trending/nagiosgraph/details]
JR-Nagios-Plugins – custom plugins I’ve created [https://github.com/JasonRivers/nagios-plugins]
WL-Nagios-Plugins – custom plugins from William Leibzon [https://github.com/willixix/WL-NagiosPlugins]
JE-Nagios-Plugins – custom plugins from Justin Ellison [https://github.com/justintime/nagios-plugins]

Step 4. How to edit file and restart nagios inside Docker
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker run –name nagios4 -v /root/nagios/etc/:/opt/nagios/etc/ -v /root/nagios/apache2/:/etc/apache2/ -p 0.0.0.0:8080:80 -it -d jasonrivers/nagios:latest

restart nagios inside Docker:
docker exec nagios4 rm etc/service/nagios
docker exec nagios4 ln -s /etc/sv/nagios /etc/service

push docker-compose images to Docker Hub

root@ubunu2004:~# docker-compose build
website uses an image, skipping
Building product-service
Step 1/3 : FROM python:3-onbuild
Executing 3 build triggers
—> Using cache
—> Using cache
—> b54600ebce6f
Step 2/3 : COPY . /usr/src/app
—> e49923648a44
Step 3/3 : CMD ["python", "api.py"]
—> Running in 3314ea5a2d0c
Removing intermediate container 3314ea5a2d0c
—> fc3e3c31fadc
Successfully built fc3e3c31fadc
Successfully tagged root_product-service:latest

root@ubunu2004:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root_product-service latest fc3e3c31fadc 31 seconds ago 701MB
root@ubunu2004:~# docker tag root_product-service:latest zhuby1973/product-service:1
root@ubunu2004:~# docker push zhuby1973/product-service:1

Docker compose tutorial

  1. install docker-compose
    apt install docker-compose
  2. create two dir: Product and website and files as below:
    root@ubunu2004:~/tmp# tree
    .
    ├── docker-compose.yml
    ├── Product
    │   ├── api.py
    │   ├── Dockerfile
    │   └── requirements.txt
    └── website
    └── index.php

    root@ubunu2004:~/Product# cat api.py
    from flask import Flask
    from flask_restful import Resource, Api
    app = Flask(__name__)
    api = Api(app)
    class Product(Resource):
    def get(self):
        return {
            'products': ['Ice cream',
                        'Chocolate',
                        'Eggs',
                        'Fruit']
        }
    api.add_resource(Product, '/')
    if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)
    root@ubunu2004:~/Product# cat requirements.txt
    Flask==0.12
    flask-restful==0.3.5
    root@ubunu2004:~/Product# cat Dockerfile
    FROM python:3-onbuild
    COPY . /usr/src/app
    CMD ["python", "api.py"]
    root@ubunu2004:~/tmp/website# cat index.php
    <html>
    <head>
        <title>My Shop</title>
    </head>
    
    <body>
        <h1>Welcome to my shop</h1>
        <ul>
            <?php
                $json = file_get_contents('http://product-service');
                $obj = json_decode($json);
                $products = $obj->products;
                foreach ($products as $product) {
                    echo "<li>$product</li>";
                }
            ?>
        </ul>
    </body>
    </html>

    create docker-compose.yml:

    version: '3'
    services:
    product-service:
    build: ./Product
    volumes:
      - ./Product:/usr/src/app
    ports:
      - 5001:80
    website:
    image: php:apache
    volumes:
      - ./website:/var/www/html
    ports:
      - 5000:80
    depends_on:
      - product-service

then we can start it:
docker-compose up
or start/stop as daemon:
root@ubunu2004:~# docker-compose up -d
Starting root_product-service_1 … done
Starting root_website_1 … done
root@ubunu2004:~# docker-compose stop
Stopping root_website_1 … done
Stopping root_product-service_1 … done

verify the app on http://192.168.0.43:5000/

Docker images and container cleanup

Stop and remove all containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Remove containers according to a pattern:
docker ps -a | grep "pattern" | awk ‘{print $3}’ | xargs docker rmi
Remove one or more specific images:
docker rmi Image Image
Purging All Unused or Dangling Images, Containers, Volumes, and Networks
docker system prune -a
REF:
https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes

pull and start an image:
docker pull zhuby1973/php:1
docker run -p 82:80 zhuby1973/php:1
you can run it as daemon with -d option:
docker run -d -p 82:80 zhuby1973/php:1

build a container with php/apache

  1. we can search php and apache image on Docker Hub, we will use php:7.0-apache for this demo.

  2. create Dockerfile and src/index.php as below

    root@ubunu2004:~/docker_sample# tree
    .
    ├── Dockerfile
    └── src
    └── index.php
    1 directory, 2 files
    root@ubunu2004:~/docker_sample# cat  Dockerfile
    FROM php:7.0-apache
    COPY src/ /var/www/html
    EXPOSE 80
    root@ubunu2004:~/docker_sample# cat src/index.php
    <?php
    echo "Hello World!";
  3. build the container
    docker build -t hello_world .

  4. run the container
    docker run -p 82:80 hello_world
    then you can verify it on http://192.168.0.43:82/

  5. if you want mapping the index.php to container, you can start it with
    docker run -p 82:80 -v /root/docker_sample/src/:/var/www/html/ hello_world
    now if you update index.php, you will get the change immediately on http://192.168.0.43:82/

  6. you can push this image to Docker Hub
    root@ubunu2004:~/docker_sample# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello_world latest dc8c649d4e8b 19 minutes ago 368MB
    root@ubunu2004:~/docker_sample# docker run -it -d hello_world:latest
    root@ubunu2004:~/docker_sample# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    d2963444e718 hello_world:latest "docker-php-entrypoi…" 10 seconds ago Up 6 seconds 80/tcp laughing_williamson
    root@ubunu2004:~/docker_sample# docker commit d2963444e718 zhuby1973/php:1
    sha256:1f2007f4786c344c8007b574af6f71b7604431c6d6cce0e2091e6adf1a8c8621
    root@ubunu2004:~/docker_sample# docker push zhuby1973/php:1

commit and push a docker image to Docker Hub

  1. sign up on Docker Hub to get your user id and password
  2. pull a image from Docker Hub
    docker pull ubuntu
  3. make a dir on ubuntu image and commit/push image
    docker images
    docker run -it -d ubuntu
    docker ps -a
    root@ubunu2004:~# docker exec -it 6e08ac7cc702 bash
    root@6e08ac7cc702:/# mkdir /test_dir
    root@6e08ac7cc702:/# exit
    exit
    docker commit 6e08ac7cc702 zhuby1973/ubuntu:1
    root@ubunu2004:~# docker login
    Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
    Username: zhuby1973
    Password:
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store
    Login Succeeded
    root@ubunu2004:~# docker push zhuby1973/ubuntu:1
    The push refers to repository [docker.io/zhuby1973/ubuntu]
    d3d28de77691: Pushed
    544a70a875fc: Mounted from library/ubuntu
    cf0f3facc4a3: Mounted from library/ubuntu
    132bcd1e0eb5: Mounted from library/ubuntu
    d22cfd6a8b16: Mounted from library/ubuntu
    2: digest: sha256:4c50ab7b0f8cadd72f5d37a569795a4ba697be892fca1b36a96eb9aa49a59d2e size: 1359
  4. logon https://hub.docker.com/repositories you will find zhuby1973/ubuntu already there!
  5. you can start this container to verify
    
    root@ubunu2004:~# docker run -it -d zhuby1973/ubuntu:1
    e5477e45b6b2bfe330e9f37a6f74e90964ef887f0f0a37ae23bbae4319a465a1
    root@ubunu2004:~# docker ps
    CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
    e5477e45b6b2        zhuby1973/ubuntu:1   "/bin/bash"         26 seconds ago      Up 24 seconds                           vigilant_satoshi
    df78087a8ef1        zhuby1973/test:2     "/bin/bash"         17 minutes ago      Up 17 minutes                           charming_wiles
    6e08ac7cc702        ubuntu               "/bin/bash"         32 minutes ago      Up 32 minutes                           charming_goodall
    root@ubunu2004:~# docker exec -it e5477e45b6b2 bash
    root@e5477e45b6b2:/# ls -ltr /test-dir
    total 0

install Docker on Ubuntu and setup CONTAINER openspug/spug

  1. install Docker on Ubuntu
    sudo apt-get update
    sudo apt install docker.io
    sudo systemctl start docker
    sudo systemctl enable docker
    docker –version
  2. setup openspug/spug container
    openspug/spug is a small server manage tool developed with python!
    https://github.com/openspug/spug
    sudo docker pull openspug/spug
    sudo docker run -d -p 80:80 openspug/spug
    if port 80 already in use, you can find the process by:
    sudo lsof -i:80
    in our case it’s apache, stop it with:
    sudo systemctl stop apache2

sudo docker ps to get running container details:

(base) ubuntu@ubunu2004:/$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
c493cbf6aceb        openspug/spug       "/entrypoint.sh"    4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   determined_allen

initial and restart the spug:
export CONTAINER_ID=c493cbf6aceb
sudo docker exec $CONTAINER_ID init_spug admin spug.dev
sudo docker restart $CONTAINER_ID

then you can login http://192.168.0.43/ with:
user: admin
password: spug.dev