-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.sh
83 lines (70 loc) · 2.79 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
set -e
: "${MYSQL_ROOT_PASSWORD:=O5Nz88BTPc6krzQ2gQuXJMOEroVKiY9po1LZSy1UQBk}"
: "${MYSQL_DATABASE:-wordpress}"
: "${MYSQL_USER:-wpuser}"
: "${MYSQL_PASSWORD:-wpuser}"
if [ ! -f /docker-entrypoint-initdb.d/flagfile ]; then
# If the command executed is 'mariadbd'
if [ "$1" = 'mariadbd' ]; then
# Check if we are running as root
if [ "$(id -u)" = '0' ]; then
# Start the MySQL server in the background as root
mysqld_safe &
# Wait for the server to start up
sleep 5s
# Set the root password only if it is not already set
if mysqladmin -u root password >/dev/null 2>&1; then
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';"
else
echo "Root user in MySQL has a password."
fi
# Stop the server
mysqladmin -u root -p"$MYSQL_ROOT_PASSWORD" shutdown
# If we are, use gosu to step down from root
exec gosu mysql "$BASH_SOURCE" "$@"
fi
# Start the MySQL server in the background
"$@" --skip-networking --socket=/tmp/mysql.sock &
# Wait for the server to start up
mysql=( mysql --protocol=socket -uroot -p"$MYSQL_ROOT_PASSWORD" -hlocalhost --socket=/tmp/mysql.sock )
for i in {30..0}; do
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
break
fi
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'MySQL init process failed.'
exit 1
fi
# Create a new database only if it does not already exist
if [ -n "$MYSQL_DATABASE" ]; then
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
fi
# Create a new user only if it does not already exist
if [ -n "$MYSQL_USER" -a -n "$MYSQL_PASSWORD" ]; then
echo "CREATE USER IF NOT EXISTS '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"
echo "GRANT ALL ON *.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
fi
# Stop the server
if ! mysqladmin --protocol=socket -uroot -p"$MYSQL_ROOT_PASSWORD" -hlocalhost --socket=/tmp/mysql.sock shutdown; then
echo >&2 'MySQL init process failed.'
exit 1
fi
echo
echo 'MySQL init process done. Ready for start up.'
echo
fi
else
echo 'Database initialization is already completed'
fi
if [ ! -f /docker-entrypoint-initdb.d/flagfile ]; then
touch /docker-entrypoint-initdb.d/flagfile
# Run the command (this will start the MySQL server)
exec "$@"
else
# Run the command (this will start the MySQL server)
exec gosu mysql "$@"
fi