Skip to content

Instantly share code, notes, and snippets.

@masami256
Created May 2, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masami256/801619f1f0f43f44982852008b5c7b44 to your computer and use it in GitHub Desktop.
Save masami256/801619f1f0f43f44982852008b5c7b44 to your computer and use it in GitHub Desktop.
#!/bin/bash
# password needs to escape symbols to use sed correctly.
docker build --build-arg MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
--build-arg MYSQL_USER_PASSWORD=${MYSQL_USER_PASSWORD} \
--build-arg MYSQL_USER_NAME=${MYSQL_USER_NAME} \
--build-arg REMOTE_IP_ADDRESS=${REMOTE_IP_ADDRESS} \
-t masami/mysql .
# Use centos
FROM centos:centos7
MAINTAINER masami256@gmail.com
COPY mysql57-community-release-el7-8.noarch.rpm /tmp/
COPY make_netmask.sh /tmp/
RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \
yum localinstall -y /tmp/mysql57-community-release-el7-8.noarch.rpm && \
yum update -y && \
yum install -y iproute && \
yum install -y mysql-community-server python-setuptools && \
easy_install supervisor
COPY supervisord.conf /etc/supervisord.conf
# Change default root password
COPY mysql-init.ini /tmp/
COPY setup.sh /tmp/
COPY setup.sql /tmp/
# read password from environment values
ARG MYSQL_ROOT_PASSWORD=hogehoge
ARG MYSQL_USER_PASSWORD=hogehoge
ARG MYSQL_USER_NAME=fuga
ARG REMOTE_IP_ADDRESS=127.0.0.1
RUN /tmp/make_netmask.sh
# Write actual values to ini file.
RUN sed -i "s/MYSQL_ROOT_PASSWORD/${MYSQL_ROOT_PASSWORD}/" /tmp/mysql-init.ini && \
sed -i "s/MYSQL_USER_PASSWORD/${MYSQL_USER_PASSWORD}/" /tmp/mysql-init.ini && \
sed -i "s/MYSQL_USER_NAME/${MYSQL_USER_NAME}/" /tmp/mysql-init.ini && \
sed -i "s/REMOTE_IP_ADDRESS/${REMOTE_IP_ADDRESS}/" /tmp/mysql-init.ini && \
sed -i "s#CONTAINER_NETWORK#`/tmp/make_netmask.sh`#" /tmp/mysql-init.ini && \
sed -i "s/MYSQL_ROOT_PASSWORD/${MYSQL_ROOT_PASSWORD}/" /tmp/setup.sh
# Initial Setup
RUN mysqld_pre_systemd
CMD /usr/bin/supervisord -c /etc/supervisord.conf
#!/bin/sh
ip_cidr=`ip a | grep "inet " | grep -v 127 | awk '{print $2}'`
ip=`echo -n ${ip_cidr} | cut -f1 -d'/'`
cidr=`echo -n ${ip_cidr} | cut -f2 -d'/'`
if test ${cidr} -eq 8 ; then
allowed_subnet=`echo $ip | cut -f1-3 -d'.' | awk '{print $1".0/255.255.255.0"}'`
elif test ${cidr} -eq 16 ; then
allowed_subnet=`echo $ip | cut -f1-2 -d'.' | awk '{print $1".0.0/255.255.0.0"}'`
elif test ${cidr} -eq 24 ; then
allowed_subnet=`echo $ip | cut -f1 -d'.' | awk '{print $1".0.0.0/255.0.0.0"}'`
else
echo "[-]something wrong"
exit -1
fi
echo -n "$allowed_subnet"
exit 0
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MYSQL_ROOT_PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO MYSQL_USER_NAME@localhost IDENTIFIED BY 'MYSQL_USER_PASSWORD' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO MYSQL_USER_NAME@REMOTE_IP_ADDRESS IDENTIFIED BY 'MYSQL_USER_PASSWORD' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO MYSQL_USER_NAME@'CONTAINER_NETWORK' IDENTIFIED BY 'MYSQL_USER_PASSWORD' WITH GRANT OPTION;
FLUSH PRIVILEGES;
#!/bin/bash
# wait sometime to be mysqld running
sleep 10
mysql -u root -p"MYSQL_ROOT_PASSWORD" < /tmp/setup.sql
create database hoge;
create table hoge.users (id int auto_increment primary key, name varchar(32));
insert into hoge.users (name) values ('fugafuga');
insert into hoge.users (name) values ('hogehoge');
[supervisord]
nodaemon=true
[program:mysqld]
user=mysql
command=/usr/sbin/mysqld --pid-file=/var/run/mysqld/mysqld.pid --init-file=/tmp/mysql-init.ini
autorestart=true
redirect_stderr=true
[program:setup]
command=/tmp/setup.sh
autorestart=false
redirect_stderr=true
priority=1000
startsec=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment