├── Dockerfile ├── LICENSE ├── README.md ├── create_mysql_users.sh ├── logo.png ├── run.sh ├── start-mysqld.sh └── supervisord-mysqld.conf /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM phusion/baseimage:latest 2 | MAINTAINER Daniel Graziotin 3 | 4 | # based on dgraziotin/lamp, which is based on 5 | # tutumcloud/tutum-docker-lamp 6 | # MAINTAINER Fernando Mayo , Feng Honglin 7 | 8 | ENV DOCKER_USER_ID 501 9 | ENV DOCKER_USER_GID 20 10 | 11 | ENV BOOT2DOCKER_ID 1000 12 | ENV BOOT2DOCKER_GID 50 13 | 14 | # Tweaks to give MySQL write permissions to the app 15 | RUN useradd -r mysql -u ${BOOT2DOCKER_ID} && \ 16 | usermod -G staff mysql 17 | 18 | RUN groupmod -g $(($BOOT2DOCKER_GID + 10000)) $(getent group $BOOT2DOCKER_GID | cut -d: -f1) 19 | RUN groupmod -g ${BOOT2DOCKER_GID} staff 20 | 21 | # Install packages 22 | ENV DEBIAN_FRONTEND noninteractive 23 | RUN apt-get update && \ 24 | apt-get -y install apt-utils && \ 25 | apt-get -y install supervisor wget git mysql-server pwgen zip unzip 26 | 27 | # Add image configuration and scripts 28 | ADD start-mysqld.sh /start-mysqld.sh 29 | ADD run.sh /run.sh 30 | RUN chmod 755 /*.sh 31 | ADD supervisord-mysqld.conf /etc/supervisor/conf.d/supervisord-mysqld.conf 32 | 33 | # Remove pre-installed database 34 | RUN rm -rf /var/lib/mysql 35 | 36 | # Add MySQL utils 37 | ADD create_mysql_users.sh /create_mysql_users.sh 38 | RUN chmod 755 /*.sh 39 | 40 | ENV MYSQL_PASS:-$(pwgen -s 12 1) 41 | 42 | # Add volumes for the app and MySql 43 | VOLUME ["/etc/mysql", "/var/lib/mysql"] 44 | 45 | EXPOSE 3306 46 | CMD ["/run.sh"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # osx-docker-mysql, a.k.a dgraziotin/mysql 2 | 3 | [![](https://images.microbadger.com/badges/image/dgraziotin/mysql.svg)](https://microbadger.com/images/dgraziotin/mysql "Get your own image badge on microbadger.com") 4 | 5 | As of [Docker for Mac v1.12.0](https://docs.docker.com/engine/installation/mac/), there is no _need_ for using my Docker images anymore (although some users still report that my image is the only one working for them). Thanks for the support! You can still use my images if you like. I will be using them and supporting them. 6 | 7 | Out-of-the-box MySQL Docker image that *just works* on Mac OS X. 8 | Including write support for mounted volumes (MySQL). 9 | No matter if using the official boot2docker or having Vagrant in the stack, as well. 10 | 11 | osx-docker-mysql, which is known as 12 | [dgraziotin/mysql](https://registry.hub.docker.com/u/dgraziotin/mysql/) 13 | in the Docker Hub, is a reduced fork of 14 | [dgraziotin/osx-docker-lamp](https://github.com/dgraziotin/osx-docker-lamp), 15 | which is an "Out-of-the-box LAMP image (PHP+MySQL) for Docker". 16 | 17 | Some info about osx-docker-mysql: 18 | 19 | - It is based on [phusion/baseimage:latest](http://phusion.github.io/baseimage-docker/) 20 | instead of ubuntu:trusty. 21 | - It works flawlessy regardless of using boot2docker standalone or with Vagrant. You will need to set three environment variables, though. 22 | - It fixes OS X related [write permission errors for MySQL](https://github.com/boot2docker/boot2docker/issues/581) 23 | - It lets you mount OS X folders *with write support* as volumes for 24 | - The database 25 | - If `CREATE_MYSQL_BASIC_USER_AND_DB="true"`, it creates a default database and user with permissions to that database 26 | - It is documented for less advanced users (like me) 27 | 28 | ## Usage 29 | 30 | If using Vagrant, please see the extra steps in the next subsection. 31 | 32 | If you need to create a custom image `youruser/mysql`, 33 | execute the following command from the `osx-docker-mysql` source folder: 34 | 35 | docker build -t youruser/mysql . 36 | 37 | If you wish, you can push your new image to the registry: 38 | 39 | docker push youruser/mysql 40 | 41 | Otherwise, you are free to use dgraziotin/mysql as it is provided. Remember first 42 | to pull it from the Docker Hub: 43 | 44 | docker pull dgraziotin/mysql 45 | 46 | ### Vagrant 47 | 48 | **Warning: I will remove Vagrant support in a coming release** 49 | 50 | If, for any reason, you would rather use Vagrant (I suggest using [AntonioMeireles/boot2docker-vagrant-box](https://github.com/AntonioMeireles/boot2docker-vagrant-box)), you need to add the following three variables when running your box: 51 | 52 | -`VAGRANT_OSX_MODE="true"` for enabling Vagrant-compatibility 53 | -`DOCKER_USER_ID=$(id -u)` for letting Vagrant use your host user ID for mounted folders 54 | -`DOCKER_USER_GID=$(id -g)` for letting Vagrant use your host user GID for mounted folders 55 | 56 | See the Environment variables section for more options. 57 | 58 | ### Running your MySQL docker image 59 | 60 | If you start the image without supplying your code, e.g., 61 | 62 | docker run -t -i -p 3306:3306 --name db dgraziotin/mysql 63 | 64 | At [boot2docker ip] you should be able to connect to MySQL. 65 | 66 | ### Loading your custom MySQL files 67 | 68 | If you wish to mount a MySQL folder locally, so that MySQL files are saved on your 69 | OS X machine, run the following instead: 70 | 71 | docker run -i -t -p "3306:3306" -v ${PWD}/mysql:/var/lib/mysql --name db dgraziotin/mysql 72 | 73 | The MySQL database will thus become persistent at each subsequent run of your image. 74 | 75 | ## Environment description 76 | 77 | ### The /mysql folder 78 | 79 | MySQL is configured to serve the files from the `/mysql` folder, which is a symbolic 80 | link to `/var/lib/mysql`. In osx-docker-mysql, the MySQL user `mysql` 81 | has full write permissions to the `mysql` folder. 82 | 83 | ### MySQL 84 | 85 | MySQL runs as user `mysql` and group `staff`. 86 | 87 | #### The three MySQL users 88 | 89 | The bundled MySQL server has two users, that are `root` and `admin`, and an optional 90 | third user `user`. 91 | 92 | The `root` account comes with an empty password, and it is for local connections 93 | (e.g., using some code). The `root` user cannot remotely access the database 94 | (and the container). 95 | 96 | However, the first time that you run your container, a new user `admin` 97 | with all root privileges will be created in MySQL with a random password. 98 | 99 | To get the password, check the logs of the container by running: 100 | 101 | docker logs [name or id, e.g., mywebsite] 102 | 103 | You will see an output like the following: 104 | 105 | ======================================================================== 106 | You can now connect to this MySQL Server using: 107 | 108 | mysql -uadmin -p47nnf4FweaKu -h -P 109 | 110 | Please remember to change the above password as soon as possible! 111 | MySQL user 'root' has no password but only allows local connections 112 | ======================================================================== 113 | 114 | In this case, `47nnf4FweaKu` is the password allocated to the `admin` user. 115 | 116 | Finally, an optional a user called `user` with password `password` can be created for your convenience either when: 117 | - The environment variable `CREATE_MYSQL_BASIC_USER_AND_DB` is true; or 118 | - Any of the `MYSQL_USER_*` variable (explained below) is true 119 | The user is called `user` and has as password `password`. 120 | 121 | The `user` user has full privileges on a database called `db`, which is also created 122 | for your convenience. As with the `admin` user, the user `user` can access 123 | the MySQL server from any host (`%`). 124 | The user name, password, and database name can be changed using 125 | the the `MYSQL_USER_*` variables, explained below. 126 | 127 | ## Environment variables 128 | 129 | - `MYSQL_ADMIN_PASS="mypass"` will use your given MySQL password for the `admin` 130 | user instead of the random one. 131 | - `CREATE_MYSQL_BASIC_USER_AND_DB="true"` will create the user `user` with db `db` and password `password`. Not needed if using one of the following three `MYSQL_USER_*` variables 132 | - `MYSQL_USER_NAME="daniel"` will use your given MySQL username instead of `user` 133 | - `MYSQL_USER_DB="supercooldb"` will use your given database name instead of `db` 134 | - `MYSQL_USER_PASS="supersecretpassword"` will use your given password instead of `password` 135 | -`VAGRANT_OSX_MODE="true"` for enabling Vagrant-compatibility 136 | -`DOCKER_USER_ID=$(id -u)` for letting Vagrant use your host user ID for mounted folders 137 | -`DOCKER_USER_GID=$(id -g)` for letting Vagrant use your host user GID for mounted folders 138 | 139 | Set these variables using the `-e` flag when invoking the `docker` client. 140 | 141 | docker run -i -t -p "3306:3306" -e MYSQL_ADMIN_PASS="mypass" --name yourdb dgraziotin/mysql 142 | 143 | Please note that the MySQL variables will not work if an existing MySQL volume is supplied. 144 | -------------------------------------------------------------------------------- /create_mysql_users.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | /usr/bin/mysqld_safe > /dev/null 2>&1 & 4 | 5 | RET=1 6 | while [[ RET -ne 0 ]]; do 7 | echo "=> Waiting for confirmation of MySQL service startup" 8 | sleep 5 9 | mysql -uroot -e "status" > /dev/null 2>&1 10 | RET=$? 11 | done 12 | 13 | PASS=${MYSQL_ADMIN_PASS:-$(pwgen -s 12 1)} 14 | _word=$( [ ${MYSQL_ADMIN_PASS} ] && echo "preset" || echo "random" ) 15 | echo "=> Creating MySQL admin user with ${_word} password" 16 | 17 | mysql -uroot -e "CREATE USER 'admin'@'%' IDENTIFIED BY '$PASS'" 18 | mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION" 19 | 20 | CREATE_MYSQL_USER=false 21 | 22 | if [ -n "$CREATE_MYSQL_BASIC_USER_AND_DB" ] || \ 23 | [ -n "$MYSQL_USER_NAME" ] || \ 24 | [ -n "$MYSQL_USER_DB" ] || \ 25 | [ -n "$MYSQL_USER_PASS" ]; then 26 | CREATE_MYSQL_USER=true 27 | fi 28 | 29 | if [ "$CREATE_MYSQL_USER" = true ]; then 30 | _user=${MYSQL_USER_NAME:-user} 31 | _userdb=${MYSQL_USER_DB:-db} 32 | _userpass=${MYSQL_USER_PASS:-password} 33 | 34 | mysql -uroot -e "CREATE USER '${_user}'@'%' IDENTIFIED BY '${_userpass}'" 35 | mysql -uroot -e "GRANT USAGE ON *.* TO '${_user}'@'%' IDENTIFIED BY '${_userpass}'" 36 | mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${_userdb}" 37 | mysql -uroot -e "GRANT ALL PRIVILEGES ON ${_userdb}.* TO '${_user}'@'%'" 38 | fi 39 | 40 | echo "=> Done!" 41 | 42 | echo "========================================================================" 43 | echo "You can now connect to this MySQL Server with $PASS" 44 | echo "" 45 | echo " mysql -uadmin -p$PASS -h -P" 46 | echo "" 47 | echo "Please remember to change the above password as soon as possible!" 48 | echo "MySQL user 'root' has no password but only allows local connections" 49 | echo "" 50 | 51 | if [ "$CREATE_MYSQL_USER" = true ]; then 52 | echo "We also created" 53 | echo "A database called '${_userdb}' and" 54 | echo "a user called '${_user}' with password '${_userpass}'" 55 | echo "'${_user}' has full access on '${_userdb}'" 56 | fi 57 | 58 | echo "enjoy!" 59 | echo "========================================================================" 60 | 61 | mysqladmin -uroot shutdown 62 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dgraziotin/osx-docker-mysql/ee1c1f7dc8d67356b7e1fa7c5a6e14402ff2ed29/logo.png -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | VOLUME_HOME="/var/lib/mysql" 4 | 5 | # Tweaks to give Apache/PHP write permissions to the app 6 | chown -R mysql:staff /var/lib/mysql 7 | chmod -R 770 /var/lib/mysql 8 | 9 | if [ -n "$VAGRANT_OSX_MODE" ];then 10 | usermod -u $DOCKER_USER_ID mysql 11 | groupmod -g $(($DOCKER_USER_GID + 10000)) $(getent group $DOCKER_USER_GID | cut -d: -f1) 12 | groupmod -g ${DOCKER_USER_GID} staff 13 | fi 14 | 15 | # Tweaks to give MySQL write permissions to the app 16 | chmod -R 770 /var/lib/mysql 17 | chown -R mysql:staff /var/lib/mysql 18 | 19 | sed -i "s/bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf 20 | sed -i "s/user.*/user = mysql/" /etc/mysql/mysql.conf.d/mysqld.cnf 21 | 22 | if [[ ! -d $VOLUME_HOME/mysql ]]; then 23 | echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME" 24 | echo "=> Installing MySQL ..." 25 | mysqld --initialize-insecure > /dev/null 2>&1 26 | echo "=> Done!" 27 | /create_mysql_users.sh 28 | else 29 | echo "=> Using an existing volume of MySQL" 30 | fi 31 | 32 | chown -R mysql:staff /var/run/mysqld 33 | chmod -R 770 /var/run/mysqld 34 | 35 | exec supervisord -n 36 | -------------------------------------------------------------------------------- /start-mysqld.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | exec mysqld_safe 3 | -------------------------------------------------------------------------------- /supervisord-mysqld.conf: -------------------------------------------------------------------------------- 1 | [program:mysqld] 2 | command=/start-mysqld.sh 3 | numprocs=1 4 | autostart=true 5 | autorestart=true --------------------------------------------------------------------------------