Skip to content

Commit 452212a

Browse files
committed
Use sqlite as database instead of mysql.
1 parent be87828 commit 452212a

File tree

9 files changed

+410
-131
lines changed

9 files changed

+410
-131
lines changed

Dockerfile

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,16 @@ RUN \
114114
RUN \
115115
add-pkg \
116116
nodejs \
117-
mariadb \
118-
mariadb-client \
119-
mariadb-server-utils \
117+
sqlite \
120118
certbot \
121119
openssl \
122120
apache2-utils \
123121
logrotate \
122+
# For openresty
123+
pcre \
124124
&& \
125125
# Adjust the logrotate config file.
126-
sed-patch 's|^/var/log/messages|#/var/log/messages|' /etc/logrotate.conf && \
127-
# Clean some uneeded stuff from mariadb.
128-
rm -r \
129-
/var/lib/mysql \
130-
&& \
131-
# Make sure mariadb listen on port 3306
132-
sed-patch 's/^skip-networking/#skip-networking/' /etc/my.cnf.d/mariadb-server.cnf
126+
sed-patch 's|^/var/log/messages|#/var/log/messages|' /etc/logrotate.conf
133127

134128
# Install Nginx Proxy Manager.
135129
RUN \

rootfs/defaults/production.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"database": {
3-
"engine": "mysql",
4-
"host": "127.0.0.1",
5-
"name": "nginxproxymanager",
6-
"user": "nginxproxymanager",
7-
"password": "password123",
8-
"port": 3306
3+
"engine": "knex-native",
4+
"knex": {
5+
"client": "sqlite3",
6+
"connection": {
7+
"filename": "/config/database.sqlite"
8+
},
9+
"useNullAsDefault": true
10+
}
911
}
1012
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/with-contenv sh
2+
3+
set -e # Exit immediately if a command exits with a non-zero status.
4+
set -u # Treat unset variables as an error.
5+
6+
log() {
7+
if [ -n "${1-}" ]; then
8+
echo "[cont-init.d] $(basename $0): $*"
9+
else
10+
while read OUTPUT; do
11+
echo "[cont-init.d] $(basename $0): $OUTPUT"
12+
done
13+
fi
14+
}
15+
16+
check_db() {
17+
echo 'SELECT 1' | mysql &> /dev/null
18+
}
19+
20+
install_db() {
21+
log "Installing database..."
22+
add-pkg --virtual upgrade-deps mariadb mariadb-client jq | log
23+
# Make sure mariadb listen on port 3306
24+
#sed-patch 's/^skip-networking/#skip-networking/' /etc/my.cnf.d/mariadb-server.cnf
25+
}
26+
27+
uninstall_db() {
28+
log "Uninstalling database..."
29+
del-pkg upgrade-deps | log
30+
}
31+
32+
start_db() {
33+
log "Starting database..."
34+
35+
# Start mysqld.
36+
mkdir -p /run/mysqld
37+
/usr/bin/mysqld --user=$(whoami) --datadir /config/mysql --tmpdir /tmp/ &
38+
pid="$!"
39+
40+
# Wait until it is ready.
41+
for i in $(seq 1 30); do
42+
if check_db; then
43+
break
44+
fi
45+
sleep 1
46+
done
47+
48+
if ! check_db; then
49+
log "ERROR: Failed to start the database."
50+
exit 1
51+
fi
52+
}
53+
54+
stop_db() {
55+
# Kill mysqld.
56+
log "Shutting down database..."
57+
if ! kill -s TERM "$pid" || ! wait "$pid"; then
58+
log "ERROR: Failed to stop the database."
59+
exit 1
60+
fi
61+
}
62+
63+
# Check if a mysql database exists.
64+
if [ ! -d /config/mysql ]; then
65+
exit 0
66+
fi
67+
68+
# Handle case where a previous conversion didn't went well.
69+
if [ -f /config/db_convert_in_progress ]; then
70+
rm -f /config/database.sqlite
71+
fi
72+
73+
log "MySQL database conversion needed."
74+
touch /config/db_convert_in_progress
75+
76+
# Temporarily start the database.
77+
install_db
78+
start_db
79+
80+
# Dump the database.
81+
log "Dumping database..."
82+
/usr/bin/mysqldump --skip-extended-insert --compact nginxproxymanager > /tmp/mysqldump.sql
83+
84+
# Convert the database.
85+
log "Converting database..."
86+
/opt/nginx-proxy-manager/mysql2sqlite /tmp/mysqldump.sql | sqlite3 /config/database.sqlite
87+
88+
# Update the database settings in configuration.
89+
if [ -f /config/production.json ]; then
90+
if [ "$(jq -r '.database.engine' /config/production.json)" == "mysql" ]
91+
then
92+
log "Updating database settings in config file..."
93+
jq -n 'input | .database = input.database' /config/production.json /defaults/production.json > /config/production.json.updated
94+
fi
95+
fi
96+
97+
# Database converted properly.
98+
log "Database conversion done."
99+
rm /config/db_convert_in_progress
100+
rm /tmp/mysqldump.sql
101+
mv /config/mysql /config/mysql.converted
102+
if [ -f /config/production.json.updated ]; then
103+
mv /config/production.json /config/production.json.old
104+
mv /config/production.json.updated /config/production.json
105+
fi
106+
107+
# Stop the database.
108+
stop_db
109+
uninstall_db
110+
111+
# vim:ft=sh:ts=4:sw=4:et:sts=4

rootfs/etc/cont-init.d/nginx-proxy-manager.sh

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,6 @@ log() {
77
echo "[cont-init.d] $(basename $0): $*"
88
}
99

10-
start_db() {
11-
log "Starting database..."
12-
13-
# Start mysqld.
14-
CUR_PWD="$(pwd)"
15-
cd /etc/services.d/mysqld
16-
./run &
17-
pid="$!"
18-
cd "$CUR_PWD"
19-
20-
# Wait until it is ready.
21-
for i in $(seq 1 30); do
22-
if /etc/services.d/mysqld/data/check; then
23-
break
24-
fi
25-
sleep 1
26-
done
27-
28-
if ! /etc/services.d/mysqld/data/check; then
29-
log "ERROR: Failed to start the database."
30-
exit 1
31-
fi
32-
}
33-
34-
stop_db() {
35-
# Kill mysqld.
36-
log "Shutting down database..."
37-
if ! kill -s TERM "$pid" || ! wait "$pid"; then
38-
log "ERROR: initialization failed."
39-
exit 1
40-
fi
41-
}
42-
4310
# Make sure mandatory directories exist.
4411
mkdir -p \
4512
/config/log/nginx \
@@ -78,62 +45,12 @@ done
7845
[ -f /config/production.json ] || cp /defaults/production.json /config/
7946
[ -f $XDG_CONFIG_HOME/letsencrypt/cli.ini ] || cp /defaults/cli.ini $XDG_CONFIG_HOME/letsencrypt/
8047

81-
# Protect against database initialization failure: make sure to remove the
82-
# database directory if it didn't initialized properly.
83-
if [ -d /config/mysql ] && [ -f /config/db_init_in_progress ]; then
84-
rm -r /config/mysql
85-
fi
86-
87-
# Create the database directory if required.
88-
if [ ! -d /config/mysql ]; then
89-
touch /config/db_init_in_progress
90-
91-
log "Initializing database data directory..."
92-
mysql_install_db --datadir=/config/mysql >/config/log/init_db.log 2>&1
93-
chown -R $USER_ID:$GROUP_ID /config/mysql
94-
log "Database data directory initialized."
95-
fi
96-
97-
# Temporarily start the database.
98-
start_db
99-
100-
# Initialize the database if required.
101-
if [ -f /config/db_init_in_progress ]; then
102-
MYSQL_DATABASE=nginxproxymanager
103-
MYSQL_USER=nginxproxymanager
104-
MYSQL_PASSWORD=password123
105-
106-
# Secure the installation.
107-
log "Securing database installation..."
108-
printf '\nn\n\n\n\n\n' | /usr/bin/mysql_secure_installation >>/config/log/init_db.log 2>&1
109-
110-
log "Initializing database ..."
111-
112-
# Create the database.
113-
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | mysql >>/config/log/init_db.log 2>&1
114-
# Create the user.
115-
echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | mysql >>/config/log/init_db.log 2>&1
116-
echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | mysql >>/config/log/init_db.log 2>&1
117-
fi
118-
119-
# Make sure to keep the database upgraded.
120-
if [ ! -f /config/db_init_in_progress ]; then
121-
log "Upgrading database if required..."
122-
/usr/bin/mysql_upgrade --silent
123-
fi
124-
12548
# Make sure there is no migration lock held.
12649
# See https://github.com/jlesage/docker-nginx-proxy-manager/issues/4
127-
if [ ! -f /config/db_init_in_progress ]; then
128-
echo 'DELETE FROM nginxproxymanager.migrations_lock WHERE is_locked = 1;' | mysql
50+
if [ -f /config/database.sqlite ]; then
51+
echo 'DELETE FROM migrations_lock WHERE is_locked = 1;' | sqlite3 /config/database.sqlite
12952
fi
13053

131-
# Database initialized properly.
132-
rm -f /config/db_init_in_progress
133-
134-
# Stop the database.
135-
stop_db
136-
13754
# Generate dummy self-signed certificate.
13855
if [ ! -f /config/nginx/dummycert.pem ] || [ ! -f /config/nginx/dummykey.pem ]
13956
then

rootfs/etc/services.d/mysqld/data/check

Lines changed: 0 additions & 8 deletions
This file was deleted.

rootfs/etc/services.d/mysqld/notification-fd

Lines changed: 0 additions & 1 deletion
This file was deleted.

rootfs/etc/services.d/mysqld/run

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)