Spaces:
Running
Running
ds commited on
Commit ·
21602df
1
Parent(s): 33a7042
- Dockerfile +27 -3
- docker-entrypoint.sh +420 -0
Dockerfile
CHANGED
|
@@ -15,6 +15,22 @@ RUN mv composer.phar /usr/local/bin/composer
|
|
| 15 |
#RUN tar -xf '/node-v20.17.0-linux-x64.tar.xz'
|
| 16 |
#RUN cp -r /node-v20.17.0-linux-x64/bin /node-v20.17.0-linux-x64/include /node-v20.17.0-linux-x64/lib /node-v20.17.0-linux-x64/share /usr/
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
RUN composer create-project laravel/laravel example-app
|
| 19 |
RUN cd example-app
|
| 20 |
WORKDIR /example-app
|
|
@@ -24,10 +40,18 @@ RUN chmod 706 -R /example-app/bootstrap/cache
|
|
| 24 |
RUN chmod 760 -R /example-app/storage
|
| 25 |
RUN chmod 706 /example-app/database/database.sqlite
|
| 26 |
RUN chown -R ubuntu:ubuntu /example-app
|
| 27 |
-
RUN apt-get install -y mariadb-server sudo
|
| 28 |
RUN usermod -aG sudo ubuntu
|
| 29 |
USER root:root
|
| 30 |
-
RUN sudo service mariadb start; service --status-all
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
RUN chmod -R 777 /var/run
|
| 33 |
RUN chmod -R 777 /run/mysqld
|
|
@@ -35,4 +59,4 @@ RUN chmod -R 777 /var/lib/mysql
|
|
| 35 |
|
| 36 |
EXPOSE 7860
|
| 37 |
|
| 38 |
-
CMD [ "bash","-c" , "sudo su; id; whoami; ls -la database;
|
|
|
|
| 15 |
#RUN tar -xf '/node-v20.17.0-linux-x64.tar.xz'
|
| 16 |
#RUN cp -r /node-v20.17.0-linux-x64/bin /node-v20.17.0-linux-x64/include /node-v20.17.0-linux-x64/lib /node-v20.17.0-linux-x64/share /usr/
|
| 17 |
|
| 18 |
+
|
| 19 |
+
# Cập nhật và cài đặt MySQL
|
| 20 |
+
RUN apt-get update && \
|
| 21 |
+
DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
|
| 22 |
+
|
| 23 |
+
# Sao chép file cấu hình MySQL
|
| 24 |
+
COPY mysql.cnf /etc/mysql/my.cnf
|
| 25 |
+
|
| 26 |
+
# Đặt mật khẩu root MySQL thông qua debconf
|
| 27 |
+
ENV MYSQL_ROOT_PASSWORD sa
|
| 28 |
+
ENV MYSQL_DATABASE=firecomic_db
|
| 29 |
+
|
| 30 |
+
RUN echo "mysql-server mysql-server/root_password password sa" | debconf-set-selections && \
|
| 31 |
+
echo "mysql-server mysql-server/root_password_again password sa" | debconf-set-selections
|
| 32 |
+
|
| 33 |
+
|
| 34 |
RUN composer create-project laravel/laravel example-app
|
| 35 |
RUN cd example-app
|
| 36 |
WORKDIR /example-app
|
|
|
|
| 40 |
RUN chmod 760 -R /example-app/storage
|
| 41 |
RUN chmod 706 /example-app/database/database.sqlite
|
| 42 |
RUN chown -R ubuntu:ubuntu /example-app
|
| 43 |
+
#RUN apt-get install -y mariadb-server sudo
|
| 44 |
RUN usermod -aG sudo ubuntu
|
| 45 |
USER root:root
|
| 46 |
+
#RUN sudo service mariadb start; service --status-all
|
| 47 |
+
|
| 48 |
+
RUN mkdir /docker-entrypoint-initdb.d
|
| 49 |
+
|
| 50 |
+
ADD docker-entrypoint.sh /docker-entrypoint.sh
|
| 51 |
+
|
| 52 |
+
ADD docker-entrypoint.sh /usr/local/bin/
|
| 53 |
+
|
| 54 |
+
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
|
| 55 |
|
| 56 |
RUN chmod -R 777 /var/run
|
| 57 |
RUN chmod -R 777 /run/mysqld
|
|
|
|
| 59 |
|
| 60 |
EXPOSE 7860
|
| 61 |
|
| 62 |
+
CMD [ "bash","-c" , "sudo su; id; whoami; ls -la database; /usr/local/bin/docker-entrypoint.sh mysqld ; php artisan serve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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -eo pipefail
|
| 3 |
+
shopt -s nullglob
|
| 4 |
+
|
| 5 |
+
# logging functions
|
| 6 |
+
mysql_log() {
|
| 7 |
+
local type="$1"; shift
|
| 8 |
+
# accept argument string or stdin
|
| 9 |
+
local text="$*"; if [ "$#" -eq 0 ]; then text="$(cat)"; fi
|
| 10 |
+
local dt; dt="$(date --rfc-3339=seconds)"
|
| 11 |
+
printf '%s [%s] [Entrypoint]: %s\n' "$dt" "$type" "$text"
|
| 12 |
+
}
|
| 13 |
+
mysql_note() {
|
| 14 |
+
mysql_log Note "$@"
|
| 15 |
+
}
|
| 16 |
+
mysql_warn() {
|
| 17 |
+
mysql_log Warn "$@" >&2
|
| 18 |
+
}
|
| 19 |
+
mysql_error() {
|
| 20 |
+
mysql_log ERROR "$@" >&2
|
| 21 |
+
exit 1
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# usage: file_env VAR [DEFAULT]
|
| 25 |
+
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
|
| 26 |
+
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
|
| 27 |
+
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
|
| 28 |
+
file_env() {
|
| 29 |
+
local var="$1"
|
| 30 |
+
local fileVar="${var}_FILE"
|
| 31 |
+
local def="${2:-}"
|
| 32 |
+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
|
| 33 |
+
mysql_error "Both $var and $fileVar are set (but are exclusive)"
|
| 34 |
+
fi
|
| 35 |
+
local val="$def"
|
| 36 |
+
if [ "${!var:-}" ]; then
|
| 37 |
+
val="${!var}"
|
| 38 |
+
elif [ "${!fileVar:-}" ]; then
|
| 39 |
+
val="$(< "${!fileVar}")"
|
| 40 |
+
fi
|
| 41 |
+
export "$var"="$val"
|
| 42 |
+
unset "$fileVar"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# check to see if this file is being run or sourced from another script
|
| 46 |
+
_is_sourced() {
|
| 47 |
+
# https://unix.stackexchange.com/a/215279
|
| 48 |
+
[ "${#FUNCNAME[@]}" -ge 2 ] \
|
| 49 |
+
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
|
| 50 |
+
&& [ "${FUNCNAME[1]}" = 'source' ]
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# usage: docker_process_init_files [file [file [...]]]
|
| 54 |
+
# ie: docker_process_init_files /always-initdb.d/*
|
| 55 |
+
# process initializer files, based on file extensions
|
| 56 |
+
docker_process_init_files() {
|
| 57 |
+
# mysql here for backwards compatibility "${mysql[@]}"
|
| 58 |
+
mysql=( docker_process_sql )
|
| 59 |
+
|
| 60 |
+
echo
|
| 61 |
+
local f
|
| 62 |
+
for f; do
|
| 63 |
+
case "$f" in
|
| 64 |
+
*.sh)
|
| 65 |
+
# https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
|
| 66 |
+
# https://github.com/docker-library/postgres/pull/452
|
| 67 |
+
if [ -x "$f" ]; then
|
| 68 |
+
mysql_note "$0: running $f"
|
| 69 |
+
"$f"
|
| 70 |
+
else
|
| 71 |
+
mysql_note "$0: sourcing $f"
|
| 72 |
+
. "$f"
|
| 73 |
+
fi
|
| 74 |
+
;;
|
| 75 |
+
*.sql) mysql_note "$0: running $f"; docker_process_sql < "$f"; echo ;;
|
| 76 |
+
*.sql.bz2) mysql_note "$0: running $f"; bunzip2 -c "$f" | docker_process_sql; echo ;;
|
| 77 |
+
*.sql.gz) mysql_note "$0: running $f"; gunzip -c "$f" | docker_process_sql; echo ;;
|
| 78 |
+
*.sql.xz) mysql_note "$0: running $f"; xzcat "$f" | docker_process_sql; echo ;;
|
| 79 |
+
*.sql.zst) mysql_note "$0: running $f"; zstd -dc "$f" | docker_process_sql; echo ;;
|
| 80 |
+
*) mysql_warn "$0: ignoring $f" ;;
|
| 81 |
+
esac
|
| 82 |
+
echo
|
| 83 |
+
done
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
# arguments necessary to run "mysqld --verbose --help" successfully (used for testing configuration validity and for extracting default/configured values)
|
| 87 |
+
_verboseHelpArgs=(
|
| 88 |
+
--verbose --help
|
| 89 |
+
--log-bin-index="$(mktemp -u)" # https://github.com/docker-library/mysql/issues/136
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
mysql_check_config() {
|
| 93 |
+
local toRun=( "$@" "${_verboseHelpArgs[@]}" ) errors
|
| 94 |
+
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
|
| 95 |
+
mysql_error $'mysqld failed while attempting to check config\n\tcommand was: '"${toRun[*]}"$'\n\t'"$errors"
|
| 96 |
+
fi
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
# Fetch value from server config
|
| 100 |
+
# We use mysqld --verbose --help instead of my_print_defaults because the
|
| 101 |
+
# latter only show values present in config files, and not server defaults
|
| 102 |
+
mysql_get_config() {
|
| 103 |
+
local conf="$1"; shift
|
| 104 |
+
"$@" "${_verboseHelpArgs[@]}" 2>/dev/null \
|
| 105 |
+
| awk -v conf="$conf" '$1 == conf && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
|
| 106 |
+
# match "datadir /some/path with/spaces in/it here" but not "--xyz=abc\n datadir (xyz)"
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
# Ensure that the package default socket can also be used
|
| 110 |
+
# since rpm packages are compiled with a different socket location
|
| 111 |
+
# and "mysqlsh --mysql" doesn't read the [client] config
|
| 112 |
+
# related to https://github.com/docker-library/mysql/issues/829
|
| 113 |
+
mysql_socket_fix() {
|
| 114 |
+
local defaultSocket
|
| 115 |
+
defaultSocket="$(mysql_get_config 'socket' mysqld --no-defaults)"
|
| 116 |
+
if [ "$defaultSocket" != "$SOCKET" ]; then
|
| 117 |
+
ln -sfTv "$SOCKET" "$defaultSocket" || :
|
| 118 |
+
fi
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
# Do a temporary startup of the MySQL server, for init purposes
|
| 122 |
+
docker_temp_server_start() {
|
| 123 |
+
# For 5.7+ the server is ready for use as soon as startup command unblocks
|
| 124 |
+
if ! "$@" --daemonize --skip-networking --default-time-zone=SYSTEM --socket="${SOCKET}"; then
|
| 125 |
+
mysql_error "Unable to start server."
|
| 126 |
+
fi
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
# Stop the server. When using a local socket file mysqladmin will block until
|
| 130 |
+
# the shutdown is complete.
|
| 131 |
+
docker_temp_server_stop() {
|
| 132 |
+
if ! mysqladmin --defaults-extra-file=<( _mysql_passfile ) shutdown -uroot --socket="${SOCKET}"; then
|
| 133 |
+
mysql_error "Unable to shut down server."
|
| 134 |
+
fi
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
# Verify that the minimally required password settings are set for new databases.
|
| 138 |
+
docker_verify_minimum_env() {
|
| 139 |
+
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
|
| 140 |
+
mysql_error <<-'EOF'
|
| 141 |
+
Database is uninitialized and password option is not specified
|
| 142 |
+
You need to specify one of the following as an environment variable:
|
| 143 |
+
- MYSQL_ROOT_PASSWORD
|
| 144 |
+
- MYSQL_ALLOW_EMPTY_PASSWORD
|
| 145 |
+
- MYSQL_RANDOM_ROOT_PASSWORD
|
| 146 |
+
EOF
|
| 147 |
+
fi
|
| 148 |
+
|
| 149 |
+
# This will prevent the CREATE USER from failing (and thus exiting with a half-initialized database)
|
| 150 |
+
if [ "$MYSQL_USER" = 'root' ]; then
|
| 151 |
+
mysql_error <<-'EOF'
|
| 152 |
+
MYSQL_USER="root", MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user
|
| 153 |
+
Remove MYSQL_USER="root" and use one of the following to control the root user password:
|
| 154 |
+
- MYSQL_ROOT_PASSWORD
|
| 155 |
+
- MYSQL_ALLOW_EMPTY_PASSWORD
|
| 156 |
+
- MYSQL_RANDOM_ROOT_PASSWORD
|
| 157 |
+
EOF
|
| 158 |
+
fi
|
| 159 |
+
|
| 160 |
+
# warn when missing one of MYSQL_USER or MYSQL_PASSWORD
|
| 161 |
+
if [ -n "$MYSQL_USER" ] && [ -z "$MYSQL_PASSWORD" ]; then
|
| 162 |
+
mysql_warn 'MYSQL_USER specified, but missing MYSQL_PASSWORD; MYSQL_USER will not be created'
|
| 163 |
+
elif [ -z "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ]; then
|
| 164 |
+
mysql_warn 'MYSQL_PASSWORD specified, but missing MYSQL_USER; MYSQL_PASSWORD will be ignored'
|
| 165 |
+
fi
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
# creates folders for the database
|
| 169 |
+
# also ensures permission for user mysql of run as root
|
| 170 |
+
docker_create_db_directories() {
|
| 171 |
+
local user; user="$(id -u)"
|
| 172 |
+
|
| 173 |
+
local -A dirs=( ["$DATADIR"]=1 )
|
| 174 |
+
local dir
|
| 175 |
+
dir="$(dirname "$SOCKET")"
|
| 176 |
+
dirs["$dir"]=1
|
| 177 |
+
|
| 178 |
+
# "datadir" and "socket" are already handled above (since they were already queried previously)
|
| 179 |
+
local conf
|
| 180 |
+
for conf in \
|
| 181 |
+
general-log-file \
|
| 182 |
+
keyring_file_data \
|
| 183 |
+
pid-file \
|
| 184 |
+
secure-file-priv \
|
| 185 |
+
slow-query-log-file \
|
| 186 |
+
; do
|
| 187 |
+
dir="$(mysql_get_config "$conf" "$@")"
|
| 188 |
+
|
| 189 |
+
# skip empty values
|
| 190 |
+
if [ -z "$dir" ] || [ "$dir" = 'NULL' ]; then
|
| 191 |
+
continue
|
| 192 |
+
fi
|
| 193 |
+
case "$conf" in
|
| 194 |
+
secure-file-priv)
|
| 195 |
+
# already points at a directory
|
| 196 |
+
;;
|
| 197 |
+
*)
|
| 198 |
+
# other config options point at a file, but we need the directory
|
| 199 |
+
dir="$(dirname "$dir")"
|
| 200 |
+
;;
|
| 201 |
+
esac
|
| 202 |
+
|
| 203 |
+
dirs["$dir"]=1
|
| 204 |
+
done
|
| 205 |
+
|
| 206 |
+
mkdir -p "${!dirs[@]}"
|
| 207 |
+
|
| 208 |
+
if [ "$user" = "0" ]; then
|
| 209 |
+
# this will cause less disk access than `chown -R`
|
| 210 |
+
find "${!dirs[@]}" \! -user mysql -exec chown --no-dereference mysql '{}' +
|
| 211 |
+
fi
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
# initializes the database directory
|
| 215 |
+
docker_init_database_dir() {
|
| 216 |
+
mysql_note "Initializing database files"
|
| 217 |
+
"$@" --initialize-insecure --default-time-zone=SYSTEM --autocommit=1
|
| 218 |
+
# explicitly enable autocommit to combat https://bugs.mysql.com/bug.php?id=110535 (TODO remove this when 8.0 is EOL; see https://github.com/mysql/mysql-server/commit/7dbf4f80ed15f3c925cfb2b834142f23a2de719a)
|
| 219 |
+
mysql_note "Database files initialized"
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
# Loads various settings that are used elsewhere in the script
|
| 223 |
+
# This should be called after mysql_check_config, but before any other functions
|
| 224 |
+
docker_setup_env() {
|
| 225 |
+
# Get config
|
| 226 |
+
declare -g DATADIR SOCKET
|
| 227 |
+
DATADIR="$(mysql_get_config 'datadir' "$@")"
|
| 228 |
+
SOCKET="$(mysql_get_config 'socket' "$@")"
|
| 229 |
+
|
| 230 |
+
# Initialize values that might be stored in a file
|
| 231 |
+
file_env 'MYSQL_ROOT_HOST' '%'
|
| 232 |
+
file_env 'MYSQL_DATABASE'
|
| 233 |
+
file_env 'MYSQL_USER'
|
| 234 |
+
file_env 'MYSQL_PASSWORD'
|
| 235 |
+
file_env 'MYSQL_ROOT_PASSWORD'
|
| 236 |
+
|
| 237 |
+
declare -g DATABASE_ALREADY_EXISTS
|
| 238 |
+
if [ -d "$DATADIR/mysql" ]; then
|
| 239 |
+
DATABASE_ALREADY_EXISTS='true'
|
| 240 |
+
fi
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
# Execute sql script, passed via stdin
|
| 244 |
+
# usage: docker_process_sql [--dont-use-mysql-root-password] [mysql-cli-args]
|
| 245 |
+
# ie: docker_process_sql --database=mydb <<<'INSERT ...'
|
| 246 |
+
# ie: docker_process_sql --dont-use-mysql-root-password --database=mydb <my-file.sql
|
| 247 |
+
docker_process_sql() {
|
| 248 |
+
passfileArgs=()
|
| 249 |
+
if [ '--dont-use-mysql-root-password' = "$1" ]; then
|
| 250 |
+
passfileArgs+=( "$1" )
|
| 251 |
+
shift
|
| 252 |
+
fi
|
| 253 |
+
# args sent in can override this db, since they will be later in the command
|
| 254 |
+
if [ -n "$MYSQL_DATABASE" ]; then
|
| 255 |
+
set -- --database="$MYSQL_DATABASE" "$@"
|
| 256 |
+
fi
|
| 257 |
+
|
| 258 |
+
mysql --defaults-extra-file=<( _mysql_passfile "${passfileArgs[@]}") --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" --comments "$@"
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
# Initializes database with timezone info and root password, plus optional extra db/user
|
| 262 |
+
docker_setup_db() {
|
| 263 |
+
# Load timezone info into database
|
| 264 |
+
if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
|
| 265 |
+
# sed is for https://bugs.mysql.com/bug.php?id=20545
|
| 266 |
+
mysql_tzinfo_to_sql /usr/share/zoneinfo \
|
| 267 |
+
| sed 's/Local time zone must be set--see zic manual page/FCTY/' \
|
| 268 |
+
| docker_process_sql --dont-use-mysql-root-password --database=mysql
|
| 269 |
+
# tell docker_process_sql to not use MYSQL_ROOT_PASSWORD since it is not set yet
|
| 270 |
+
fi
|
| 271 |
+
# Generate random root password
|
| 272 |
+
if [ -n "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
|
| 273 |
+
MYSQL_ROOT_PASSWORD="$(openssl rand -base64 24)"; export MYSQL_ROOT_PASSWORD
|
| 274 |
+
mysql_note "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
|
| 275 |
+
fi
|
| 276 |
+
# Sets root password and creates root users for non-localhost hosts
|
| 277 |
+
local rootCreate=
|
| 278 |
+
# default root to listen for connections from anywhere
|
| 279 |
+
if [ -n "$MYSQL_ROOT_HOST" ] && [ "$MYSQL_ROOT_HOST" != 'localhost' ]; then
|
| 280 |
+
# no, we don't care if read finds a terminating character in this heredoc
|
| 281 |
+
# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
|
| 282 |
+
read -r -d '' rootCreate <<-EOSQL || true
|
| 283 |
+
CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
|
| 284 |
+
GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
|
| 285 |
+
EOSQL
|
| 286 |
+
fi
|
| 287 |
+
|
| 288 |
+
local passwordSet=
|
| 289 |
+
# no, we don't care if read finds a terminating character in this heredoc (see above)
|
| 290 |
+
read -r -d '' passwordSet <<-EOSQL || true
|
| 291 |
+
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
|
| 292 |
+
EOSQL
|
| 293 |
+
|
| 294 |
+
# tell docker_process_sql to not use MYSQL_ROOT_PASSWORD since it is just now being set
|
| 295 |
+
docker_process_sql --dont-use-mysql-root-password --database=mysql <<-EOSQL
|
| 296 |
+
-- enable autocommit explicitly (in case it was disabled globally)
|
| 297 |
+
SET autocommit = 1;
|
| 298 |
+
|
| 299 |
+
-- What's done in this file shouldn't be replicated
|
| 300 |
+
-- or products like mysql-fabric won't work
|
| 301 |
+
SET @@SESSION.SQL_LOG_BIN=0;
|
| 302 |
+
|
| 303 |
+
${passwordSet}
|
| 304 |
+
GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
|
| 305 |
+
FLUSH PRIVILEGES ;
|
| 306 |
+
${rootCreate}
|
| 307 |
+
DROP DATABASE IF EXISTS test ;
|
| 308 |
+
EOSQL
|
| 309 |
+
|
| 310 |
+
# Creates a custom database and user if specified
|
| 311 |
+
if [ -n "$MYSQL_DATABASE" ]; then
|
| 312 |
+
mysql_note "Creating database ${MYSQL_DATABASE}"
|
| 313 |
+
docker_process_sql --database=mysql <<<"CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;"
|
| 314 |
+
fi
|
| 315 |
+
|
| 316 |
+
if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ]; then
|
| 317 |
+
mysql_note "Creating user ${MYSQL_USER}"
|
| 318 |
+
docker_process_sql --database=mysql <<<"CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;"
|
| 319 |
+
|
| 320 |
+
if [ -n "$MYSQL_DATABASE" ]; then
|
| 321 |
+
mysql_note "Giving user ${MYSQL_USER} access to schema ${MYSQL_DATABASE}"
|
| 322 |
+
docker_process_sql --database=mysql <<<"GRANT ALL ON \`${MYSQL_DATABASE//_/\\_}\`.* TO '$MYSQL_USER'@'%' ;"
|
| 323 |
+
fi
|
| 324 |
+
fi
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
_mysql_passfile() {
|
| 328 |
+
# echo the password to the "file" the client uses
|
| 329 |
+
# the client command will use process substitution to create a file on the fly
|
| 330 |
+
# ie: --defaults-extra-file=<( _mysql_passfile )
|
| 331 |
+
if [ '--dont-use-mysql-root-password' != "$1" ] && [ -n "$MYSQL_ROOT_PASSWORD" ]; then
|
| 332 |
+
cat <<-EOF
|
| 333 |
+
[client]
|
| 334 |
+
password="${MYSQL_ROOT_PASSWORD}"
|
| 335 |
+
EOF
|
| 336 |
+
fi
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
# Mark root user as expired so the password must be changed before anything
|
| 340 |
+
# else can be done (only supported for 5.6+)
|
| 341 |
+
mysql_expire_root_user() {
|
| 342 |
+
if [ -n "$MYSQL_ONETIME_PASSWORD" ]; then
|
| 343 |
+
docker_process_sql --database=mysql <<-EOSQL
|
| 344 |
+
ALTER USER 'root'@'%' PASSWORD EXPIRE;
|
| 345 |
+
EOSQL
|
| 346 |
+
fi
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
# check arguments for an option that would cause mysqld to stop
|
| 350 |
+
# return true if there is one
|
| 351 |
+
_mysql_want_help() {
|
| 352 |
+
local arg
|
| 353 |
+
for arg; do
|
| 354 |
+
case "$arg" in
|
| 355 |
+
-'?'|--help|--print-defaults|-V|--version)
|
| 356 |
+
return 0
|
| 357 |
+
;;
|
| 358 |
+
esac
|
| 359 |
+
done
|
| 360 |
+
return 1
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
_main() {
|
| 364 |
+
# if command starts with an option, prepend mysqld
|
| 365 |
+
if [ "${1:0:1}" = '-' ]; then
|
| 366 |
+
set -- mysqld "$@"
|
| 367 |
+
fi
|
| 368 |
+
|
| 369 |
+
# skip setup if they aren't running mysqld or want an option that stops mysqld
|
| 370 |
+
if [ "$1" = 'mysqld' ] && ! _mysql_want_help "$@"; then
|
| 371 |
+
mysql_note "Entrypoint script for MySQL Server ${MYSQL_VERSION} started."
|
| 372 |
+
|
| 373 |
+
mysql_check_config "$@"
|
| 374 |
+
# Load various environment variables
|
| 375 |
+
docker_setup_env "$@"
|
| 376 |
+
docker_create_db_directories "$@"
|
| 377 |
+
|
| 378 |
+
# If container is started as root user, restart as dedicated mysql user
|
| 379 |
+
if [ "$(id -u)" = "0" ]; then
|
| 380 |
+
mysql_note "Switching to dedicated user 'mysql'"
|
| 381 |
+
exec gosu mysql "$BASH_SOURCE" "$@"
|
| 382 |
+
fi
|
| 383 |
+
|
| 384 |
+
# there's no database, so it needs to be initialized
|
| 385 |
+
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
|
| 386 |
+
docker_verify_minimum_env
|
| 387 |
+
|
| 388 |
+
# check dir permissions to reduce likelihood of half-initialized database
|
| 389 |
+
ls /docker-entrypoint-initdb.d/ > /dev/null
|
| 390 |
+
|
| 391 |
+
docker_init_database_dir "$@"
|
| 392 |
+
|
| 393 |
+
mysql_note "Starting temporary server"
|
| 394 |
+
docker_temp_server_start "$@"
|
| 395 |
+
mysql_note "Temporary server started."
|
| 396 |
+
|
| 397 |
+
mysql_socket_fix
|
| 398 |
+
docker_setup_db
|
| 399 |
+
docker_process_init_files /docker-entrypoint-initdb.d/*
|
| 400 |
+
|
| 401 |
+
mysql_expire_root_user
|
| 402 |
+
|
| 403 |
+
mysql_note "Stopping temporary server"
|
| 404 |
+
docker_temp_server_stop
|
| 405 |
+
mysql_note "Temporary server stopped"
|
| 406 |
+
|
| 407 |
+
echo
|
| 408 |
+
mysql_note "MySQL init process done. Ready for start up."
|
| 409 |
+
echo
|
| 410 |
+
else
|
| 411 |
+
mysql_socket_fix
|
| 412 |
+
fi
|
| 413 |
+
fi
|
| 414 |
+
exec "$@"
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
# If we are sourced from elsewhere, don't perform any further actions
|
| 418 |
+
if ! _is_sourced; then
|
| 419 |
+
_main "$@"
|
| 420 |
+
fi
|