[
  {
    "path": ".circleci/config.yml",
    "content": "version: 2\njobs:\n  build:\n    working_directory: /workdir\n    docker:\n      - image: docker:18.03.0-ce-git\n        environment:\n          IMAGE_NAME: \"sameersbn/mysql\"\n\n    steps:\n      - checkout\n\n      - setup_remote_docker:\n          version: 18.03.1-ce\n\n      - run:\n          name: Docker info\n          command: |\n            docker version\n            docker info\n\n      - restore_cache:\n          keys:\n            - cache-{{ .Branch }}\n          paths:\n            - /cache/layers.tar\n\n      - run:\n          name: Loading docker cache\n          command: |\n            if [[ -f /cache/layers.tar ]]; then\n              docker load -i /cache/layers.tar\n            fi\n\n      - run:\n          name: Build docker image\n          command: |\n            docker build --cache-from=${IMAGE_NAME} -t ${IMAGE_NAME} .\n\n      - run:\n          name: Generate docker build image cache\n          command: |\n            mkdir -p /cache\n            docker save -o /cache/layers.tar ${IMAGE_NAME}\n\n      - save_cache:\n          key: cache-{{ .Branch }}-{{ epoch }}\n          paths:\n            - /cache/layers.tar\n\nworkflows:\n  version: 2\n  build-and-test:\n    jobs:\n      - build:\n          filters:\n            branches:\n              only: /.*/\n            tags:\n              only: /.*/\n"
  },
  {
    "path": ".dockerignore",
    "content": ".git\ncircle.yml\nLICENSE\nVERSION\nREADME.md\nChangelog.md\nMakefile\n"
  },
  {
    "path": "Changelog.md",
    "content": "# Changelog\n\n**5.7.26-0**\n- update base image to ubuntu:bionic-20190612\n- upgrade to mysql-server 5.7.26\n\n**5.7.24**\n- update base image to ubuntu:bionic-20181204\n- upgrade to mysql-server 5.7.24\n\n**5.7.22**\n- mount volume at `/var/run/mysqld` allowing the mysql unix socket can be exposed\n- determine remote access filter based on the docker network (first run only).\n- switched to sameersbn/ubuntu:14.04.20140818 base image\n- optimized image size by removing `/var/lib/apt/lists/*`.\n- accept connections only after we have create the users and databases\n"
  },
  {
    "path": "Dockerfile",
    "content": "FROM ubuntu:bionic-20190612\n\nLABEL maintainer=\"sameer@damagehead.com\"\n\nENV MYSQL_USER=mysql \\\n    MYSQL_VERSION=5.7.26 \\\n    MYSQL_DATA_DIR=/var/lib/mysql \\\n    MYSQL_RUN_DIR=/run/mysqld \\\n    MYSQL_LOG_DIR=/var/log/mysql\n\nRUN apt-get update \\\n && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server=${MYSQL_VERSION}* \\\n && rm -rf ${MYSQL_DATA_DIR} \\\n && rm -rf /var/lib/apt/lists/*\n\nCOPY entrypoint.sh /sbin/entrypoint.sh\n\nRUN chmod 755 /sbin/entrypoint.sh\n\nEXPOSE 3306/tcp\n\nENTRYPOINT [\"/sbin/entrypoint.sh\"]\n\nCMD [\"/usr/bin/mysqld_safe\"]\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2014 Sameer Naik\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "all: build\n\nbuild:\n\t@docker build --tag=sameersbn/mysql .\n"
  },
  {
    "path": "README.md",
    "content": "[![Circle CI](https://circleci.com/gh/sameersbn/docker-mysql.svg?style=svg)](https://circleci.com/gh/sameersbn/docker-mysql) [![Docker Repository on Quay.io](https://quay.io/repository/sameersbn/mysql/status \"Docker Repository on Quay.io\")](https://quay.io/repository/sameersbn/mysql)\n\n# Table of Contents\n\n- [Introduction](#introduction)\n- [Contributing](#contributing)\n- [Changelog](Changelog.md)\n- [Reporting Issues](#reporting-issues)\n- [Installation](#installation)\n- [Quick Start](#quick-start)\n- [Data Store](#data-store)\n- [Creating User and Database at Launch](#creating-user-and-database-at-launch)\n- [Creating remote user with privileged access](#creating-remote-user-with-privileged-access)\n- [Shell Access](#shell-access)\n- [Upgrading](#upgrading)\n\n# Introduction\n\nDockerfile to build a MySQL container image which can be linked to other containers.\n\n# Contributing\n\nIf you find this image useful here's how you can help:\n\n- Send a Pull Request with your awesome new features and bug fixes\n- Help new users with [Issues](https://github.com/sameersbn/docker-mysql/issues) they may encounter\n- Support the development of this image with a [donation](http://www.damagehead.com/donate/)\n\n# Reporting Issues\n\nDocker is a relatively new project and is active being developed and tested by a thriving community of developers and testers and every release of docker features many enhancements and bugfixes.\n\nGiven the nature of the development and release cycle it is very important that you have the latest version of docker installed because any issue that you encounter might have already been fixed with a newer docker release.\n\nFor ubuntu users I suggest [installing docker](https://docs.docker.com/installation/ubuntulinux/) using docker's own package repository since the version of docker packaged in the ubuntu repositories are a little dated.\n\nHere is the shortform of the installation of an updated version of docker on ubuntu.\n\n```bash\nsudo apt-get purge docker.io\ncurl -s https://get.docker.io/ubuntu/ | sudo sh\nsudo apt-get update\nsudo apt-get install lxc-docker\n```\n\nFedora and RHEL/CentOS users should try disabling selinux with `setenforce 0` and check if resolves the issue. If it does than there is not much that I can help you with. You can either stick with selinux disabled (not recommended by redhat) or switch to using ubuntu.\n\nIf using the latest docker version and/or disabling selinux does not fix the issue then please file a issue request on the [issues](https://github.com/sameersbn/docker-mysql/issues) page.\n\nIn your issue report please make sure you provide the following information:\n\n- The host distribution and release version.\n- Output of the `docker version` command\n- Output of the `docker info` command\n- The `docker run` command you used to run the image (mask out the sensitive bits).\n\n# Installation\n\nAutomated builds of the image are available on [Dockerhub](https://hub.docker.com/r/sameersbn/mysql) and is the recommended method of installation.\n\n> **Note**: Builds are also available on [Quay.io](https://quay.io/repository/sameersbn/mysql)\n\n```bash\ndocker pull sameersbn/mysql:5.7.26-0\n```\n\nAlternately you can build the image yourself.\n\n```bash\ndocker build -t sameersbn/mysql github.com/sameersbn/docker-mysql\n```\n\n# Quick Start\n\nRun the mysql image\n\n```bash\ndocker run --name mysql -d sameersbn/mysql:5.7.26-0\n```\n\nYou can access the mysql server as the root user using the following command:\n\n```bash\ndocker run -it --rm --volumes-from=mysql sameersbn/mysql:5.7.26-0 mysql -uroot\n```\n\n# Data Store\n\nYou should mount a volume at `/var/lib/mysql`.\n\nSELinux users are also required to change the security context of the mount point so that it plays nicely with selinux.\n\n```bash\nmkdir -p /opt/mysql/data\nsudo chcon -Rt svirt_sandbox_file_t /opt/mysql/data\n```\n\nThe updated run command looks like this.\n\n```\ndocker run --name mysql -d \\\n  -v /opt/mysql/data:/var/lib/mysql sameersbn/mysql:5.7.26-0\n```\n\nThis will make sure that the data stored in the database is not lost when the image is stopped and started again.\n\n# Creating User and Database at Launch\n\n> **NOTE**\n>\n> For this feature to work the `debian-sys-maint` user needs to exist. This user is automatically created when the database is installed for the first time (firstrun).\n>\n> However if you were using this image before this feature was added, then it will not work as-is. You are required to create the `debian-sys-maint` user\n>\n>```bash\n>docker run -it --rm --volumes-from=mysql sameersbn/mysql \\\n>  mysql -uroot -e \"GRANT ALL PRIVILEGES on *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '' WITH GRANT OPTION;\"\n>```\n\nTo create a new database specify the database name in the `DB_NAME` variable. The following command creates a new database named *dbname*:\n\n```bash\ndocker run --name mysql -d \\\n  -e 'DB_NAME=dbname' sameersbn/mysql:5.7.26-0\n```\n\nYou may also specify a comma separated list of database names in the `DB_NAME` variable. The following command creates two new databases named *dbname1* and *dbname2*\n\n```bash\ndocker run --name mysql -d \\\n-e 'DB_NAME=dbname1,dbname2' sameersbn/mysql:5.7.26-0\n```\n\nTo create a new user you should specify the `DB_USER` and `DB_PASS` variables.\n\n```bash\ndocker run --name mysql -d \\\n  -e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' -e 'DB_NAME=dbname' \\\n  sameersbn/mysql:5.7.26-0\n```\n\nThe above command will create a user *dbuser* with the password *dbpass* and will also create a database named *dbname*. The *dbuser* user will have full/remote access to the database.\n\n**NOTE**\n- If the `DB_NAME` is not specified, the user will not be created\n- If the user/database user already exists no changes are be made\n- If `DB_PASS` is not specified, an empty password will be set for the user\n\nBy default the new database will be created with the `utf8` character set and `utf8_unicode_ci` collation. You may override these with the `MYSQL_CHARSET` and `MYSQL_COLLATION` variables.\n\n```bash\ndocker run --name mysql -d \\\n  -e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' -e 'DB_NAME=dbname' \\\n  -e 'MYSQL_CHARSET=utf8mb4' -e 'MYSQL_COLLATION=utf8_bin' \\\n  sameersbn/mysql:5.7.26-0\n```\n\n# Creating remote user with privileged access\n\nTo create a remote user with privileged access, you need to specify the `DB_REMOTE_ROOT_NAME` and `DB_REMOTE_ROOT_PASS` variables, eg.\n\n```bash\ndocker run --name mysql -d \\\n  -e 'DB_REMOTE_ROOT_NAME=root' -e 'DB_REMOTE_ROOT_PASS=secretpassword' \\\n  sameersbn/mysql:5.7.26-0\n```\n\nOptionally you can specify the `DB_REMOTE_ROOT_HOST` variable to define the address space within which remote access should be permitted. This defaults to `172.17.0.1` and should suffice for most cases.\n\nSituations that would require you to override the default `DB_REMOTE_ROOT_HOST` setting are:\n\n- If you have changed the ip address of the `docker0` interface\n- If you are using host networking, i.e. `--net=host`, etc.\n\n# Shell Access\n\nFor debugging and maintenance purposes you may want access the containers shell. If you are using docker version `1.3.0` or higher you can access a running containers shell using `docker exec` command.\n\n```bash\ndocker exec -it mysql bash\n```\n\nIf you are using an older version of docker, you can use the [nsenter](http://man7.org/linux/man-pages/man1/nsenter.1.html) linux tool (part of the util-linux package) to access the container shell.\n\nSome linux distros (e.g. ubuntu) use older versions of the util-linux which do not include the `nsenter` tool. To get around this @jpetazzo has created a nice docker image that allows you to install the `nsenter` utility and a helper script named `docker-enter` on these distros.\n\nTo install `nsenter` execute the following command on your host,\n\n```bash\ndocker run --rm -v /usr/local/bin:/target jpetazzo/nsenter\n```\n\nNow you can access the container shell using the command\n\n```bash\nsudo docker-enter mysql\n```\n\nFor more information refer https://github.com/jpetazzo/nsenter\n\n# Upgrading\n\nTo upgrade to newer releases, simply follow this 3 step upgrade procedure.\n\n- **Step 1**: Stop the currently running image\n\n```bash\ndocker stop mysql\n```\n\n- **Step 2**: Update the docker image.\n\n```bash\ndocker pull sameersbn/mysql:5.7.26-0\n```\n\n- **Step 3**: Start the image\n\n```bash\ndocker run --name mysql -d [OPTIONS] sameersbn/mysql:5.7.26-0\n```\n"
  },
  {
    "path": "VERSION",
    "content": "5.7.26-0\n"
  },
  {
    "path": "circle.yml",
    "content": "machine:\n  services:\n    - docker\n\ndependencies:\n  override:\n    - docker info\n\ntest:\n  override:\n    - docker build -t sameersbn/mysql .\n    - docker run --name=mysql -d sameersbn/mysql; sleep 10\n    - docker run --volumes-from=mysql sameersbn/mysql mysqladmin status\n"
  },
  {
    "path": "entrypoint.sh",
    "content": "#!/bin/bash\nset -e\n\n[[ $DEBUG == true ]] && set -x\n\nDB_NAME=${DB_NAME:-}\nDB_USER=${DB_USER:-}\nDB_PASS=${DB_PASS:-}\n\nDB_REMOTE_ROOT_NAME=${DB_REMOTE_ROOT_NAME:-}\nDB_REMOTE_ROOT_PASS=${DB_REMOTE_ROOT_PASS:-}\nDB_REMOTE_ROOT_HOST=${DB_REMOTE_ROOT_HOST:-\"172.17.0.1\"}\n\nMYSQL_CHARSET=${MYSQL_CHARSET:-\"utf8\"}\nMYSQL_COLLATION=${MYSQL_COLLATION:-\"utf8_unicode_ci\"}\n\ncreate_data_dir() {\n  mkdir -p ${MYSQL_DATA_DIR}\n  chmod -R 0700 ${MYSQL_DATA_DIR}\n  chown -R ${MYSQL_USER}:${MYSQL_USER} ${MYSQL_DATA_DIR}\n}\n\ncreate_run_dir() {\n  mkdir -p ${MYSQL_RUN_DIR}\n  chmod -R 0755 ${MYSQL_RUN_DIR}\n  chown -R ${MYSQL_USER}:root ${MYSQL_RUN_DIR}\n\n  # hack: remove any existing lock files\n  rm -rf ${MYSQL_RUN_DIR}/mysqld.sock.lock\n}\n\ncreate_log_dir() {\n  mkdir -p ${MYSQL_LOG_DIR}\n  chmod -R 0755 ${MYSQL_LOG_DIR}\n  chown -R ${MYSQL_USER}:${MYSQL_USER} ${MYSQL_LOG_DIR}\n}\n\nlisten() {\n  sed -e \"s/^bind-address\\(.*\\)=.*/bind-address = $1/\" -i /etc/mysql/mysql.conf.d/mysqld.cnf\n}\n\napply_configuration_fixes() {\n  # disable error log\n  sed 's/^log_error/# log_error/' -i /etc/mysql/mysql.conf.d/mysqld.cnf\n\n  # Fixing StartUp Porblems with some DNS Situations and Speeds up the stuff\n  # http://www.percona.com/blog/2008/05/31/dns-achilles-heel-mysql-installation/\n  cat > /etc/mysql/conf.d/mysql-skip-name-resolv.cnf <<EOF\n[mysqld]\nskip_name_resolve\nEOF\n}\n\nremove_debian_system_maint_password() {\n  #\n  # the default password for the debian-sys-maint user is randomly generated\n  # during the installation of the mysql-server package.\n  #\n  # Due to the nature of docker we blank out the password such that the maintenance\n  # user can login without a password.\n  #\n  sed 's/password = .*/password = /g' -i /etc/mysql/debian.cnf\n}\n\ninitialize_mysql_database() {\n  # initialize MySQL data directory\n  if [ ! -d ${MYSQL_DATA_DIR}/mysql ]; then\n    echo \"Installing database...\"\n    mysqld --initialize-insecure --user=mysql >/dev/null 2>&1\n\n    # start mysql server\n    echo \"Starting MySQL server...\"\n    /usr/bin/mysqld_safe >/dev/null 2>&1 &\n\n    # wait for mysql server to start (max 30 seconds)\n    timeout=30\n    echo -n \"Waiting for database server to accept connections\"\n    while ! /usr/bin/mysqladmin -u root status >/dev/null 2>&1\n    do\n      timeout=$(($timeout - 1))\n      if [ $timeout -eq 0 ]; then\n        echo -e \"\\nCould not connect to database server. Aborting...\"\n        exit 1\n      fi\n      echo -n \".\"\n      sleep 1\n    done\n    echo\n\n    ## create a localhost only, debian-sys-maint user\n    ## the debian-sys-maint is used while creating users and database\n    ## as well as to shut down or starting up the mysql server via mysqladmin\n    echo \"Creating debian-sys-maint user...\"\n    mysql -uroot -e \"CREATE USER 'debian-sys-maint'@'localhost' IDENTIFIED BY '';\"\n    mysql -uroot -e \"GRANT ALL PRIVILEGES on *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '' WITH GRANT OPTION;\"\n\n    if [ -n \"${DB_REMOTE_ROOT_NAME}\" -a -n \"${DB_REMOTE_ROOT_HOST}\" ]; then\n      echo \"Creating remote user \\\"${DB_REMOTE_ROOT_NAME}\\\" with root privileges...\"\n      mysql -uroot \\\n      -e \"GRANT ALL PRIVILEGES ON *.* TO '${DB_REMOTE_ROOT_NAME}'@'${DB_REMOTE_ROOT_HOST}' IDENTIFIED BY '${DB_REMOTE_ROOT_PASS}' WITH GRANT OPTION; FLUSH PRIVILEGES;\"\n    fi\n\n    /usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf shutdown\n  fi\n}\n\ncreate_users_and_databases() {\n  # create new user / database\n  if [ -n \"${DB_USER}\" -o -n \"${DB_NAME}\" ]; then\n    /usr/bin/mysqld_safe >/dev/null 2>&1 &\n\n    # wait for mysql server to start (max 30 seconds)\n    timeout=30\n    while ! /usr/bin/mysqladmin -u root status >/dev/null 2>&1\n    do\n      timeout=$(($timeout - 1))\n      if [ $timeout -eq 0 ]; then\n        echo \"Could not connect to mysql server. Aborting...\"\n        exit 1\n      fi\n      sleep 1\n    done\n\n    if [ -n \"${DB_NAME}\" ]; then\n      for db in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< \"${DB_NAME}\"); do\n        echo \"Creating database \\\"$db\\\"...\"\n        mysql --defaults-file=/etc/mysql/debian.cnf \\\n          -e \"CREATE DATABASE IF NOT EXISTS \\`$db\\` DEFAULT CHARACTER SET \\`$MYSQL_CHARSET\\` COLLATE \\`$MYSQL_COLLATION\\`;\"\n          if [ -n \"${DB_USER}\" ]; then\n            echo \"Granting access to database \\\"$db\\\" for user \\\"${DB_USER}\\\"...\"\n            mysql --defaults-file=/etc/mysql/debian.cnf \\\n            -e \"GRANT ALL PRIVILEGES ON \\`$db\\`.* TO '${DB_USER}' IDENTIFIED BY '${DB_PASS}';\"\n          fi\n        done\n    fi\n    /usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf shutdown\n  fi\n}\n\ncreate_data_dir\ncreate_run_dir\ncreate_log_dir\n\n# allow arguments to be passed to mysqld_safe\nif [[ ${1:0:1} = '-' ]]; then\n  EXTRA_ARGS=\"$@\"\n  set --\nelif [[ ${1} == mysqld_safe || ${1} == $(which mysqld_safe) ]]; then\n  EXTRA_ARGS=\"${@:2}\"\n  set --\nfi\n\n# default behaviour is to launch mysqld_safe\nif [[ -z ${1} ]]; then\n  listen \"127.0.0.1\"\n  apply_configuration_fixes\n  remove_debian_system_maint_password\n  initialize_mysql_database\n  create_users_and_databases\n  listen \"0.0.0.0\"\n  exec $(which mysqld_safe) $EXTRA_ARGS\nelse\n  exec \"$@\"\nfi\n"
  }
]