Raspberry PI has an ARM architecture. While newer boards has 64-bit ARM CPU, official Raspberry PI OS is still running at 32-bit and many Docker official images are unable to run directly in your PI. One of most common need for my projects regards using containerized MariaDB database.
MariaDB is a fork of MySQL. It can be used with same MySQL clients and applications and uses same sql sintax. 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 Debian base image.
What We Need
As usual, I suggest adding from now to your favorite e-commerce shopping cart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from the shopping cart. So, hardware will be only:
- Raspberry PI (including proper power supply or using a smartphone micro usb charger with at least 3A)
- micro SD card (at least 16 GB, at least class 10)
I’ll be using a Raspberry PI 3 model A+, but this guide applies also to newer Raspberry PI boards.
We’ll use docker-compose to create MariaDB container. This way, you can just reuse my yaml file with addictional services composing your custom stack. Or you can leave all as-is, creating a simple MariaDB instance.
docker-compose.yml calls a build command to create our MariaDB container from Debian base image. With this image, mariadb is installed via apt and then it is setup for external access outside container (add port mapping for external exposing or include containers on 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 terminal, update – if not still done – your OS:
sudo apt update sudo apt upgrade
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 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)
Copy and paste following content in 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/mysql start && mysql -uroot --password="" -e "source /mysql_scripts/mySqlScript.sql" CMD /etc/init.d/mysql start && tail -f /dev/null
This dockerfile is used by build command to create our custom image.
“FROM debian” indicates that we are going to start from Debian base image. “latest” tag will be added by default from docker and it will be downloaded from 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 of mariadb from 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 on using the correct number of spaces (12 between “bind-address” and “= 127.0.0.1”) because 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 host to container.
“RUN /etc/init.d/mysql 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 root password.
Last row is “CMD /etc/init.d/mysql start && tail -f /dev/null”. You could ask why repeating mysql start and why using a useless “tail -f /dev/null”. There is a very simple reason: containers are designed to be as ephimeral as possible. So, every docker container stays up if its internal main process is active and running. With docker build, main process for 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).
Copy and paste 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 database or create table) to customize and authomatize your database inizialization.
Rows included in my example simply create a user with password and gives it full remote control.
Copy and paste following content:
#My MariaDB version: '3' services: database: build: . restart: always
This is the simplest yaml file which create a service.
Customize it 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.
With all the work prepared in previous files, container creation becames as simple as launching from same directory where files are stored following command:
docker-compose up -d
You will see docker downloading Debian image (if not available locally), updating aptitude repositories, installing and configuring you MariaDB container.
At 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 ready to use image (with 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
Check hardware prices with following links:
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?