Skip to content

Commit 42a6a49

Browse files
author
Rafael Grigorian
committed
Fixed GH-7
1 parent 7517317 commit 42a6a49

File tree

5 files changed

+239
-69
lines changed

5 files changed

+239
-69
lines changed

README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ docker-sync clean # Only if not starting again
5050
All docker images, except for the php-cli containers, have the ability to tail their container's relevant logs if the `VERBOSE=true` flag is passed as an environmental variable. Passing it to the container is optional since by default `VERBOSE=false`. It is explicitly stated within the sample _docker-compose.yml_ file for documentation purposes only. Please also note that you will only be able to see the verbose output of these containers if you run docker-compose without the _detach_ flag, i.e. `docker-compose up`.
5151

5252
### mysql
53-
Below are the possible environment variable that can be passed to the _mysql_ docker container with some sample values. If non are passed, then only the _root_ user will exist with the default password of _mysql_.
54-
- `MYSQL_ROOT_PASSWORD=mysql`
55-
- `MYSQL_USER_NAME=magento_db_user`
56-
- `MYSQL_USER_PASSWORD=password123`
57-
- `MYSQL_DATABASE=magento`
53+
Since we build off of the `mysql:5.7` image, the environment variables will look the same. One exeption is that we use `MYSQL_USER_NAME` instead of `MYSQL_USER` and `MYSQL_USER_PASSWORD` instead of `MYSQL_PASSWORD`. This is for backwards compatibility purposes. A full list of environment variables can be found [here](https://hub.docker.com/_/mysql#environment-variables).
5854

5955
### nginx
6056
When NGINX starts, it copies the appropriate configuration file so it can be used. This configuration file is determined by the environment variable below. If no variable is passed then the below value is set.

src/mysql/Dockerfile

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
FROM alpine:3.6
1+
FROM mysql:5.7
22
LABEL maintainer development@jetrails.com
33

4-
RUN apk --update --no-cache add \
5-
shadow \
6-
mysql \
7-
mysql-client
4+
COPY ./bin/entrypoint.sh /usr/local/bin/docker-entrypoint.sh
5+
COPY ./conf/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
86

9-
RUN mkdir -p /var/log/mysql && \
10-
touch /var/log/mysql/mysql_error.log
11-
12-
COPY ./bin/startup.sh /usr/local/bin/startup.sh
13-
COPY ./conf/my.cnf /etc/mysql/my.cnf
14-
15-
EXPOSE 3306
16-
WORKDIR var/lib/mysql
17-
CMD [ "sh", "/usr/local/bin/startup.sh" ]
7+
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

src/mysql/bin/entrypoint.sh

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
#!/bin/bash
2+
3+
# What Was Modified:
4+
#
5+
# Choosing to use mysql official image and building off of it. Since variable
6+
# names are slightly different, this file was modified to use our choice of
7+
# variable names for backward compatibility purposes.
8+
#
9+
# MYSQL_USER is overwritten by MYSQL_USER_NAME
10+
# MYSQL_PASSWORD is overwritten by MYSQL_USER_PASSWORD
11+
12+
if [[ -n "$MYSQL_USER_NAME" ]]; then MYSQL_USER=$MYSQL_USER_NAME; fi
13+
if [[ -n "$MYSQL_USER_PASSWORD" ]]; then MYSQL_PASSWORD=$MYSQL_USER_PASSWORD; fi
14+
15+
set -eo pipefail
16+
shopt -s nullglob
17+
18+
# if command starts with an option, prepend mysqld
19+
if [ "${1:0:1}" = '-' ]; then
20+
set -- mysqld "$@"
21+
fi
22+
23+
# skip setup if they want an option that stops mysqld
24+
wantHelp=
25+
for arg; do
26+
case "$arg" in
27+
-'?'|--help|--print-defaults|-V|--version)
28+
wantHelp=1
29+
break
30+
;;
31+
esac
32+
done
33+
34+
# usage: file_env VAR [DEFAULT]
35+
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
36+
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
37+
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
38+
file_env() {
39+
local var="$1"
40+
local fileVar="${var}_FILE"
41+
local def="${2:-}"
42+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
43+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
44+
exit 1
45+
fi
46+
local val="$def"
47+
if [ "${!var:-}" ]; then
48+
val="${!var}"
49+
elif [ "${!fileVar:-}" ]; then
50+
val="$(< "${!fileVar}")"
51+
fi
52+
export "$var"="$val"
53+
unset "$fileVar"
54+
}
55+
56+
# usage: process_init_file FILENAME MYSQLCOMMAND...
57+
# ie: process_init_file foo.sh mysql -uroot
58+
# (process a single initializer file, based on its extension. we define this
59+
# function here, so that initializer scripts (*.sh) can use the same logic,
60+
# potentially recursively, or override the logic used in subsequent calls)
61+
process_init_file() {
62+
local f="$1"; shift
63+
local mysql=( "$@" )
64+
65+
case "$f" in
66+
*.sh) echo "$0: running $f"; . "$f" ;;
67+
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
68+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
69+
*) echo "$0: ignoring $f" ;;
70+
esac
71+
echo
72+
}
73+
74+
_check_config() {
75+
toRun=( "$@" --verbose --help )
76+
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
77+
cat >&2 <<-EOM
78+
79+
ERROR: mysqld failed while attempting to check config
80+
command was: "${toRun[*]}"
81+
82+
$errors
83+
EOM
84+
exit 1
85+
fi
86+
}
87+
88+
# Fetch value from server config
89+
# We use mysqld --verbose --help instead of my_print_defaults because the
90+
# latter only show values present in config files, and not server defaults
91+
_get_config() {
92+
local conf="$1"; shift
93+
"$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null | awk '$1 == "'"$conf"'" { print $2; exit }'
94+
}
95+
96+
# allow the container to be started with `--user`
97+
if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
98+
_check_config "$@"
99+
DATADIR="$(_get_config 'datadir' "$@")"
100+
mkdir -p "$DATADIR"
101+
chown -R mysql:mysql "$DATADIR"
102+
exec gosu mysql "$BASH_SOURCE" "$@"
103+
fi
104+
105+
if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
106+
# still need to check config, container may have started with --user
107+
_check_config "$@"
108+
# Get config
109+
DATADIR="$(_get_config 'datadir' "$@")"
110+
111+
if [ ! -d "$DATADIR/mysql" ]; then
112+
file_env 'MYSQL_ROOT_PASSWORD'
113+
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
114+
echo >&2 'error: database is uninitialized and password option is not specified '
115+
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
116+
exit 1
117+
fi
118+
119+
mkdir -p "$DATADIR"
120+
121+
echo 'Initializing database'
122+
"$@" --initialize-insecure
123+
echo 'Database initialized'
124+
125+
if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then
126+
# https://github.com/mysql/mysql-server/blob/23032807537d8dd8ee4ec1c4d40f0633cd4e12f9/packaging/deb-in/extra/mysql-systemd-start#L81-L84
127+
echo 'Initializing certificates'
128+
mysql_ssl_rsa_setup --datadir="$DATADIR"
129+
echo 'Certificates initialized'
130+
fi
131+
132+
SOCKET="$(_get_config 'socket' "$@")"
133+
"$@" --skip-networking --socket="${SOCKET}" &
134+
pid="$!"
135+
136+
mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
137+
138+
for i in {30..0}; do
139+
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
140+
break
141+
fi
142+
echo 'MySQL init process in progress...'
143+
sleep 1
144+
done
145+
if [ "$i" = 0 ]; then
146+
echo >&2 'MySQL init process failed.'
147+
exit 1
148+
fi
149+
150+
if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
151+
# sed is for https://bugs.mysql.com/bug.php?id=20545
152+
mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
153+
fi
154+
155+
if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
156+
export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
157+
echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
158+
fi
159+
160+
rootCreate=
161+
# default root to listen for connections from anywhere
162+
file_env 'MYSQL_ROOT_HOST' '%'
163+
if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then
164+
# no, we don't care if read finds a terminating character in this heredoc
165+
# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
166+
read -r -d '' rootCreate <<-EOSQL || true
167+
CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
168+
GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
169+
EOSQL
170+
fi
171+
172+
"${mysql[@]}" <<-EOSQL
173+
-- What's done in this file shouldn't be replicated
174+
-- or products like mysql-fabric won't work
175+
SET @@SESSION.SQL_LOG_BIN=0;
176+
177+
SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ;
178+
GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
179+
${rootCreate}
180+
DROP DATABASE IF EXISTS test ;
181+
FLUSH PRIVILEGES ;
182+
EOSQL
183+
184+
if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
185+
mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
186+
fi
187+
188+
file_env 'MYSQL_DATABASE'
189+
if [ "$MYSQL_DATABASE" ]; then
190+
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
191+
mysql+=( "$MYSQL_DATABASE" )
192+
fi
193+
194+
file_env 'MYSQL_USER'
195+
file_env 'MYSQL_PASSWORD'
196+
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
197+
echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"
198+
199+
if [ "$MYSQL_DATABASE" ]; then
200+
echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
201+
fi
202+
203+
echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
204+
fi
205+
206+
echo
207+
ls /docker-entrypoint-initdb.d/ > /dev/null
208+
for f in /docker-entrypoint-initdb.d/*; do
209+
process_init_file "$f" "${mysql[@]}"
210+
done
211+
212+
if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
213+
"${mysql[@]}" <<-EOSQL
214+
ALTER USER 'root'@'%' PASSWORD EXPIRE;
215+
EOSQL
216+
fi
217+
if ! kill -s TERM "$pid" || ! wait "$pid"; then
218+
echo >&2 'MySQL init process failed.'
219+
exit 1
220+
fi
221+
222+
echo
223+
echo 'MySQL init process done. Ready for start up.'
224+
echo
225+
fi
226+
fi
227+
228+
exec "$@"

src/mysql/bin/startup.sh

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
[client]
2-
port = 3306
3-
socket = /run/mysqld.sock
4-
51
[mysqld]
6-
port = 3306
7-
socket = /run/mysqld.sock
8-
skip-external-locking
2+
pid-file = /var/run/mysqld/mysqld.pid
3+
socket = /var/run/mysqld/mysqld.sock
4+
datadir = /var/lib/mysql
5+
log-error = /var/log/mysql/mysql_error.log
6+
bind-address = 0.0.0.0
7+
symbolic-links=0
98
key_buffer_size = 16M
109
max_allowed_packet = 1M
1110
table_open_cache = 64
@@ -14,6 +13,3 @@ net_buffer_length = 8K
1413
read_buffer_size = 256K
1514
read_rnd_buffer_size = 512K
1615
myisam_sort_buffer_size = 8M
17-
log-bin=mysql-bin
18-
binlog_format=mixed
19-
server-id = 1

0 commit comments

Comments
 (0)