├── Dockerfile ├── LICENSE ├── Makefile ├── README.md └── scripts ├── first_run.sh ├── normal_run.sh └── start.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | #MariaDB (https://mariadb.org/) 2 | 3 | FROM phusion/baseimage:0.9.10 4 | MAINTAINER Ryan Seto 5 | 6 | # Ensure UTF-8 7 | RUN locale-gen en_US.UTF-8 8 | 9 | # Disable SSH (Not using it at the moment). 10 | RUN rm -rf /etc/service/sshd /etc/my_init.d/00_regen_ssh_host_keys.sh 11 | 12 | # Install MariaDB from repository. 13 | RUN echo "deb http://ftp.osuosl.org/pub/mariadb/repo/5.5/ubuntu trusty main" > /etc/apt/sources.list.d/mariadb.list && \ 14 | apt-get update && \ 15 | DEBIAN_FRONTEND=noninteractive apt-get install -y --force-yes mariadb-server mariadb-server-5.5 16 | 17 | # Install other tools. 18 | RUN DEBIAN_FRONTEND=noninteractive apt-get install -y pwgen inotify-tools 19 | 20 | # Clean up APT when done. 21 | RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* 22 | 23 | # Configure the database to use our data dir. 24 | RUN sed -i -e 's/^datadir\s*=.*/datadir = \/data/' /etc/mysql/my.cnf 25 | 26 | # Configure MariaDB to listen on any address. 27 | RUN sed -i -e 's/^bind-address/#bind-address/' /etc/mysql/my.cnf 28 | 29 | # Change the innodb-buffer-pool-size to 128M (default is 256M). 30 | # This should make it friendlier to run on low memory servers. 31 | RUN sed -i -e 's/^innodb_buffer_pool_size\s*=.*/innodb_buffer_pool_size = 128M/' /etc/mysql/my.cnf 32 | 33 | EXPOSE 3306 34 | ADD scripts /scripts 35 | RUN chmod +x /scripts/start.sh 36 | RUN touch /firstrun 37 | 38 | # Expose our data, log, and configuration directories. 39 | VOLUME ["/data", "/var/log/mysql", "/etc/mysql"] 40 | 41 | # Use baseimage-docker's init system. 42 | CMD ["/sbin/my_init", "--", "/scripts/start.sh"] 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Ryan Seto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Substitute your own docker index username, if you like. 2 | DOCKER_USER=paintedfox 3 | 4 | # Change this to suit your needs. 5 | NAME:=mariadb 6 | USER:=super 7 | PASS:=$(shell pwgen -s -1 16) 8 | DATA_DIR:=/tmp/mariadb 9 | PORT:=127.0.0.1:3306 10 | 11 | RUNNING:=$(shell docker ps | grep $(NAME) | cut -f 1 -d ' ') 12 | ALL:=$(shell docker ps -a | grep $(NAME) | cut -f 1 -d ' ') 13 | DOCKER_RUN_COMMON=--name="$(NAME)" -p $(PORT):3306 -v $(DATA_DIR):/data -e USER="$(USER)" -e PASS="$(PASS)" $(DOCKER_USER)/mariadb 14 | 15 | all: build 16 | 17 | build: 18 | docker build -t="$(DOCKER_USER)/mariadb" . 19 | 20 | run: clean 21 | mkdir -p $(DATA_DIR) 22 | docker run -d $(DOCKER_RUN_COMMON) 23 | 24 | bash: clean 25 | mkdir -p $(DATA_DIR) 26 | docker run -t -i $(DOCKER_RUN_COMMON) /sbin/my_init -- bash -l 27 | 28 | # Removes existing containers. 29 | clean: 30 | ifneq ($(strip $(RUNNING)),) 31 | docker stop $(RUNNING) 32 | endif 33 | ifneq ($(strip $(ALL)),) 34 | docker rm $(ALL) 35 | endif 36 | 37 | # Destroys the data directory. 38 | deepclean: clean 39 | sudo rm -rf $(DATA_DIR) 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-mariadb 2 | 3 | A Dockerfile that produces a container that will run [MariaDB][mariadb] 5.5, 4 | a drop-in replacement for MySQL. 5 | 6 | [mariadb]: https://mariadb.org/ 7 | 8 | ## Image Creation 9 | 10 | This example creates the image with the tag `paintedfox/mariadb`, but you can 11 | change this to use your own username. 12 | 13 | ``` 14 | $ docker build -t="paintedfox/mariadb" . 15 | ``` 16 | 17 | Alternately, you can run the following if you have *make* installed... 18 | 19 | ``` 20 | $ make 21 | ``` 22 | 23 | You can also specify a custom docker username like so: 24 | 25 | ``` 26 | $ make DOCKER_USER=paintedfox 27 | ``` 28 | 29 | ## Container Creation / Running 30 | 31 | The MariaDB server is configured to store data in `/data` inside the container. 32 | You can map the container's `/data` volume to a volume on the host so the data 33 | becomes independant of the running container. 34 | 35 | This example uses `/tmp/mariadb` to store the MariaDB data, but you can modify 36 | this to your needs. 37 | 38 | When the container runs, it creates a superuser with a random password. You 39 | can set the username and password for the superuser by setting the container's 40 | environment variables. This lets you discover the username and password of the 41 | superuser from within a linked container or from the output of `docker inspect 42 | mariadb`. 43 | 44 | ``` shell 45 | $ mkdir -p /tmp/mariadb 46 | $ docker run -d -name="mariadb" \ 47 | -p 127.0.0.1:3306:3306 \ 48 | -v /tmp/mariadb:/data \ 49 | -e USER="super" \ 50 | -e PASS="$(pwgen -s -1 16)" \ 51 | paintedfox/mariadb 52 | ``` 53 | 54 | Alternately, you can run the following if you have *make* installed... 55 | 56 | ``` shell 57 | $ make run 58 | ``` 59 | 60 | You can also specify a custom port to bind to on the host, a custom data 61 | directory, and the superuser username and password on the host like so: 62 | 63 | ``` shell 64 | $ sudo mkdir -p /srv/docker/mariadb 65 | $ make run PORT=127.0.0.1:3306 \ 66 | DATA_DIR=/srv/docker/mariadb \ 67 | USER=super \ 68 | PASS=$(pwgen -s -1 16) 69 | ``` 70 | 71 | ## Connecting to the Database 72 | 73 | To connect to the MariaDB server, you will need to make sure you have a client. 74 | You can install the `mysql-client` on your host machine by running the 75 | following (Ubuntu 12.04LTS): 76 | 77 | ``` shell 78 | $ sudo apt-get install mysql-client 79 | ``` 80 | 81 | As part of the startup for MariaDB, the container will generate a random 82 | password for the superuser. To view the login in run `docker logs 83 | ` like so: 84 | 85 | ``` shell 86 | $ docker logs mariadb 87 | MARIADB_USER=super 88 | MARIADB_PASS=FzNQiroBkTHLX7y4 89 | MARIADB_DATA_DIR=/data 90 | Starting MariaDB... 91 | 140103 20:33:49 mysqld_safe Logging to '/data/mysql.log'. 92 | 140103 20:33:49 mysqld_safe Starting mysqld daemon with databases from /data 93 | ``` 94 | 95 | Then you can connect to the MariaDB server from the host with the following 96 | command: 97 | 98 | ``` shell 99 | $ mysql -u super --password=FzNQiroBkTHLX7y4 --protocol=tcp 100 | ``` 101 | 102 | ## Linking with the Database Container 103 | 104 | You can link a container to the database container. You may want to do this to 105 | keep web application processes that need to connect to the database in 106 | a separate container. 107 | 108 | To demonstrate this, we can spin up a new container like so: 109 | 110 | ``` shell 111 | $ docker run -t -i -link mariadb:db ubuntu bash 112 | ``` 113 | 114 | This assumes you're already running the database container with the name 115 | *mariadb*. The `-link mariadb:db` will give the linked container the alias 116 | *db* inside of the new container. 117 | 118 | From the new container you can connect to the database by running the following 119 | commands: 120 | 121 | ``` shell 122 | $ apt-get install -y mysql-client 123 | $ mysql -u "$DB_ENV_USER" --password="$DB_ENV_PASS" -h "$DB_PORT_3306_TCP_ADDR" -P "$DB_PORT_3306_TCP_PORT" 124 | ``` 125 | 126 | If you ran the *mariadb* container with the flags `-e USER=` and `-e 127 | PASS=`, then the linked container should have these variables available 128 | in its environment. Since we aliased the database container with the name 129 | *db*, the environment variables from the database container are copied into the 130 | linked container with the prefix `DB_ENV_`. 131 | -------------------------------------------------------------------------------- /scripts/first_run.sh: -------------------------------------------------------------------------------- 1 | USER=${USER:-super} 2 | PASS=${PASS:-$(pwgen -s -1 16)} 3 | 4 | pre_start_action() { 5 | # Echo out info to later obtain by running `docker logs container_name` 6 | echo "MARIADB_USER=$USER" 7 | echo "MARIADB_PASS=$PASS" 8 | echo "MARIADB_DATA_DIR=$DATA_DIR" 9 | 10 | # test if DATA_DIR has content 11 | if [[ ! "$(ls -A $DATA_DIR)" ]]; then 12 | echo "Initializing MariaDB at $DATA_DIR" 13 | # Copy the data that we generated within the container to the empty DATA_DIR. 14 | cp -R /var/lib/mysql/* $DATA_DIR 15 | fi 16 | 17 | # Ensure mysql owns the DATA_DIR 18 | chown -R mysql $DATA_DIR 19 | chown root $DATA_DIR/debian*.flag 20 | } 21 | 22 | post_start_action() { 23 | # The password for 'debian-sys-maint'@'localhost' is auto generated. 24 | # The database inside of DATA_DIR may not have been generated with this password. 25 | # So, we need to set this for our database to be portable. 26 | DB_MAINT_PASS=$(cat /etc/mysql/debian.cnf | grep -m 1 "password\s*=\s*"| sed 's/^password\s*=\s*//') 27 | mysql -u root -e \ 28 | "GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '$DB_MAINT_PASS';" 29 | 30 | # Create the superuser. 31 | mysql -u root <<-EOF 32 | DELETE FROM mysql.user WHERE user = '$USER'; 33 | FLUSH PRIVILEGES; 34 | CREATE USER '$USER'@'localhost' IDENTIFIED BY '$PASS'; 35 | GRANT ALL PRIVILEGES ON *.* TO '$USER'@'localhost' WITH GRANT OPTION; 36 | CREATE USER '$USER'@'%' IDENTIFIED BY '$PASS'; 37 | GRANT ALL PRIVILEGES ON *.* TO '$USER'@'%' WITH GRANT OPTION; 38 | EOF 39 | 40 | rm /firstrun 41 | } 42 | -------------------------------------------------------------------------------- /scripts/normal_run.sh: -------------------------------------------------------------------------------- 1 | pre_start_action() { 2 | # Cleanup previous sockets 3 | rm -f /run/mysqld/mysqld.sock 4 | } 5 | 6 | post_start_action() { 7 | : # No-op 8 | } 9 | -------------------------------------------------------------------------------- /scripts/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Starts up MariaDB within the container. 3 | 4 | # Stop on error 5 | set -e 6 | 7 | DATA_DIR=/data 8 | 9 | if [[ -e /firstrun ]]; then 10 | source /scripts/first_run.sh 11 | else 12 | source /scripts/normal_run.sh 13 | fi 14 | 15 | wait_for_mysql_and_run_post_start_action() { 16 | # Wait for mysql to finish starting up first. 17 | while [[ ! -e /run/mysqld/mysqld.sock ]] ; do 18 | inotifywait -q -e create /run/mysqld/ >> /dev/null 19 | done 20 | 21 | post_start_action 22 | } 23 | 24 | pre_start_action 25 | 26 | wait_for_mysql_and_run_post_start_action & 27 | 28 | # Start MariaDB 29 | echo "Starting MariaDB..." 30 | exec /usr/bin/mysqld_safe 31 | --------------------------------------------------------------------------------