Skip to content

Commit

Permalink
#20 - Add MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
johanjanssens committed Oct 11, 2021
1 parent eb0a3f8 commit fcc04dc
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 4 deletions.
12 changes: 9 additions & 3 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ FROM ghcr.io/joomlatools/jt-base:latest AS base
ARG DEBIAN_FRONTEND=noninteractive

# HTTP Apache
EXPOSE 8080
# HTTPS Apache
EXPOSE 8443
EXPOSE 8080 8443
# HTTP Swoole - FastCGI
EXPOSE 8081
# HTTP Swoole - Webhooks
EXPOSE 8082
# MySQL
EXPOSE 3306 33060

# Install MySQL
COPY ./config/mysql /etc/mysql/

RUN apt-get install -y --no-install-recommends mysql-server php7.4-mysql; \
/bin/bash -e /var/scripts/mysql_initialize.sh \

##
# Stage: build
Expand Down
20 changes: 20 additions & 0 deletions config/mysql/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
secure-file-priv = NULL
default-time-zone = SYSTEM

# Make sure Data Dictionary is cross platform
# https://dev.mysql.com/doc/refman/8.0/en/identifier-case-sensitivity.html
lower_case_table_names = 1

# Custom config should go here
!includedir /etc/mysql/conf.d/
47 changes: 47 additions & 0 deletions config/s6/cont-init.d/110-mysql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/with-contenv bash

# https://github.com/docker-library/mysql/issues/275#issuecomment-292208567
# > mysqld --verbose --help | grep bind-address

file="${0##*/}"

MYSQL_DATA_DIR=$APP_VOLUME/mysql
MYSQL_PID_FILE=/var/run/mysqld/mysqld.pid

# Remove stale MySQL PID file left behind when docker stops container
if [[ -f $MYSQL_PID_FILE ]]; then
rm -f $MYSQL_PID_FILE
fi

# Initialize MySQL data directory (if needed)
# See https://dev.mysql.com/doc/refman/8.0/en/data-directory-initialization.html
if [[ ! -d $MYSQL_DATA_DIR ]]; then

echo "[cont-init.d] ${file}: An empty or uninitialized MySQL volume is detected in ${MYSQL_DATA_DIR}"
echo "[cont-init.d] ${file}: Installing MySQL in ${MYSQL_DATA_DIR} ..."
mkdir $MYSQL_DATA_DIR
chown --reference=/var/lib/mysql $MYSQL_DATA_DIR
chmod --reference=/var/lib/mysql $MYSQL_DATA_DIR
cp -R -p /var/lib/mysql/* $MYSQL_DATA_DIR

fi

# Grant or revoke passwordless remote access
/usr/bin/mysqld_safe --datadir=$MYSQL_DATA_DIR --user=mysql -D
if [[ $APP_ENV = "development" ]]
then
echo "[cont-init.d] ${file}: Granting remote access of MySQL database from any IP address"
/usr/bin/mysql -u root -e "
CREATE USER IF NOT EXISTS 'root'@'%';
GRANT ALL ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;
"
else
echo "[cont-init.d] ${file}: Revoking remote access of MySQL database from any IP address"
/usr/bin/mysql -u root -e -f "
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'root'@'%';
DROP USER IF EXISTS 'root'@'%';
"
fi

/usr/bin/mysqladmin -u root shutdown
3 changes: 3 additions & 0 deletions config/s6/cont-init.d/120-php.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/with-contenv bash

phpenmod -v 7.4 pdo_mysql
1 change: 1 addition & 0 deletions config/s6/services.d/mysql/down-signal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SIGQTERM
12 changes: 12 additions & 0 deletions config/s6/services.d/mysql/finish
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/execlineb -S1
#https://danyspin97.org/blog/getting-started-with-execline-scripting/

# @see https://github.com/just-containers/s6-overlay/issues/101

# Rely on container platform to restart this container on crash (marathon/docker-swarm/kubernetes)
# BUT, when container is SIGNALLED to stop, don't interfere
if { s6-test ${1} -ne 0 }
if { s6-test ${1} -ne 256 }

# When mysql process dies, the container should come down with it
s6-svscanctl -t /var/run/s6/services
3 changes: 3 additions & 0 deletions config/s6/services.d/mysql/log/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/execlineb -P

sed "s/^/[mysql] /" --unbuffered
1 change: 1 addition & 0 deletions config/s6/services.d/mysql/reload-signal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SIGUP
35 changes: 35 additions & 0 deletions config/s6/services.d/mysql/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/execlineb -P
#https://danyspin97.org/blog/getting-started-with-execline-scripting/

### Commands
# https://dev.mysql.com/doc/refman/8.0/en/unix-signal-response.html#server-signal-response
# https://skarnet.org/software/s6/s6-svc.html

# SIGUP: Reload the grant tables and to flush tables, logs, the thread cache, and the host cache
# > s6-svc -h /var/run/s6/services/mysql

# SIGUSR2: Shutdown
# > s6-svc -t /var/run/s6/services/mysql

# Status
# https://skarnet.org/software/s6/s6-svstat.html
# > s6-svstat /var/run/s6/services/mysql

# Explicitly load container environment
with-contenv

foreground
{
importas -D production APP_ENV APP_ENV
echo "Starting MySQL with environment:" $APP_ENV
}

fdmove -c 2 1 # redirect stderr into stdout

trap -x
{
term { mysqladmin -u root shutdown }
}

importas -D /mnt/www APP_VOLUME APP_VOLUME
exec /usr/bin/mysqld_safe --datadir=${APP_VOLUME}/mysql --user=mysql
26 changes: 26 additions & 0 deletions config/scripts/mysql_initialize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash -e

#-------------------------------------------------------------------
# Initialize mysql (if there is no datadir present).
#-------------------------------------------------------------------

MYSQL_DATA_DIR=${APP_VOLUME:=/mnt/www}/mysql
MYSQL_RUN_DIR=/var/run/mysqld

# Make sure lib dir exists
if [[ ! -d $MYSQL_DATA_DIR ]]; then
mkdir -p $MYSQL_DATA_DIR
fi

# Make sure run dir exists
if [[ ! -d $MYSQL_RUN_DIR ]]; then
mkdir -p $MYSQL_RUN_DIR
fi

# Ensure that /var/run/mysqld (used for socket and lock files) is writable
chown -R mysql:mysql $MYSQL_RUN_DIR $MYSQL_DATA_DIR
chmod 1777 $MYSQL_RUN_DIR $MYSQL_DATA_DIR

# Initialize MySQL data directory (if needed)
# See https://dev.mysql.com/doc/refman/8.0/en/data-directory-initialization.html
/usr/bin/mysqld_safe --initialize-insecure --datadir=${MYSQL_DATA_DIR} --user=mysql
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ services:
- "8080:8080" # HTTP Apache
- "8443:8443" # HTTPS Apache
- "8081:8081" # HTTP Service - FastCGI
- "8082:8082" # HTTP Service - Webhooks
- "8082:8082" # HTTP Service - Webhooks
- "3306:3306" # MySQL
- "33060:33060" # MySQL - X Plugin

0 comments on commit fcc04dc

Please sign in to comment.