From 9cdf2951cd30fc47afaeeaffeea6e844be67cbbc Mon Sep 17 00:00:00 2001 From: Ben Aylott Date: Mon, 15 Aug 2016 16:55:17 +0100 Subject: [PATCH 1/2] Working configuration with SMX SD card image --- Dockerfile | 2 +- docker-compose.override.yml | 4 ++-- docker-compose.yml | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index a76d617..f9137ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # Offical Docker PHP & Apache image https://hub.docker.com/_/php/ # To do: fix compatiability with php 7 -FROM php:5.6-apache +FROM nidorpi/rpi-php:5.6-apache # Install deps RUN apt-get update && apt-get install -y \ diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 7f97d4e..cb3d506 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -6,7 +6,7 @@ services: web: ports: # Expose Emoncms on localhost port 8080 - - 8080:80 + - 4000:80 volumes: #mount emoncms files from local FS for dev - ./emoncms:/var/www/html @@ -19,4 +19,4 @@ services: - 'default.docker-env' redis: env_file: - - 'default.docker-env' \ No newline at end of file + - 'default.docker-env' diff --git a/docker-compose.yml b/docker-compose.yml index 0119733..bafd0e9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: # PHP & apache container using offical Docker PHP iamge web: # If pre-built image from docker hub exists then use that (docker pull openenergymonitor/emoncms) if not Buildcontainer see Dockerflile - image: openenergymonitor/emoncms + image: beaylott/emoncms build: . volumes: # mount docker volumes persistant inside docker container @@ -22,15 +22,15 @@ services: # MYSQL Database db: # Official MYSQL docker image - # To do: make compatiable with 5.7 - image: mysql:5.6 + # To do: make compatiable with 5.7 (B: or even 5.6?!) + image: beaylott/rpi-mysql:5.5 environment: - "MYSQL_HOST=localhost" volumes: - emon-db-data:/var/lib/mysql/data redis: # Official redis image - image: redis:3.0.7 + image: hypriot/rpi-redis:3.0.4 volumes: - emon-redis-data:/data @@ -44,4 +44,4 @@ volumes: emon-db-data: driver: local emon-redis-data: - driver: local \ No newline at end of file + driver: local From b32ef1a2d2275f93a055b97de2daeaf55831dafd Mon Sep 17 00:00:00 2001 From: Ben Aylott Date: Mon, 15 Aug 2016 17:28:47 +0100 Subject: [PATCH 2/2] added modified rpi-mysql image files --- rpi-mysql/Dockerfile | 37 +++++++++ rpi-mysql/LICENSE | 22 ++++++ rpi-mysql/Makefile | 23 ++++++ rpi-mysql/README.md | 165 ++++++++++++++++++++++++++++++++++++++++ rpi-mysql/entrypoint.sh | 55 ++++++++++++++ 5 files changed, 302 insertions(+) create mode 100644 rpi-mysql/Dockerfile create mode 100644 rpi-mysql/LICENSE create mode 100644 rpi-mysql/Makefile create mode 100644 rpi-mysql/README.md create mode 100755 rpi-mysql/entrypoint.sh diff --git a/rpi-mysql/Dockerfile b/rpi-mysql/Dockerfile new file mode 100644 index 0000000..05a5fe2 --- /dev/null +++ b/rpi-mysql/Dockerfile @@ -0,0 +1,37 @@ +# Pull base image +FROM resin/rpi-raspbian:wheezy +MAINTAINER Govinda fichtner + +# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added +RUN groupadd -r mysql && useradd -r -g mysql mysql + +# FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db: +# File::Basename +# File::Copy +# Sys::Hostname +# Data::Dumper +RUN apt-get update && apt-get install -y perl --no-install-recommends && rm -rf /var/lib/apt/lists/* + +ENV MYSQL_VERSION 5.5 + +# the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql) +# also, we set debconf keys to make APT a little quieter +RUN { \ + echo mysql-server mysql-server/data-dir select ''; \ + echo mysql-server mysql-server/root-pass password ''; \ + echo mysql-server mysql-server/re-root-pass password ''; \ + echo mysql-server mysql-server/remove-test-db select false; \ + } | debconf-set-selections \ + && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}"* && rm -rf /var/lib/apt/lists/* \ + && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql && chown -R mysql:mysql /var/lib/mysql + +# comment out a few problematic configuration values +RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf + +VOLUME /var/lib/mysql + +COPY entrypoint.sh / +ENTRYPOINT ["/entrypoint.sh"] + +EXPOSE 3306 +CMD ["mysqld"] diff --git a/rpi-mysql/LICENSE b/rpi-mysql/LICENSE new file mode 100644 index 0000000..0db8419 --- /dev/null +++ b/rpi-mysql/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Hypriot + +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. + diff --git a/rpi-mysql/Makefile b/rpi-mysql/Makefile new file mode 100644 index 0000000..636763a --- /dev/null +++ b/rpi-mysql/Makefile @@ -0,0 +1,23 @@ +DOCKER_IMAGE_VERSION=5.5 +DOCKER_IMAGE_NAME=hypriot/rpi-mysql +DOCKER_IMAGE_TAGNAME=$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_VERSION) + +default: build + +build: + docker build -t $(DOCKER_IMAGE_TAGNAME) . + docker tag -f $(DOCKER_IMAGE_TAGNAME) $(DOCKER_IMAGE_NAME):latest + +push: + docker push $(DOCKER_IMAGE_NAME) + +test: + docker run --rm $(DOCKER_IMAGE_TAGNAME) /bin/echo "Success." + +version: + docker run --rm $(DOCKER_IMAGE_TAGNAME) mysql --version + +rmi: + docker rmi -f $(DOCKER_IMAGE_TAGNAME) + +rebuild: rmi build diff --git a/rpi-mysql/README.md b/rpi-mysql/README.md new file mode 100644 index 0000000..8af1a4a --- /dev/null +++ b/rpi-mysql/README.md @@ -0,0 +1,165 @@ +# rpi-mysql + +Raspberry Pi compatible Docker base image with [MySQL](https://www.mysql.com/). +It is based on the official [Mysql Docker Image](https://registry.hub.docker.com/_/mysql/) ported to the ARM based Raspbery Pi. + +## What is MySQL? + +![logo](https://raw.githubusercontent.com/docker-library/docs/master/mysql/logo.png) + +MySQL is the world's most popular open source database. With its proven performance, reliability and ease-of-use, MySQL has become the leading database choice for web-based applications, covering the entire range from personal projects and websites, via e-commerce and information services, all the way to high profile web properties including Facebook, Twitter, YouTube, Yahoo! and many more. + +For more information and related downloads for MySQL Server and other MySQL products, please visit [www.mysql.com](http://www.mysql.com). + +## Build Details +- [Source Project Page](https://github.com/hypriot) +- [Source Repository](https://github.com/hypriot/rpi-mysql) +- [Dockerfile](https://github.com/hypriot/rpi-mysql/blob/master/Dockerfile) +- [DockerHub] (https://registry.hub.docker.com/u/hypriot/rpi-mysql/) + +## Build the Docker Image +Run all the commands from within the project root directory. + +```bash +make build +``` + +### Run the Docker Image and get the version of the installed mysql +```bash +make version +``` + +### Push the Docker Image to the Docker Hub +* First use a `docker login` with username, password and email address +* Second push the Docker Image to the official Docker Hub + +```bash +make push +``` + +## How to use this image + +### Start a MySQL server instance + +Starting a MySQL instance is simple: + + docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag + +... where `some-mysql` is the name you want to assign to your container, `my-secret-pw` is the password to be set for the MySQL root user and `tag` is the tag specifying the MySQL version you want. See the list above for relevant tags. + +### Connect to MySQL from an application in another Docker container + +This image exposes the standard MySQL port (3306), so container linking makes the MySQL instance available to other application containers. Start your application container like this in order to link it to the MySQL container: + + docker run --name some-app --link some-mysql:mysql -d app-that-uses-mysql + +### Connect to MySQL from the MySQL command line client + +The following command starts another MySQL container instance and runs the `mysql` command line client against your original MySQL container, allowing you to execute SQL statements against your database instance: + + docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"' + +... where `some-mysql` is the name of your original MySQL Server container. + +More information about the MySQL command line client can be found in the [MySQL documentation](http://dev.mysql.com/doc/en/mysql.html) + +### Container shell access and viewing MySQL logs + +The `docker exec` command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your `mysql` container: + + docker exec -it some-mysql bash + +The MySQL Server log is available through Docker's container log: + + docker logs some-mysql + +### Using a custom MySQL configuration file + +The MySQL startup configuration is specified in the file `/etc/mysql/my.cnf`, and that file in turn includes any files found in the `/etc/mysql/conf.d` directory. Settings in files in this directory will augment and/or override settings in `/etc/mysql/my.cnf`. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as `/etc/mysql/conf.d` inside the `mysql` container. + +If `/my/custom/config-file` is the path and name of your custom configuration file, you can start your `mysql` container like this (note that only the directory path of the custom config file is used in this command): + + docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag + +This will start a new container `some-mysql` where the MySQL instance uses the combined startup settings from `/etc/mysql/my.cnf` and `/etc/mysql/conf.d/config-file`, with settings from the latter taking precedence. + +Note that users on host systems with SELinux enabled may see issues with this. The current workaround is to assign the relevant SELinux policy type to your new config file so that the container will be allowed to mount it: + + chcon -Rt svirt_sandbox_file_t /my/custom + +### Environment Variables + +When you start the `mysql` image, you can adjust the configuration of the MySQL instance by passing one or more environment variables on the `docker run` command line. Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup. + +#### `MYSQL_ROOT_PASSWORD` + +This variable is mandatory and specifies the password that will be set for the MySQL `root` superuser account. In the above example, it was set to `my-secret-pw`. + +#### `MYSQL_DATABASE` + +This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access ([corresponding to `GRANT ALL`](http://dev.mysql.com/doc/en/adding-users.html)) to this database. + +#### `MYSQL_USER`, `MYSQL_PASSWORD` + +These variables are optional, used in conjunction to create a new user and to set that user's password. This user will be granted superuser permissions (see above) for the database specified by the `MYSQL_DATABASE` variable. Both variables are required for a user to be created. + +Do note that there is no need to use this mechanism to create the root superuser, that user gets created by default with the password specified by the `MYSQL_ROOT_PASSWORD` variable. + +#### `MYSQL_ALLOW_EMPTY_PASSWORD` + +This is an optional variable. Set to `yes` to allow the container to be started with a blank password for the root user. *NOTE*: Setting this variable to `yes` is not recommended unless you really know what you are doing, since this will leave your MySQL instance completely unprotected, allowing anyone to gain complete superuser access. + +## Caveats + +### Where to Store Data + +Important note: There are several ways to store data used by applications that run in Docker containers. We encourage users of the `mysql` images to familiarize themselves with the options available, including: + +- Let Docker manage the storage of your database data [by writing the database files to disk on the host system using its own internal volume management](https://docs.docker.com/userguide/dockervolumes/#adding-a-data-volume). This is the default and is easy and fairly transparent to the user. The downside is that the files may be hard to locate for tools and applications that run directly on the host system, i.e. outside containers. +- Create a data directory on the host system (outside the container) and [mount this to a directory visible from inside the container](https://docs.docker.com/userguide/dockervolumes/#mount-a-host-directory-as-a-data-volume). This places the database files in a known location on the host system, and makes it easy for tools and applications on the host system to access the files. The downside is that the user needs to make sure that the directory exists, and that e.g. directory permissions and other security mechanisms on the host system are set up correctly. + +The Docker documentation is a good starting point for understanding the different storage options and variations, and there are multiple blogs and forum postings that discuss and give advice in this area. We will simply show the basic procedure here for the latter option above: + +1. Create a data directory on a suitable volume on your host system, e.g. `/my/own/datadir`. +2. Start your `mysql` container like this: + + docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag + +The `-v /my/own/datadir:/var/lib/mysql` part of the command mounts the `/my/own/datadir` directory from the underlying host system as `/var/lib/mysql` inside the container, where MySQL by default will write its data files. + +Note that users on host systems with SELinux enabled may see issues with this. The current workaround is to assign the relevant SELinux policy type to the new data directory so that the container will be allowed to access it: + + chcon -Rt svirt_sandbox_file_t /my/own/datadir + +### No connections until MySQL init completes + +If there is no database initialized when the container starts, then a default database will be created. While this is the expected behavior, this means that it will not accept incoming connections until such initialization completes. This may cause issues when using automation tools, such as `docker-compose`, which start several containers simultaneously. + +### Usage against an existing database + +If you start your `mysql` container instance with a data directory that already contains a database (specifically, a `mysql` subdirectory), the `$MYSQL_ROOT_PASSWORD` variable should be omitted from the run command line; it will in any case be ignored, and the pre-existing database will not be changed in any way. + + +## License + +The MIT License (MIT) + +Copyright (c) 2015 Hypriot + +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. diff --git a/rpi-mysql/entrypoint.sh b/rpi-mysql/entrypoint.sh new file mode 100755 index 0000000..e806760 --- /dev/null +++ b/rpi-mysql/entrypoint.sh @@ -0,0 +1,55 @@ +#!/bin/bash +set -e + +if [ "${1:0:1}" = '-' ]; then + set -- mysqld "$@" +fi + +if [ "$1" = 'mysqld' ]; then + # read DATADIR from the MySQL config + DATADIR="$("$@" --verbose --help 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" + + if [ ! -d "$DATADIR/mysql" ]; then + if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD"]; then + echo >&2 'error: database is uninitialized and MYSQL_ROOT_PASSWORD not set' + echo >&2 ' Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?' + exit 1 + fi + + echo 'Initializing database' + mysql_install_db --datadir="$DATADIR" + echo 'Database initialized' + + # These statements _must_ be on individual lines, and _must_ end with + # semicolons (no line breaks or comments are permitted). + # TODO proper SQL escaping on ALL the things D: + + tempSqlFile='/tmp/mysql-first-time.sql' + cat > "$tempSqlFile" <<-EOSQL + DELETE FROM mysql.user ; + CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ; + GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ; + DROP DATABASE IF EXISTS test ; + EOSQL + + if [ "$MYSQL_DATABASE" ]; then + echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile" + fi + + if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then + echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile" + + if [ "$MYSQL_DATABASE" ]; then + echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" >> "$tempSqlFile" + fi + fi + + echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile" + + set -- "$@" --init-file="$tempSqlFile" + fi + + chown -R mysql:mysql "$DATADIR" +fi + +exec "$@"