Dockerized MySQL Alpine

Gets the latest version of Alpine with the latest MySQL.
In favor for smallest image size.

Use existing database or a clean slate

Pair your database through Volume

# existing one
-v /my/existing/db:/var/lib/mysql

# start clean slate with empty directory
-v /my/empty/db:/var/lib/mysql

Use custom config

Currently default config is this

file: my.cnf

[mysqld]
expire-logs-days = 3  
user = root  
datadir = /var/lib/mysql  
port = 3306  
log-bin = /var/lib/mysql/mysql-bin  
socket = /var/lib/mysql/mysql.sock  
!includedir /etc/mysql/conf.d/

Pair your config through Volume

-v /my/custom/configs:/etc/mysql/conf.d

Quick run command

docker run --rm -v /tmp/empty/database:/var/lib/mysql -p 3306:3306 harianto/mysql  

Example

Create empty database directory. Run the image as "mysqlsrv".

# create empty directory
mkdir -p /tmp/db  
# assign directory and run image (remove image upon exit)
docker run --rm --name "mysqlsrv" -v /tmp/db:/var/lib/mysql harianto/mysql

# link mysqlsrv as db and run Alpine OS shell
docker run --rm --link mysqlsrv:db -it alpine /bin/sh  

--rm : Automatically remove the container when it exits
--name string : Assign a name to the container
-v, --volume value : Bind mount a volume (default [])
-i, --interactive : Keep STDIN open even if not attached
-t, --tty : Allocate a pseudo-TTY
--link value : Add link to another container (default [])

Now in the Alpine Shell

MySQL Client hasn't been installed yet, you can do it like:

# install mysql-client
apk add --update mysql-client

# connect mysql assigned 'db' host
mysql -h db  

Now in MySQL Shell

MariaDB [(none)]> show databases;  
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.02 sec)  

Let’s create database

MariaDB [(none)]> create database example;  
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use example;  
Database changed  
MariaDB [example]> create table `users` (`id` bigint(20) not null auto_increment, `name` char(30) not null, `age` smallint(6) not null, primary key(`id`));  
Query OK, 0 rows affected (0.01 sec)

MariaDB [example]> insert into users (id, name, age) value (1,'first',42);  
Query OK, 1 row affected (0.01 sec)

MariaDB [example]> select * from users;  
+----+-------+-----+
| id | name  | age |
+----+-------+-----+
|  1 | first |  42 |
+----+-------+-----+
1 row in set (0.00 sec)

MariaDB [example]> \q  
Bye

In your host, check the folder: /tmp/db See mysql data being populated

comments powered by Disqus