Create a Custom MariaDB Container with Raspberry PI and Docker (32-bit)

3.5
(4)

Last Updated on 9th December 2022 by peppe8o

Raspberry PI has an ARM architecture. While newer boards have 64-bit ARM CPU, some users may need support for old official Raspberry PI OS versions still running at 32-bit and many Docker official images are unable to run directly in your PI. One of the most common needs for my projects regards using containerized MariaDB database.

MariaDB is a fork of MySQL. It can be used with the same MySQL clients and applications and uses the same SQL syntax. MariaDB uses by default 3306 port.

In this article I’ll show you how to create your custom container with MariaDB in your Raspberry PI with Docker and docker-compose, starting from the Debian base image. Please note that the 64-bit Raspberry PI OS can use directly the official MariaDB docker image.

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Raspberry PI 4 model B image

I’ll be using a Raspberry PI 3 model A+, but this guide applies also to newer Raspberry PI boards.

Check hardware prices with the following links:

Amazon raspberry pi boards box
Amazon Micro SD box

Step-by-Step Procedure

We’ll use docker-compose to create a MariaDB container. This way, you can just reuse my YAML file with additional services composing your custom stack. Or you can leave it all as-is, creating a simple MariaDB instance.

docker-compose.yml calls a build command to create our MariaDB container from the Debian base image. With this image, MariaDB is installed via apt and then it is setup for external access outside the container (add port mapping for external exposing or include containers on the same docker network).

Prepare Operating System with Raspberry PI OS Lite

We need to start from an up to date operating system base (please refer to install Raspberry PI OS Lite guide), including Docker and docker-compose setup (please refer to install Docker in your Raspberry PI guide both for Docker and docker-compose).

From the terminal, update – if not still done – your OS:

sudo apt update -y && sudo apt upgrade -y

Create Configuration Files

Create a directory (docker uses by default your directory name for stack naming) and enter it:

mkdir myMariaDB
cd myMariaDB

This directory will include the following files we are going to create:

  • Dockerfile (used to build MariaDB container)
  • mySqlScript.sql (used for first boot SQL statements)
  • docker-compose.yml (used to create or remove MariaDB stack)

Dockerfile:

nano Dockerfile

Copy and paste the following content into your Dockerfile:

FROM debian
RUN apt update -y && apt install mariadb-server -y
RUN sed -i 's/bind-address/bind-address = 0.0.0.0 #/i' /etc/mysql/mariadb.conf.d/50-server.cnf
RUN mkdir /mysql_scripts
COPY mySqlScript.sql /mysql_scripts/
RUN /etc/init.d/mariadb start && mysql -uroot --password="" -e "source /mysql_scripts/mySqlScript.sql"
CMD /etc/init.d/mariadb start && tail -f /dev/null

This dockerfile is used by the build command to create our custom image.

“FROM debian” indicates that we are going to start from the Debian base image. “latest” tag will be added by default from docker and it will be downloaded from the Docker repository if not still present in your docker installation.

“RUN apt update -y && apt install mariadb-server -y” will execute inside our container and update and install MariaDB from the Debian repository.

“RUN sed -i ‘s/bind-address….” will enable external access to your container. It edits MariaDB bind-address in server configs from binding only localhost to binding external access. You can also restrict access by using a more precise network to allow. Please take care of using the correct number of spaces (12 between “bind-address” and “= 127.0.0.1”) because the sed instruction executes an “exact match” find and replace operation.

“RUN mkdir /mysql_scripts” creates a folder where to store our SQL scripts, while “COPY mySqlScript.sql /mysql_scripts/” copies our custom SQL script (described later) from the host to the container.

“RUN /etc/init.d/mariadb start && mysql -uroot –password=”” -e “source /mysql_scripts/mySqlScript.sql” “starts MariaDB service and executes copied script logging-in as root with default MariaDB root password (empty). It is a good security practice after our MariaDB container will be ready, to change the root password.

Last row is “CMD /etc/init.d/mariadb start && tail -f /dev/null”. You could ask why repeating MariaDB starts and why using a useless “tail -f /dev/null”. There is a very simple reason: containers are designed to be as ephemeral as possible. So, every docker container stays up if its internal main process is active and running. With the docker build, the main process for the custom container becomes the last CMD command. So you need to give something to do to your container to be kept alive from docker. Otherwise, you will experience a container going continuously down with exit code 0 (no error).

mySqlScript.sql

nano mySqlScript.sql

Copy and paste the following content:

create user myMariadbUser@'%' identified by 'myMariadbPassword';
grant all privileges on *.* to myMariadbUser@'%';
flush privileges;
quit

Change myMariadbUser and myMariadbPassword with your favourite ones and reduce privileges according to your needs. You can include in this file other statements (like create a database or create table) to customize and automate your database initialization.

Rows included in my example simply create a user with a password and give it full remote control.

docker-compose.yml

nano docker-compose.yml

Copy and paste the following content:

#My MariaDB
version: '3'
services:
  database:
   build: .
   restart: always

This is the simplest YAML file that creates a service.

Customize it by adding more options like, for example, port mapping (to expose service externally), volumes mapping (to save persistent copies of your data/config folders) and links (to simplify containers connection).

Dot near build statement tells docker that it will find Dockerfile inside the same directory where it is launched.

Execute Docker-Compose

With all the work prepared in previous files, container creation becomes as simple as launching from the same directory where files are stored following command:

docker-compose up -d

You will see docker downloading the Debian image (if not available locally), updating aptitude repositories, and installing and configuring your MariaDB container.

Note for “At least one invalid signature was encountered.” error

After some release upgrades, a few people encountered an error on “docker-compose up -d” command. This error reports something similar to the following output:

Step 2/7 : RUN apt-get update -y && apt-get install mariadb-server -y
—> Running in 432c6c634d35
Get:1 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
Get:2 http://deb.debian.org/debian bullseye InRelease [113 kB]
Err:1 http://security.debian.org/debian-security bullseye-security InRelease
At least one invalid signature was encountered.
Err:2 http://deb.debian.org/debian bullseye InRelease
At least one invalid signature was encountered.
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [36.8 kB]
Err:3 http://deb.debian.org/debian bullseye-updates InRelease
At least one invalid signature was encountered.

With the container not running at all.

The root cause of the problem seems to be in libseccomp version installed by default on Raspberry PI OS. The newer version of libseccomp fixes the problem, but it is not yet available in Debian’s stable repository (from apt) and you need to install it manually. From the terminal, download the deb package:

wget http://ftp.de.debian.org/debian/pool/main/libs/libseccomp/libseccomp2_2.5.4-1+b2_armhf.deb

Then install the package:

sudo dpkg -i libseccomp2_2.5.4-1+b2_armhf.deb

This will solve the “At least one invalid signature was encountered” error. Then you can go on as for this tutorial with:

docker-compose up -d

Check the MariaDB Container Is Running

At the process end, you will find your service running with the simple docker ps command:

pi@raspberrypi:~/myMariaDB $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be53f72f5974 mymariadb_database "/bin/sh -c '/etc/in…" 29 seconds ago Up 26 seconds mymariadb_database_1

You will also find your image ready to use (with a previously downloaded Debian one):

pi@raspberrypi:~/myMariaDB $ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mymariadb_database latest 34302c7bc147 About a minute ago 535MB
debian latest 808b13323d0b 3 weeks ago 92.8MB

What’s Next

If you want to discover many other projects for your Raspberry PI, you can take a look at peppe8o Raspberry PI tutorials.

Enjoy!

If you are looking for a job, check -> Jooble

How useful was this post?

Click on a star to rate it anonymously!

Average rating 3.5 / 5. Vote count: 4

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?