Repository: sameersbn/docker-mysql Branch: master Commit: 5806e619d803 Files: 10 Total size: 16.8 KB Directory structure: gitextract_47vhvkc6/ ├── .circleci/ │ └── config.yml ├── .dockerignore ├── Changelog.md ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── VERSION ├── circle.yml └── entrypoint.sh ================================================ FILE CONTENTS ================================================ ================================================ FILE: .circleci/config.yml ================================================ version: 2 jobs: build: working_directory: /workdir docker: - image: docker:18.03.0-ce-git environment: IMAGE_NAME: "sameersbn/mysql" steps: - checkout - setup_remote_docker: version: 18.03.1-ce - run: name: Docker info command: | docker version docker info - restore_cache: keys: - cache-{{ .Branch }} paths: - /cache/layers.tar - run: name: Loading docker cache command: | if [[ -f /cache/layers.tar ]]; then docker load -i /cache/layers.tar fi - run: name: Build docker image command: | docker build --cache-from=${IMAGE_NAME} -t ${IMAGE_NAME} . - run: name: Generate docker build image cache command: | mkdir -p /cache docker save -o /cache/layers.tar ${IMAGE_NAME} - save_cache: key: cache-{{ .Branch }}-{{ epoch }} paths: - /cache/layers.tar workflows: version: 2 build-and-test: jobs: - build: filters: branches: only: /.*/ tags: only: /.*/ ================================================ FILE: .dockerignore ================================================ .git circle.yml LICENSE VERSION README.md Changelog.md Makefile ================================================ FILE: Changelog.md ================================================ # Changelog **5.7.26-0** - update base image to ubuntu:bionic-20190612 - upgrade to mysql-server 5.7.26 **5.7.24** - update base image to ubuntu:bionic-20181204 - upgrade to mysql-server 5.7.24 **5.7.22** - mount volume at `/var/run/mysqld` allowing the mysql unix socket can be exposed - determine remote access filter based on the docker network (first run only). - switched to sameersbn/ubuntu:14.04.20140818 base image - optimized image size by removing `/var/lib/apt/lists/*`. - accept connections only after we have create the users and databases ================================================ FILE: Dockerfile ================================================ FROM ubuntu:bionic-20190612 LABEL maintainer="sameer@damagehead.com" ENV MYSQL_USER=mysql \ MYSQL_VERSION=5.7.26 \ MYSQL_DATA_DIR=/var/lib/mysql \ MYSQL_RUN_DIR=/run/mysqld \ MYSQL_LOG_DIR=/var/log/mysql RUN apt-get update \ && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server=${MYSQL_VERSION}* \ && rm -rf ${MYSQL_DATA_DIR} \ && rm -rf /var/lib/apt/lists/* COPY entrypoint.sh /sbin/entrypoint.sh RUN chmod 755 /sbin/entrypoint.sh EXPOSE 3306/tcp ENTRYPOINT ["/sbin/entrypoint.sh"] CMD ["/usr/bin/mysqld_safe"] ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2014 Sameer Naik Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Makefile ================================================ all: build build: @docker build --tag=sameersbn/mysql . ================================================ FILE: README.md ================================================ [![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) # Table of Contents - [Introduction](#introduction) - [Contributing](#contributing) - [Changelog](Changelog.md) - [Reporting Issues](#reporting-issues) - [Installation](#installation) - [Quick Start](#quick-start) - [Data Store](#data-store) - [Creating User and Database at Launch](#creating-user-and-database-at-launch) - [Creating remote user with privileged access](#creating-remote-user-with-privileged-access) - [Shell Access](#shell-access) - [Upgrading](#upgrading) # Introduction Dockerfile to build a MySQL container image which can be linked to other containers. # Contributing If you find this image useful here's how you can help: - Send a Pull Request with your awesome new features and bug fixes - Help new users with [Issues](https://github.com/sameersbn/docker-mysql/issues) they may encounter - Support the development of this image with a [donation](http://www.damagehead.com/donate/) # Reporting Issues Docker 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. Given 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. For 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. Here is the shortform of the installation of an updated version of docker on ubuntu. ```bash sudo apt-get purge docker.io curl -s https://get.docker.io/ubuntu/ | sudo sh sudo apt-get update sudo apt-get install lxc-docker ``` Fedora 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. If 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. In your issue report please make sure you provide the following information: - The host distribution and release version. - Output of the `docker version` command - Output of the `docker info` command - The `docker run` command you used to run the image (mask out the sensitive bits). # Installation Automated builds of the image are available on [Dockerhub](https://hub.docker.com/r/sameersbn/mysql) and is the recommended method of installation. > **Note**: Builds are also available on [Quay.io](https://quay.io/repository/sameersbn/mysql) ```bash docker pull sameersbn/mysql:5.7.26-0 ``` Alternately you can build the image yourself. ```bash docker build -t sameersbn/mysql github.com/sameersbn/docker-mysql ``` # Quick Start Run the mysql image ```bash docker run --name mysql -d sameersbn/mysql:5.7.26-0 ``` You can access the mysql server as the root user using the following command: ```bash docker run -it --rm --volumes-from=mysql sameersbn/mysql:5.7.26-0 mysql -uroot ``` # Data Store You should mount a volume at `/var/lib/mysql`. SELinux users are also required to change the security context of the mount point so that it plays nicely with selinux. ```bash mkdir -p /opt/mysql/data sudo chcon -Rt svirt_sandbox_file_t /opt/mysql/data ``` The updated run command looks like this. ``` docker run --name mysql -d \ -v /opt/mysql/data:/var/lib/mysql sameersbn/mysql:5.7.26-0 ``` This will make sure that the data stored in the database is not lost when the image is stopped and started again. # Creating User and Database at Launch > **NOTE** > > 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). > > 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 > >```bash >docker run -it --rm --volumes-from=mysql sameersbn/mysql \ > mysql -uroot -e "GRANT ALL PRIVILEGES on *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '' WITH GRANT OPTION;" >``` To create a new database specify the database name in the `DB_NAME` variable. The following command creates a new database named *dbname*: ```bash docker run --name mysql -d \ -e 'DB_NAME=dbname' sameersbn/mysql:5.7.26-0 ``` You 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* ```bash docker run --name mysql -d \ -e 'DB_NAME=dbname1,dbname2' sameersbn/mysql:5.7.26-0 ``` To create a new user you should specify the `DB_USER` and `DB_PASS` variables. ```bash docker run --name mysql -d \ -e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' -e 'DB_NAME=dbname' \ sameersbn/mysql:5.7.26-0 ``` The 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. **NOTE** - If the `DB_NAME` is not specified, the user will not be created - If the user/database user already exists no changes are be made - If `DB_PASS` is not specified, an empty password will be set for the user By 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. ```bash docker run --name mysql -d \ -e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' -e 'DB_NAME=dbname' \ -e 'MYSQL_CHARSET=utf8mb4' -e 'MYSQL_COLLATION=utf8_bin' \ sameersbn/mysql:5.7.26-0 ``` # Creating remote user with privileged access To create a remote user with privileged access, you need to specify the `DB_REMOTE_ROOT_NAME` and `DB_REMOTE_ROOT_PASS` variables, eg. ```bash docker run --name mysql -d \ -e 'DB_REMOTE_ROOT_NAME=root' -e 'DB_REMOTE_ROOT_PASS=secretpassword' \ sameersbn/mysql:5.7.26-0 ``` Optionally 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. Situations that would require you to override the default `DB_REMOTE_ROOT_HOST` setting are: - If you have changed the ip address of the `docker0` interface - If you are using host networking, i.e. `--net=host`, etc. # Shell Access For 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. ```bash docker exec -it mysql bash ``` If 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. Some 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. To install `nsenter` execute the following command on your host, ```bash docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter ``` Now you can access the container shell using the command ```bash sudo docker-enter mysql ``` For more information refer https://github.com/jpetazzo/nsenter # Upgrading To upgrade to newer releases, simply follow this 3 step upgrade procedure. - **Step 1**: Stop the currently running image ```bash docker stop mysql ``` - **Step 2**: Update the docker image. ```bash docker pull sameersbn/mysql:5.7.26-0 ``` - **Step 3**: Start the image ```bash docker run --name mysql -d [OPTIONS] sameersbn/mysql:5.7.26-0 ``` ================================================ FILE: VERSION ================================================ 5.7.26-0 ================================================ FILE: circle.yml ================================================ machine: services: - docker dependencies: override: - docker info test: override: - docker build -t sameersbn/mysql . - docker run --name=mysql -d sameersbn/mysql; sleep 10 - docker run --volumes-from=mysql sameersbn/mysql mysqladmin status ================================================ FILE: entrypoint.sh ================================================ #!/bin/bash set -e [[ $DEBUG == true ]] && set -x DB_NAME=${DB_NAME:-} DB_USER=${DB_USER:-} DB_PASS=${DB_PASS:-} DB_REMOTE_ROOT_NAME=${DB_REMOTE_ROOT_NAME:-} DB_REMOTE_ROOT_PASS=${DB_REMOTE_ROOT_PASS:-} DB_REMOTE_ROOT_HOST=${DB_REMOTE_ROOT_HOST:-"172.17.0.1"} MYSQL_CHARSET=${MYSQL_CHARSET:-"utf8"} MYSQL_COLLATION=${MYSQL_COLLATION:-"utf8_unicode_ci"} create_data_dir() { mkdir -p ${MYSQL_DATA_DIR} chmod -R 0700 ${MYSQL_DATA_DIR} chown -R ${MYSQL_USER}:${MYSQL_USER} ${MYSQL_DATA_DIR} } create_run_dir() { mkdir -p ${MYSQL_RUN_DIR} chmod -R 0755 ${MYSQL_RUN_DIR} chown -R ${MYSQL_USER}:root ${MYSQL_RUN_DIR} # hack: remove any existing lock files rm -rf ${MYSQL_RUN_DIR}/mysqld.sock.lock } create_log_dir() { mkdir -p ${MYSQL_LOG_DIR} chmod -R 0755 ${MYSQL_LOG_DIR} chown -R ${MYSQL_USER}:${MYSQL_USER} ${MYSQL_LOG_DIR} } listen() { sed -e "s/^bind-address\(.*\)=.*/bind-address = $1/" -i /etc/mysql/mysql.conf.d/mysqld.cnf } apply_configuration_fixes() { # disable error log sed 's/^log_error/# log_error/' -i /etc/mysql/mysql.conf.d/mysqld.cnf # Fixing StartUp Porblems with some DNS Situations and Speeds up the stuff # http://www.percona.com/blog/2008/05/31/dns-achilles-heel-mysql-installation/ cat > /etc/mysql/conf.d/mysql-skip-name-resolv.cnf </dev/null 2>&1 # start mysql server echo "Starting MySQL server..." /usr/bin/mysqld_safe >/dev/null 2>&1 & # wait for mysql server to start (max 30 seconds) timeout=30 echo -n "Waiting for database server to accept connections" while ! /usr/bin/mysqladmin -u root status >/dev/null 2>&1 do timeout=$(($timeout - 1)) if [ $timeout -eq 0 ]; then echo -e "\nCould not connect to database server. Aborting..." exit 1 fi echo -n "." sleep 1 done echo ## create a localhost only, debian-sys-maint user ## the debian-sys-maint is used while creating users and database ## as well as to shut down or starting up the mysql server via mysqladmin echo "Creating debian-sys-maint user..." mysql -uroot -e "CREATE USER 'debian-sys-maint'@'localhost' IDENTIFIED BY '';" mysql -uroot -e "GRANT ALL PRIVILEGES on *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '' WITH GRANT OPTION;" if [ -n "${DB_REMOTE_ROOT_NAME}" -a -n "${DB_REMOTE_ROOT_HOST}" ]; then echo "Creating remote user \"${DB_REMOTE_ROOT_NAME}\" with root privileges..." mysql -uroot \ -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;" fi /usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf shutdown fi } create_users_and_databases() { # create new user / database if [ -n "${DB_USER}" -o -n "${DB_NAME}" ]; then /usr/bin/mysqld_safe >/dev/null 2>&1 & # wait for mysql server to start (max 30 seconds) timeout=30 while ! /usr/bin/mysqladmin -u root status >/dev/null 2>&1 do timeout=$(($timeout - 1)) if [ $timeout -eq 0 ]; then echo "Could not connect to mysql server. Aborting..." exit 1 fi sleep 1 done if [ -n "${DB_NAME}" ]; then for db in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< "${DB_NAME}"); do echo "Creating database \"$db\"..." mysql --defaults-file=/etc/mysql/debian.cnf \ -e "CREATE DATABASE IF NOT EXISTS \`$db\` DEFAULT CHARACTER SET \`$MYSQL_CHARSET\` COLLATE \`$MYSQL_COLLATION\`;" if [ -n "${DB_USER}" ]; then echo "Granting access to database \"$db\" for user \"${DB_USER}\"..." mysql --defaults-file=/etc/mysql/debian.cnf \ -e "GRANT ALL PRIVILEGES ON \`$db\`.* TO '${DB_USER}' IDENTIFIED BY '${DB_PASS}';" fi done fi /usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf shutdown fi } create_data_dir create_run_dir create_log_dir # allow arguments to be passed to mysqld_safe if [[ ${1:0:1} = '-' ]]; then EXTRA_ARGS="$@" set -- elif [[ ${1} == mysqld_safe || ${1} == $(which mysqld_safe) ]]; then EXTRA_ARGS="${@:2}" set -- fi # default behaviour is to launch mysqld_safe if [[ -z ${1} ]]; then listen "127.0.0.1" apply_configuration_fixes remove_debian_system_maint_password initialize_mysql_database create_users_and_databases listen "0.0.0.0" exec $(which mysqld_safe) $EXTRA_ARGS else exec "$@" fi