diff --git a/10.0/Dockerfile b/10.0/Dockerfile index 682af3da..ea2f2a1e 100644 --- a/10.0/Dockerfile +++ b/10.0/Dockerfile @@ -4,11 +4,25 @@ FROM debian:jessie # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r mysql && useradd -r -g mysql mysql -# install "pwgen" for randomizing passwords -RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* +# add gosu for easy step-down from root +ENV GOSU_VERSION 1.7 +RUN set -x \ + && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ + && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ + && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ + && export GNUPGHOME="$(mktemp -d)" \ + && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ + && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ + && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ + && chmod +x /usr/local/bin/gosu \ + && gosu nobody true \ + && apt-get purge -y --auto-remove ca-certificates wget RUN mkdir /docker-entrypoint-initdb.d +# install "pwgen" for randomizing passwords +RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* + # Key fingerprint = 1993 69E5 404B D5FC 7D2F E43B CBCB 082A 1BB9 43DB # MariaDB Package Signing Key # Key fingerprint = 430B DF5C 56E7 C94E 848E E60C 1C4C BDCD CD2E FD2A @@ -48,8 +62,13 @@ RUN { \ percona-xtrabackup \ socat \ && rm -rf /var/lib/apt/lists/* \ - && rm -rf /var/lib/mysql \ - && mkdir /var/lib/mysql +# comment out any "user" entires in the MySQL config ("docker-entrypoint.sh" or "--user" will handle user switching) + && sed -ri 's/^user\s/#&/' /etc/mysql/my.cnf /etc/mysql/conf.d/* \ +# purge and re-create /var/lib/mysql with appropriate ownership + && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \ + && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \ +# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime + && chmod 777 /var/run/mysqld # comment out a few problematic configuration values # don't reverse lookup hostnames, they are usually another container @@ -59,9 +78,9 @@ RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \ VOLUME /var/lib/mysql -COPY docker-entrypoint.sh / - -ENTRYPOINT ["/docker-entrypoint.sh"] +COPY docker-entrypoint.sh /usr/local/bin/ +RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat +ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 3306 CMD ["mysqld"] diff --git a/10.0/docker-entrypoint.sh b/10.0/docker-entrypoint.sh index d0e9284b..ea896b99 100755 --- a/10.0/docker-entrypoint.sh +++ b/10.0/docker-entrypoint.sh @@ -17,9 +17,21 @@ for arg; do esac done +_datadir() { + "$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }' +} + +# allow the container to be started with `--user` +if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then + DATADIR="$(_datadir "$@")" + mkdir -p "$DATADIR" + chown -R mysql:mysql "$DATADIR" + exec gosu mysql "$BASH_SOURCE" "$@" +fi + if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then # Get config - DATADIR="$("$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" + DATADIR="$(_datadir "$@")" if [ ! -d "$DATADIR/mysql" ]; then if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then @@ -29,10 +41,9 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then fi mkdir -p "$DATADIR" - chown -R mysql:mysql "$DATADIR" echo 'Initializing database' - mysql_install_db --user=mysql --datadir="$DATADIR" --rpm + mysql_install_db --datadir="$DATADIR" --rpm echo 'Database initialized' "$@" --skip-networking & @@ -112,8 +123,6 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then echo 'MySQL init process done. Ready for start up.' echo fi - - chown -R mysql:mysql "$DATADIR" fi exec "$@" diff --git a/10.1/Dockerfile b/10.1/Dockerfile index 7f0fc00f..b0767338 100644 --- a/10.1/Dockerfile +++ b/10.1/Dockerfile @@ -4,11 +4,25 @@ FROM debian:jessie # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r mysql && useradd -r -g mysql mysql -# install "pwgen" for randomizing passwords -RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* +# add gosu for easy step-down from root +ENV GOSU_VERSION 1.7 +RUN set -x \ + && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ + && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ + && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ + && export GNUPGHOME="$(mktemp -d)" \ + && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ + && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ + && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ + && chmod +x /usr/local/bin/gosu \ + && gosu nobody true \ + && apt-get purge -y --auto-remove ca-certificates wget RUN mkdir /docker-entrypoint-initdb.d +# install "pwgen" for randomizing passwords +RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* + # Key fingerprint = 1993 69E5 404B D5FC 7D2F E43B CBCB 082A 1BB9 43DB # MariaDB Package Signing Key # Key fingerprint = 430B DF5C 56E7 C94E 848E E60C 1C4C BDCD CD2E FD2A @@ -48,8 +62,13 @@ RUN { \ percona-xtrabackup \ socat \ && rm -rf /var/lib/apt/lists/* \ - && rm -rf /var/lib/mysql \ - && mkdir /var/lib/mysql +# comment out any "user" entires in the MySQL config ("docker-entrypoint.sh" or "--user" will handle user switching) + && sed -ri 's/^user\s/#&/' /etc/mysql/my.cnf /etc/mysql/conf.d/* \ +# purge and re-create /var/lib/mysql with appropriate ownership + && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \ + && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \ +# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime + && chmod 777 /var/run/mysqld # comment out a few problematic configuration values # don't reverse lookup hostnames, they are usually another container @@ -59,9 +78,9 @@ RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \ VOLUME /var/lib/mysql -COPY docker-entrypoint.sh / - -ENTRYPOINT ["/docker-entrypoint.sh"] +COPY docker-entrypoint.sh /usr/local/bin/ +RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat +ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 3306 CMD ["mysqld"] diff --git a/10.1/docker-entrypoint.sh b/10.1/docker-entrypoint.sh index d0e9284b..ea896b99 100755 --- a/10.1/docker-entrypoint.sh +++ b/10.1/docker-entrypoint.sh @@ -17,9 +17,21 @@ for arg; do esac done +_datadir() { + "$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }' +} + +# allow the container to be started with `--user` +if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then + DATADIR="$(_datadir "$@")" + mkdir -p "$DATADIR" + chown -R mysql:mysql "$DATADIR" + exec gosu mysql "$BASH_SOURCE" "$@" +fi + if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then # Get config - DATADIR="$("$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" + DATADIR="$(_datadir "$@")" if [ ! -d "$DATADIR/mysql" ]; then if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then @@ -29,10 +41,9 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then fi mkdir -p "$DATADIR" - chown -R mysql:mysql "$DATADIR" echo 'Initializing database' - mysql_install_db --user=mysql --datadir="$DATADIR" --rpm + mysql_install_db --datadir="$DATADIR" --rpm echo 'Database initialized' "$@" --skip-networking & @@ -112,8 +123,6 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then echo 'MySQL init process done. Ready for start up.' echo fi - - chown -R mysql:mysql "$DATADIR" fi exec "$@" diff --git a/5.5/Dockerfile b/5.5/Dockerfile index fb1e7732..91823b61 100644 --- a/5.5/Dockerfile +++ b/5.5/Dockerfile @@ -4,11 +4,25 @@ FROM debian:wheezy # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r mysql && useradd -r -g mysql mysql -# install "pwgen" for randomizing passwords -RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* +# add gosu for easy step-down from root +ENV GOSU_VERSION 1.7 +RUN set -x \ + && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ + && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ + && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ + && export GNUPGHOME="$(mktemp -d)" \ + && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ + && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ + && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ + && chmod +x /usr/local/bin/gosu \ + && gosu nobody true \ + && apt-get purge -y --auto-remove ca-certificates wget RUN mkdir /docker-entrypoint-initdb.d +# install "pwgen" for randomizing passwords +RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* + # Key fingerprint = 1993 69E5 404B D5FC 7D2F E43B CBCB 082A 1BB9 43DB # MariaDB Package Signing Key # Key fingerprint = 430B DF5C 56E7 C94E 848E E60C 1C4C BDCD CD2E FD2A @@ -48,8 +62,13 @@ RUN { \ percona-xtrabackup \ socat \ && rm -rf /var/lib/apt/lists/* \ - && rm -rf /var/lib/mysql \ - && mkdir /var/lib/mysql +# comment out any "user" entires in the MySQL config ("docker-entrypoint.sh" or "--user" will handle user switching) + && sed -ri 's/^user\s/#&/' /etc/mysql/my.cnf /etc/mysql/conf.d/* \ +# purge and re-create /var/lib/mysql with appropriate ownership + && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \ + && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \ +# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime + && chmod 777 /var/run/mysqld # comment out a few problematic configuration values # don't reverse lookup hostnames, they are usually another container @@ -59,9 +78,9 @@ RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \ VOLUME /var/lib/mysql -COPY docker-entrypoint.sh / - -ENTRYPOINT ["/docker-entrypoint.sh"] +COPY docker-entrypoint.sh /usr/local/bin/ +RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat +ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 3306 CMD ["mysqld"] diff --git a/5.5/docker-entrypoint.sh b/5.5/docker-entrypoint.sh index d0e9284b..ea896b99 100755 --- a/5.5/docker-entrypoint.sh +++ b/5.5/docker-entrypoint.sh @@ -17,9 +17,21 @@ for arg; do esac done +_datadir() { + "$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }' +} + +# allow the container to be started with `--user` +if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then + DATADIR="$(_datadir "$@")" + mkdir -p "$DATADIR" + chown -R mysql:mysql "$DATADIR" + exec gosu mysql "$BASH_SOURCE" "$@" +fi + if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then # Get config - DATADIR="$("$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" + DATADIR="$(_datadir "$@")" if [ ! -d "$DATADIR/mysql" ]; then if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then @@ -29,10 +41,9 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then fi mkdir -p "$DATADIR" - chown -R mysql:mysql "$DATADIR" echo 'Initializing database' - mysql_install_db --user=mysql --datadir="$DATADIR" --rpm + mysql_install_db --datadir="$DATADIR" --rpm echo 'Database initialized' "$@" --skip-networking & @@ -112,8 +123,6 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then echo 'MySQL init process done. Ready for start up.' echo fi - - chown -R mysql:mysql "$DATADIR" fi exec "$@" diff --git a/Dockerfile.template b/Dockerfile.template index 8f902006..f591aef5 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -4,11 +4,25 @@ FROM debian:%%SUITE%% # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r mysql && useradd -r -g mysql mysql -# install "pwgen" for randomizing passwords -RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* +# add gosu for easy step-down from root +ENV GOSU_VERSION 1.7 +RUN set -x \ + && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ + && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ + && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ + && export GNUPGHOME="$(mktemp -d)" \ + && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ + && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ + && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ + && chmod +x /usr/local/bin/gosu \ + && gosu nobody true \ + && apt-get purge -y --auto-remove ca-certificates wget RUN mkdir /docker-entrypoint-initdb.d +# install "pwgen" for randomizing passwords +RUN apt-get update && apt-get install -y pwgen && rm -rf /var/lib/apt/lists/* + # Key fingerprint = 1993 69E5 404B D5FC 7D2F E43B CBCB 082A 1BB9 43DB # MariaDB Package Signing Key # Key fingerprint = 430B DF5C 56E7 C94E 848E E60C 1C4C BDCD CD2E FD2A @@ -48,8 +62,13 @@ RUN { \ percona-xtrabackup \ socat \ && rm -rf /var/lib/apt/lists/* \ - && rm -rf /var/lib/mysql \ - && mkdir /var/lib/mysql +# comment out any "user" entires in the MySQL config ("docker-entrypoint.sh" or "--user" will handle user switching) + && sed -ri 's/^user\s/#&/' /etc/mysql/my.cnf /etc/mysql/conf.d/* \ +# purge and re-create /var/lib/mysql with appropriate ownership + && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \ + && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \ +# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime + && chmod 777 /var/run/mysqld # comment out a few problematic configuration values # don't reverse lookup hostnames, they are usually another container @@ -59,9 +78,9 @@ RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \ VOLUME /var/lib/mysql -COPY docker-entrypoint.sh / - -ENTRYPOINT ["/docker-entrypoint.sh"] +COPY docker-entrypoint.sh /usr/local/bin/ +RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat +ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 3306 CMD ["mysqld"] diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index d0e9284b..ea896b99 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -17,9 +17,21 @@ for arg; do esac done +_datadir() { + "$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }' +} + +# allow the container to be started with `--user` +if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then + DATADIR="$(_datadir "$@")" + mkdir -p "$DATADIR" + chown -R mysql:mysql "$DATADIR" + exec gosu mysql "$BASH_SOURCE" "$@" +fi + if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then # Get config - DATADIR="$("$@" --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" + DATADIR="$(_datadir "$@")" if [ ! -d "$DATADIR/mysql" ]; then if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then @@ -29,10 +41,9 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then fi mkdir -p "$DATADIR" - chown -R mysql:mysql "$DATADIR" echo 'Initializing database' - mysql_install_db --user=mysql --datadir="$DATADIR" --rpm + mysql_install_db --datadir="$DATADIR" --rpm echo 'Database initialized' "$@" --skip-networking & @@ -112,8 +123,6 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then echo 'MySQL init process done. Ready for start up.' echo fi - - chown -R mysql:mysql "$DATADIR" fi exec "$@"