├── .gitignore ├── Dockerfile ├── build ├── my.cnf └── mysql.sh ├── etc └── my_init.d │ └── 99_mysql_setup.sh └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | ._* 3 | .vagrant 4 | *~ 5 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM phusion/baseimage:0.9.9 2 | 3 | ENV HOME /root 4 | 5 | RUN /etc/my_init.d/00_regen_ssh_host_keys.sh 6 | 7 | CMD ["/sbin/my_init"] 8 | 9 | # Some Environment Variables 10 | ENV DEBIAN_FRONTEND noninteractive 11 | 12 | RUN sed -i "s/^exit 101$/exit 0/" /usr/sbin/policy-rc.d 13 | 14 | # MySQL Installation 15 | RUN apt-get update 16 | RUN echo "mysql-server mysql-server/root_password password root" | debconf-set-selections 17 | RUN echo "mysql-server mysql-server/root_password_again password root" | debconf-set-selections 18 | RUN apt-get install -y mysql-server 19 | 20 | ADD build/my.cnf /etc/mysql/my.cnf 21 | 22 | RUN mkdir /etc/service/mysql 23 | ADD build/mysql.sh /etc/service/mysql/run 24 | RUN chmod +x /etc/service/mysql/run 25 | 26 | RUN mkdir -p /var/lib/mysql/ 27 | RUN chmod -R 755 /var/lib/mysql/ 28 | 29 | ADD etc/my_init.d/99_mysql_setup.sh /etc/my_init.d/99_mysql_setup.sh 30 | RUN chmod +x /etc/my_init.d/99_mysql_setup.sh 31 | 32 | EXPOSE 3306 33 | # END MySQL Installation 34 | 35 | RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* 36 | -------------------------------------------------------------------------------- /build/my.cnf: -------------------------------------------------------------------------------- 1 | # 2 | # The MySQL database server configuration file. 3 | # 4 | # You can copy this to one of: 5 | # - "/etc/mysql/my.cnf" to set global options, 6 | # - "~/.my.cnf" to set user-specific options. 7 | # 8 | # One can use all long options that the program supports. 9 | # Run program with --help to get a list of available options and with 10 | # --print-defaults to see which it would actually understand and use. 11 | # 12 | # For explanations see 13 | # http://dev.mysql.com/doc/mysql/en/server-system-variables.html 14 | 15 | # This will be passed to all mysql clients 16 | # It has been reported that passwords should be enclosed with ticks/quotes 17 | # escpecially if they contain "#" chars... 18 | # Remember to edit /etc/mysql/debian.cnf when changing the socket location. 19 | [client] 20 | port = 3306 21 | socket = /var/run/mysqld/mysqld.sock 22 | 23 | # Here is entries for some specific programs 24 | # The following values assume you have at least 32M ram 25 | 26 | # This was formally known as [safe_mysqld]. Both versions are currently parsed. 27 | [mysqld_safe] 28 | socket = /var/run/mysqld/mysqld.sock 29 | nice = 0 30 | 31 | [mysqld] 32 | # 33 | # * Basic Settings 34 | # 35 | user = mysql 36 | pid-file = /var/run/mysqld/mysqld.pid 37 | socket = /var/run/mysqld/mysqld.sock 38 | port = 3306 39 | basedir = /usr 40 | datadir = /var/lib/mysql 41 | tmpdir = /tmp 42 | lc-messages-dir = /usr/share/mysql 43 | skip-external-locking 44 | # 45 | # Instead of skip-networking the default is now to listen only on 46 | # localhost which is more compatible and is not less secure. 47 | # bind-address = 0.0.0.0 48 | # 49 | # * Fine Tuning 50 | # 51 | key_buffer = 16M 52 | max_allowed_packet = 16M 53 | thread_stack = 192K 54 | thread_cache_size = 8 55 | # This replaces the startup script and checks MyISAM tables if needed 56 | # the first time they are touched 57 | myisam-recover = BACKUP 58 | #max_connections = 100 59 | #table_cache = 64 60 | #thread_concurrency = 10 61 | # 62 | # * Query Cache Configuration 63 | # 64 | query_cache_limit = 1M 65 | query_cache_size = 16M 66 | # 67 | # * Logging and Replication 68 | # 69 | # Both location gets rotated by the cronjob. 70 | # Be aware that this log type is a performance killer. 71 | # As of 5.1 you can enable the log at runtime! 72 | # general_log_file = /var/log/mysql/mysql.log 73 | # general_log = 1 74 | # 75 | # Error log - should be very few entries. 76 | # 77 | log_error = /var/log/mysql/error.log 78 | # 79 | # Here you can see queries with especially long duration 80 | #log_slow_queries = /var/log/mysql/mysql-slow.log 81 | #long_query_time = 2 82 | #log-queries-not-using-indexes 83 | # 84 | # The following can be used as easy to replay backup logs or for replication. 85 | # note: if you are setting up a replication slave, see README.Debian about 86 | # other settings you may need to change. 87 | #server-id = 1 88 | #log_bin = /var/log/mysql/mysql-bin.log 89 | expire_logs_days = 10 90 | max_binlog_size = 100M 91 | #binlog_do_db = include_database_name 92 | #binlog_ignore_db = include_database_name 93 | # 94 | # * InnoDB 95 | # 96 | # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. 97 | # Read the manual for more InnoDB related options. There are many! 98 | # 99 | # * Security Features 100 | # 101 | # Read the manual, too, if you want chroot! 102 | # chroot = /var/lib/mysql/ 103 | # 104 | # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". 105 | # 106 | # ssl-ca=/etc/mysql/cacert.pem 107 | # ssl-cert=/etc/mysql/server-cert.pem 108 | # ssl-key=/etc/mysql/server-key.pem 109 | 110 | 111 | 112 | [mysqldump] 113 | quick 114 | quote-names 115 | max_allowed_packet = 16M 116 | 117 | [mysql] 118 | #no-auto-rehash # faster start of mysql but no tab completition 119 | 120 | [isamchk] 121 | key_buffer = 16M 122 | 123 | # 124 | # * IMPORTANT: Additional settings that can override those from this file! 125 | # The files must end with '.cnf', otherwise they'll be ignored. 126 | # 127 | !includedir /etc/mysql/conf.d/ 128 | -------------------------------------------------------------------------------- /build/mysql.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | trap 'mysqladmin -u root -proot shutdown' EXIT 4 | 5 | mysqld_safe 6 | -------------------------------------------------------------------------------- /etc/my_init.d/99_mysql_setup.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | if [ ! -d /var/lib/mysql/mysql ]; then 4 | echo 'Rebuilding mysql data dir' 5 | 6 | chown -R mysql.mysql /var/lib/mysql 7 | mysql_install_db > /dev/null 8 | 9 | rm -rf /var/run/mysqld/* 10 | 11 | echo 'Starting mysqld' 12 | # The sleep 1 is there to make sure that inotifywait starts up before the socket is created 13 | mysqld_safe & 14 | 15 | echo 'Waiting for mysqld to come online' 16 | while [ ! -x /var/run/mysqld/mysqld.sock ]; do 17 | sleep 1 18 | done 19 | 20 | echo 'Setting root password to root' 21 | /usr/bin/mysqladmin -u root password 'root' 22 | 23 | if [ -d /var/lib/mysql/setup ]; then 24 | echo 'Found /var/lib/mysql/setup - scanning for SQL scripts' 25 | for sql in $(ls /var/lib/mysql/setup/*.sql 2>/dev/null | sort); do 26 | echo 'Running script:' $sql 27 | mysql -uroot -proot -e "\. $sql" 28 | mv $sql $sql.processed 29 | done 30 | else 31 | echo 'No setup directory with extra sql scripts to run' 32 | fi 33 | 34 | echo 'Shutting down mysqld' 35 | mysqladmin -uroot -proot shutdown 36 | 37 | fi 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Docker MySQL Server 2 | 3 | To use, edit the included `build/setup.sh` so it creates a database, user and password to your needs. A future update will let you define which host/ip to use when creating the user permissions. 4 | 5 | Then, build the Docker container: 6 | 7 | ```bash 8 | # cd into the git repository 9 | cd /path/to/repo/docker-mysql 10 | docker build -t mysql . # Build a Docker image named "mysql" from this location "." 11 | # wait for it to build... 12 | 13 | # Run the docker container 14 | docker run -p 3306:3306 --name mysql -d mysql /sbin/my_init --enable-insecure-key # Give container a name in case it's linked to another app container 15 | ``` 16 | 17 | * `docker run` - starts a new docker container 18 | * * `-p 3306:3306` - Binds the local port 3306 to the container's port 3306, so a local. 19 | * * `-d mysql` - Use the image tagged "mysql" 20 | * * `/sbin/my_init` - Run the init scripts used to kick off long-running processes and other bootstrapping, as per [phusion/baseimage-docker](https://github.com/phusion/baseimage-docker) 21 | * * `--enable-insecure-key` - Enable a generated SSL key so you can SSH into the container, again as per [phusion/baseimage-docker](https://github.com/phusion/baseimage-docker). Generate your own SSH key for production use. 22 | * * If you use this with [fideloper/docker-nginx-php](https://github.com/fideloper/docker-nginx-php), then naming this container via `-name mysql` will allow you to [link it](http://docs.docker.io/en/latest/use/working_with_links_names/) with the web-app. 23 | --------------------------------------------------------------------------------