{"id":77,"date":"2020-04-26T16:07:03","date_gmt":"2020-04-26T15:07:03","guid":{"rendered":"http:\/\/eggblog.invertedegg.com\/?p=77"},"modified":"2020-04-26T16:07:20","modified_gmt":"2020-04-26T15:07:20","slug":"solving-mysql-mariadb-startup-connection-problems-when-running-zoneminder-in-a-docker-image","status":"publish","type":"post","link":"http:\/\/eggblog.invertedegg.com\/?p=77","title":{"rendered":"Solving MySQL \/ MariaDB startup \/ connection problems when running ZoneMinder in a Docker image"},"content":{"rendered":"\n<p>After a few years away from <a href=\"https:\/\/zoneminder.com\/\">ZoneMinder<\/a>, I decided to reinstall, but this time using a Docker image.  I selected <a href=\"https:\/\/github.com\/dlandon\/zoneminder\">dlandon&#8217;s very helpful packaging<\/a> rather than attempting to create my own image.  I created the necessary directories (for the mapped in recording cache and for the MySQL database) and started up the image with the parameters as described in <a href=\"https:\/\/github.com\/dlandon\/zoneminder#zoneminder-docker\">dlandon&#8217;s documentation<\/a>.<\/p>\n\n\n\n<p>However, the image kept refusing to start for me.  I would get:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Starting MariaDB database server mysqld,\n...fail!<\/code><\/pre>\n\n\n\n<div class=\"wp-container-1 wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<p>First thing to do was to figure out what the problem was for mysqld.  You can&#8217;t just run a shell inside the image though, as the image has quit by the time you&#8217;re looking at it.  So let&#8217;s modify the image to start up with a bash shell (while keeping its state, so that we can check the logs, etc.) and poke around inside.  I recommend storing all this in a script so you can repeat it!  Here&#8217;s the script I used to commit a new copy with bash as the entry point, start it up, and then delete it afterwards (while it&#8217;s running you can exec bash in it from another terminal if you want to have multiple terminals poking around, which can be helpful):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>YOURCONFIGPATH=your config path goes here\nYOURCACHEPATH=your cache path goes here\nYOURUSERNAME=your user name goes here\n\n# Get the original container\nCONTAINERID=`docker ps -a | grep zoneminder | awk '{print $1}'`\n\n# Clone it\ndocker commit ${CONTAINERID} ${YOURUSERNAME}\/test\n\n# Start it with bash as the modified entry point\ndocker run -it --entrypoint=bash \\\n--name=\"Zoneminder2\" \\\n--privileged=\"true\" \\\n-p 8443:443\/tcp \\\n-p 9000:9000\/tcp \\\n-e TZ=\"America\/Chicago\" \\\n-e SHMEM=\"30%\" \\\n-e PUID=\"99\" \\\n-e PGID=\"100\" \\\n-v \"${YOURCONFIGPATH}\":\"\/config\":rw \\\n-v \"${YOURCACHEPATH}\":\"\/var\/cache\/zoneminder\":rw \\\n${YOURUSERNAME}\/test\n\n# Clean up afterwards ...\ndocker images -a | grep \"${YOURUSERNAME}\/test\" | awk '{print $3}' | xargs docker rmi -f\ndocker ps -a | grep \"Zoneminder2\" | awk '{print $1}' | xargs docker rm<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>Now you can poke around inside the container to figure out why mysqld isn&#8217;t happy running.  You&#8217;ll find your key log file here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/var\/log\/mysql\/error.log<\/code><\/pre>\n\n\n\n<p>You may want to clean out the log file (it probably has some goop inherited from the original docker build) and re-run the service to get a clean and easy view:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm \/var\/log\/mysql\/error.log\nservice mysql start<\/code><\/pre>\n\n\n\n<p>OK, so what you&#8217;re probably seeing in the log file is a warning about not being able to write a test file, and some following MariaDB errors about bad permissions (I&#8217;m no longer getting them, so this is from memory\/Googling others):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Warning] Can't create test file<\/code><\/pre>\n\n\n\n<p>This is indicating that MySQL can&#8217;t write to the directory you&#8217;ve pointed it to.<\/p>\n\n\n\n<p>First, ensure that there are no basic permissions problems.  I won&#8217;t get too into this, as you should be able to do this &#8212; but make sure that wherever you put the database (it&#8217;s going to be in ${CONFIG}\/mysql) is fully accessible (e.g., the directory path can all be read, the directory itself is writable, etc.)<\/p>\n\n\n\n<p>I&#8217;m assuming you have what look to be fine permissions &#8212; I sure did.<\/p>\n\n\n\n<p>At this point, you probably have one of three culprits:<\/p>\n\n\n\n<ol><li>If you located the CONFIG outside of the normal spots (let&#8217;s say, anywhere outside of \/var), then it&#8217;s possible you&#8217;re running into MariaDB&#8217;s own protection.  It&#8217;s got some settings to prevent writing to \/usr, \/etc, \/home, and so forth.  This wasn&#8217;t my problem, but I found it while trying to figure out what my problem was.  If this is your problem, easiest would be to relocate the directory, but if you can&#8217;t, you may find some help in tinkering with the <code>ProtectHome <\/code>and <code>ProtectSystem <\/code>settings for MySQL in the configuration (<em>inside the docker image!<\/em> &#8212; which introduces its own long-term complexities).<\/li><li>If you&#8217;re running SELinix in your host system, SELinux may be blocking access to your data directory.  I&#8217;m not, so I can&#8217;t give you details, but you may find <a href=\"https:\/\/blogs.oracle.com\/jsmyth\/selinux-and-mysql\">SELinux and MySQL by Jeremy Smyth<\/a> to be of use.<\/li><li>For me, the problem was that AppArmor (in my host system) was blocking access.  I could see this very clearly (once I started looking) by checking <code>\/var\/log\/syslog<\/code> and looking for <code>apparmor <\/code>lines.  You can find the fix for this in <a href=\"https:\/\/blogs.oracle.com\/jsmyth\/apparmor-and-mysql\">AppArmor and MySQL by Jeremy Smyth<\/a> .  In brief, the fix is to add two lines to the bottom of \/etc\/apparmor.d\/local\/usr.sbin.mysqld (customized for your data location, of course):<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\/YOURDATAPATH\/ r,\n\/YOURDATAPATH\/** rwk,<\/code><\/pre>\n\n\n\n<p>Et voila, everything started working!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After a few years away from ZoneMinder, I decided to reinstall, but this time using a Docker image. I selected dlandon&#8217;s very helpful packaging rather than attempting to create my own image. I created the necessary directories (for the mapped in recording cache and for the MySQL database) and started up the image with the &hellip; <a href=\"http:\/\/eggblog.invertedegg.com\/?p=77\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Solving MySQL \/ MariaDB startup \/ connection problems when running ZoneMinder in a Docker image&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[15,17,16,11,14],"_links":{"self":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts\/77"}],"collection":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=77"}],"version-history":[{"count":1,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":78,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions\/78"}],"wp:attachment":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}