Public | Automated Build

Last pushed: a year ago
Short Description
Ergomentum MariaDB
Full Description


Ergomentum MariaDB

Provides a MariaDB image based on the Ergomentum CentOS image.

MariaDB can be considered as a drop-in replacement for MySQL.

Since a database deals inevitably with data persistence whereas container are feasible stateless we have to manage this. The recommended approach for this are named
volumes
.

Because "multiple containers writing to a single shared volume can cause data corruption"
<sup>[1]</sup> each data container should not be used by more than one database container. Using more
than one databases you have to made the trade-off between one database container with multiple databases vs. multiple
database containers with one database each. The former needs lowest resources but violates the tightly coupling of the
containers. The later ensures the tightly coupling but multiplies the resource requirements. This image supports both
scenarios.

Another decision is how to initialize your databases. You must at least provide the MySQL root user password as an
environment variable starting a container from this image the first time (assuming an empty
directory /var/lib/mysql for the database files). You may create a MySQL non root user at the same time and initialize
a database with additional environment variables. If you use your database container for more than one database you have
to start your database container one time for each database. Finally you may want to restart your database container one
more time skipping the MySQL root password in the environment variables for security reasons.

Additionally you can place any number of SQL files in the volume /docker-entrypoint-initdb.d to initialize your
database. Each filename must end with .sql Each statement must be on a single line. Or you can connect with the MySQL root user to
the running database container to initialize additional databases.

To backup your database you have to options:

  • Physical backup
  • Logical backup

For a physical backup you have top stop your database service to ensure a consistent state for all database files. Then
you can copy all files from /var/lib/mysql.

For a logical backup you leave your database service running and run commands below.

Notice: There is no chance to do a hot backup like Percona XtraBackup running only one process per container.

Volumes

<dl>
<dt><code>/docker-entrypoint-initdb.d</code></dt>
<dd>Files for database initialization (see above).</dd>
</dl>

Environment Variables

Warning: Keep in mind "that all environment variables originating from Docker within a container are made available
to any container that links to it. This could have serious security implications if sensitive data is stored in them."
<sup>[2]</sup>
As a consequence it is strictly recommended to prefer a user defined network to legacy container links for maximum isolation.

<dl>
<dt><code>MYSQL_ROOT_PASSWORD</code></dt>
<dd>Mandatory to start the database container first time with an empty directory <code>/var/lib/mysql</code>.</dd>

<dt><code>MYSQL_DATABASE</code></dt>
<dd>Optional database name used to create a new database if not exists. If not set <code>MYSQL_USER</code> will be
used as default.</dd>

<dt><code>MYSQL_USER</code></dt>
<dd>Optional database user used to create a new database.</dd>

<dt><code>MYSQL_PASSWORD</code></dt>
<dd>Mandatory if <code>MYSQL_USER</code> is set.</dd>
</dl>

Exposed Ports

<dl>
<dt><code>3306</code></dt>
<dd>The MySQL default port.</dd>
</dl>

Usage

Is is not necessary to use this image as base image. Simply create a container from it.

Create a named volume to store database files

It's recommended to store store database files in a named volume.

docker \
  volume \
  create \
  --name mydbdata

Start daemonized MySQL server with public port binding creating no new databases

docker \
  volume \
  create \
  --name mydbdata
docker \
  run \
  -d \
  -e MYSQL_ROOT_PASSWORD=secret \
  -p 3306:3306 \
  -v mydbdata:/var/lib/mysql \
  ergomentum/mariadb

This allows you to connect with any mysql client to initialize the database:

<pre>
mysql -h<var>ip_of_your_docker_host</var> -uroot -psecret
</pre>

Start daemonized MySQL server with custom network to be used from another container

docker \
  volume \
  create \
  --name mydbdata
docker \
  network \
  create \
  --driver bridge \
  mynetwork
docker \
  run \
  -d \
  -e MYSQL_ROOT_PASSWORD=secret \
  -p 3306:3306 \
  -v mydbdata:/var/lib/mysql \
  --net=mynetwork \
  --name=mydbservice \
  ergomentum/mariadb
docker \
  run \
  --rm \
  -ti \
  --net=mynetwork \
  ergomentum/mariadb

Notice: There is no container linking used. There are no environment
variable from the database container exposed. You must know the credentials to connect to the database container.

Get inside a running database container

docker \
  volume \
  create \
  --name mydbdata
docker \
  run \
  -d \
  -e MYSQL_ROOT_PASSWORD=secret \
  -p 3306:3306 \
  -v mydbdata:/var/lib/mysql \
  --name=mydbservice \
  ergomentum/mariadb
docker \
  exec \
  -it \
  mydbservice \
  bash

View Logs

docker \
  volume \
  create \
  --name mydbdata
docker \
  run \
  -d \
  -e MYSQL_ROOT_PASSWORD=secret \
  -p 3306:3306 \
  -v mydbdata:/var/lib/mysql \
  --name=mydbservice \
  ergomentum/mariadb
docker \
  logs \
  mydbservice

Backup

docker \
  volume \
  create \
  --name mydbdata
docker \
  network \
  create \
  --driver bridge \
  mynetwork
docker \
  run \
  -d \
  -e MYSQL_ROOT_PASSWORD=secret \
  -p 3306:3306 \
  -v mydbdata:/var/lib/mysql \
  --net=mynetwork \
  --name=mydbservice \
  ergomentum/mariadb
docker \
  run \
  --rm \
  -it \
  --net=mynetwork \
  ergomentum/mariadb
mysqldump -hmydbservice -uroot -psecret --single-transaction --all-databases \
> "mydbservice_$(date -u +'%y-%m-%d_%H-%M-%S').sql"

Restore

docker \
  stop \
  mydbservice
docker \
  run \
  -d \
  -e MYSQL_ROOT_PASSWORD=secret \
  --net=mynetwork \
  --name=mydbservice2 \
  ergomentum/mariadb
docker \
  run \
  --rm \
  -i \
  --net=mynetwork \
  ergomentum/mariadb
mysql -hmydbservice2 -uroot -psecret < *.sql

Contributing

To contribute a feature or a bugfix please open a pull request on
GitHub.

See CONTRIBUTING for details.

License

See the LICENSE file for license
rights and limitations (Apache License, Version 2.0).

References

<a id="sharedvolumes">1</a>.
https://docs.docker.com/engine/userguide/containers/dockervolumes/#important-tips-on-using-shared-volumes

<a id="dockerlinks">2</a>.
https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/

Docker Pull Command
Owner
ergomentum
Source Repository

Comments (0)