Trying to prep a db in a docker image

Hi

Starting my docker journey, I want to create a container that runs a nagios monitoring box and an extra app called Nagvis that requires a tool called NDOUTILS.

This tool requires a mysql instance and I am trying to set it up in my Dockerfile but no joy so far

Here is the Dockerfile part

#ENV BUILD=aarch64-unknown-linux-gnu

RUN apt-get update && apt-get install -y mysql-server libmysqlclient-dev libdbd-mysql-perl
EXPOSE 3306
RUN service mysql restart && \
    'printf "CREATE DATABASE nagios DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; " ' | mysql -u root && \
    'printf "CREATE USER 'ndoutils'@'localhost' IDENTIFIED BY 'ndoutils_password' ;" '| mysql -u root && \
    'printf "GRANT ALL PRIVILEGES ON nagios.* TO 'ndoutils'@'localhost' WITH GRANT OPTION ;" ' | mysql -u root && \
    'printf "GRANT USAGE ON *.* TO 'ndoutils'@'localhost' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;" ' | mysql -u root && \
    git clone https://github.com/NagiosEnterprises/ndoutils.git && \
    cd ndoutils && \
    ./configure --prefix /opt/nagios --build=aarch64-unknown-linux-gnu --with-init-type=systemd &&\
    make all && make install && make install-init && make install-config && \
    cd db && \
    ./installdb -u ndoutils -p 'ndoutils_password' -h localhost -d nagios
 
RUN mv /opt/nagios/etc/ndo2db.cfg-sample /opt/nagios/etc/ndo2db.cfg
RUN sh -c 'sed -i 's/^db_user=.*/db_user=ndoutils/g' /opt/nagios/etc/ndo2db.cfg'
RUN sh -c 'sed -i 's/^db_pass=.*/db_pass=ndoutils_password/g' /opt/nagios/etc/ndo2db.cfg'
RUN mv /opt/nagios/etc/ndomod.cfg-sample /opt/nagios/etc/ndomod.cfg

And here is the Docker build output:

* Starting MySQL database server mysqld
su: warning: cannot change directory to /nonexistent: No such file or directory
...done.
/bin/sh: 1: printf "CREATE DATABASE nagios DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; " : not found
/bin/sh: 1: printf "CREATE USER ndoutils@localhost IDENTIFIED BY ndoutils_password ;" : not found
/bin/sh: 1: printf "GRANT ALL PRIVILEGES ON nagios.* TO ndoutils@localhost WITH GRANT OPTION ;" : not found
/bin/sh: 1: printf "GRANT USAGE ON *.* TO ndoutils@localhost WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;" : not found
Cloning into 'ndoutils'...
checking for a BSD-compatible install.../usr/bin/install -c
...

The console output:

8.323 DBI connect('nagios;localhost:','ndoutils',...) failed: Access denied for user 'ndoutils'@'localhost' (using password: YES) at ./installdb line 42.
------
Dockerfile:190
--------------------
 189 |     EXPOSE 3306
 190 | >>> RUN service mysql restart && \
 191 | >>>     'printf "CREATE DATABASE nagios DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; " ' | mysql -u root && \
 192 | >>>     'printf "CREATE USER 'ndoutils'@'localhost' IDENTIFIED BY 'ndoutils_password' ;" '| mysql -u root && \
 193 | >>>     'printf "GRANT ALL PRIVILEGES ON nagios.* TO 'ndoutils'@'localhost' WITH GRANT OPTION ;" ' | mysql -u root && \
 194 | >>>     'printf "GRANT USAGE ON *.* TO 'ndoutils'@'localhost' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;" ' | mysql -u root && \
 195 | >>>     git clone https://github.com/NagiosEnterprises/ndoutils.git && \
 196 | >>>     cd ndoutils && \
 197 | >>>     ./configure --prefix /opt/nagios --build=aarch64-unknown-linux-gnu --with-init-type=systemd &&\
 198 | >>>     make all && make install && make install-init && make install-config && \
 199 | >>>     cd db && \
 200 | >>>     ./installdb -u ndoutils -p 'ndoutils_password' -h localhost -d nagios
 201 |      
--------------------
ERROR: failed to solve: process "/bin/sh -c service mysql restart &&     'printf \"CREATE DATABASE nagios DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; \" ' | mysql -u root &&     'printf \"CREATE USER 'ndoutils'@'localhost' IDENTIFIED BY 'ndoutils_password' ;\" '| mysql -u root &&     'printf \"GRANT ALL PRIVILEGES ON nagios.* TO 'ndoutils'@'localhost' WITH GRANT OPTION ;\" ' | mysql -u root &&     'printf \"GRANT USAGE ON *.* TO 'ndoutils'@'localhost' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;\" ' | mysql -u root &&     git clone https://github.com/NagiosEnterprises/ndoutils.git &&     cd ndoutils &&     ./configure --prefix /opt/nagios --build=aarch64-unknown-linux-gnu --with-init-type=systemd &&    make all && make install && make install-init && make install-config &&     cd db &&     ./installdb -u ndoutils -p 'ndoutils_password' -h localhost -d nagios" did not complete successfully: exit code: 255

Looks like none of the SQL statements I am trying to run are executed, I have tried many format options…

Use 3 backticks before and after code/config to make it more readable and preserve spacing.

Hi

Your post is really difficult to read without a proper formating.

If I correctly understand you need a mysql instance, right ? If so, forget your RUN statements but simply create a .sql file and mount that file in your mysql container, in a very specific folder

See chapter “Initializing a fresh instance” on https://hub.docker.com/_/mysql,

Hi

Sorry for the formatting, I fixed it hopefully.

I need a mysql instance yes, but I also need a web server running on that container, i’d prefer to run both services on the same container, it looks like a format issue on the RUN command

After changing a bit the Dockerfile I get a different error

RUN apt-get update && apt-get install -y mysql-server libmysqlclient-dev libdbd-mysql-perl
EXPOSE 3306
RUN service mysql stop && \
    usermod -d /var/lib/mysql/ mysql && \
    service mysql start && \
    sh -c 'printf "CREATE DATABASE nagios DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; " ' | mysql -u root && \
    sh -c 'printf "CREATE USER 'ndoutils'@'localhost' IDENTIFIED BY 'ndoutils_password' ;" '| mysql -u root && \
    sh -c 'printf "GRANT ALL PRIVILEGES ON nagios.* TO 'ndoutils'@'localhost' WITH GRANT OPTION ;" ' | mysql -u root && \
    sh -c 'printf "GRANT USAGE ON *.* TO 'ndoutils'@'localhost' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;" ' | mysql -u root && \
    sh -c 'printf "FLUSH PRIVILEGES;" ' | mysql -u root && \
    git clone https://github.com/NagiosEnterprises/ndoutils.git && \
    cd ndoutils && \
    ./configure --prefix /opt/nagios --build=aarch64-unknown-linux-gnu --with-init-type=systemd &&\
    make all && make install && make install-init && make install-config && \
    cd db && \
    ./installdb -u ndoutils -p 'ndoutils_password' -h localhost -d nagios
 

[13/37] RUN service mysql stop && usermod -d /var/lib/mysql/ mysql && service mysql start && sh -c 'printf "CREATE DATABASE nagios DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; " ’ | mysql -u root && sh -c 'printf “CREATE USER ‘ndoutils’@‘localhost’ IDENTIFIED BY ‘ndoutils_password’ ;” '| mysql -u root && sh -c 'printf “GRANT ALL PRIVILEGES ON nagios.* TO ‘ndoutils’@‘localhost’ WITH GRANT OPTION ;” ’ | mysql -u root && sh -c 'printf “GRANT USAGE ON . TO ‘ndoutils’@‘localhost’ WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;” ’ | mysql -u root && sh -c 'printf “FLUSH PRIVILEGES;” ’ | mysql -u root && git clone GitHub - NagiosEnterprises/ndoutils: NDOUtils - Database Output for Nagios Core && cd ndoutils && ./configure --prefix /opt/nagios --build=aarch64-unknown-linux-gnu --with-init-type=systemd && make all && make install && make install-init && make install-config && cd db && ./installdb -u ndoutils -p ‘ndoutils_password’ -h localhost -d nagios:
0.085 * Stopping MySQL database server mysqld
0.100 …done.
0.118 * Starting MySQL database server mysqld
1.159 …done.
1.171 ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ndoutils_password’ at line 1

Why just one container for two services ? Did you have a really good reason for this ?

I don’t know any good reason to do this on my own.

Containers are not VMs. They are intended to run a single process with its dependencies (libraries) in isolation.