Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

Create a Custom MariaDB Container with Raspberry PI and Docker

Raspberry PI Docker custom mariadb logo_2

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 4 model B image

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

Step-by-Step Procedure

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)


nano Dockerfile

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 = #/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 “=”) 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).


nano mySqlScript.sql

Copy and paste following content:

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

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.


nano docker-compose.yml

Copy and paste following content:

#My MariaDB
version: '3'
   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.

Execute Docker-Compose

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
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
mymariadb_database latest 34302c7bc147 About a minute ago 535MB
debian latest 808b13323d0b 3 weeks ago 92.8MB


Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon Micro SD box

How useful was this post?

Click on a star to rate it anonymously!

Average rating 5 / 5. Vote count: 1

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?


  1. Hi

    Thank you for your guidance…..
    Nice work !

    But your solution are missing the last step for been useful to “ordinary” people.

    Your solution provides a UNIX socket.

    But “ordinary” people are using TCP port 3306 and PhpMyadmin.

    Can you tell the changes ? Especially WHERE to correct…


    I am working on a larger site in order to create documentation on a 16 cores RaspBerry Pi hardware, software, Docker and WordPress.. -> Supercomputing

    1. Inside docker environment, this MariaDB container exposes its service with the build command:
      RUN sed -i ‘s/bind-address/bind-address = #/i’ /etc/mysql/mariadb.conf.d/50-server.cnf
      (This build command adds the binding to external connections with “bind-address =” config)

      And sql script command:

      grant all privileges on *.* to myMariadbUser@’%’;
      (this grants remote access to “myMariadbUser”)

      If you want to connect database container from outside docker environment, you need to add “EXPOSE 3306/tcp” to Dockerfile and run container with “-p 3306:3306” option in docker run command to map ports

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy