Introduction
Docker has revolutionized application development and deployment with its light-weight containerization approach. As per Statista, Docker adoption has risen from 35% in 2017 to 49% among organizations in 2022.
This growth is driven by benefits like:
- Standardization – Docker guarantees app portability and consistency across environments
- Speed – Containers provide faster deployment and scaling over VMs
- Agility – Development teams can build, update and release new versions quicker
- Collaboration – Common runtime facilitates sharing pre-built components
Therefore, its imperative for technology teams to containerize traditional apps like databases. This enables migrating them to modern infra while keeping data intact.
MariaDB is the ideal open-source replacement for Oracle MySQL for next-gen Cloud-native apps. As per DB-Engines ranking, MariaDB now holds 3.64% database market share while MySQL has dropped to 11.26% in 2024.
This guide covers multiple hands-on examples of running MariaDB on Docker. We have referenced official documentation, reputed technology sites and our development experience for providing clear instructions. Follow along to containerize your MariaDB workloads.
Docker and MariaDB Recap
Before jumping into MariaDB deployment specifics, its useful understanding the technologies involved:
Docker – Seldom would one find a technology that has so profoundly transformed software engineering as containers in recent years. Docker (now part of Mirantis) has become synonymous with containerization just like Kubernetes with orchestration.
As per 2022 StackOverflow survey, Docker usage grew 2x to reach 68.4% among professional developers.

While early versions had flaws, modern Docker releases provide excellent security, resource management and tooling like:
- Namespaces â Process and network isolation between containers
- Control groups â Limit CPU, memory for containers
- UnionFS â Layered filesystem enabling Docker image layers
- Container networking â Cross-node connectivity out of the box
- Compose and Swarm – Multi-container and cluster management
So for both greenfield and migrating legacy software, its prudent embracing Docker containers.
MariaDB – Released in 2009, MariaDB aimed to maintain MySQL‘s technical strengths while adding new storage engines, features and performance optimizations.
As evident below, MariaDB usage has grown 3x over 5 years as per DB-Engines tracking:

Notable highlights of MariaDB are:
- Supports wide range of programming languages â Python, Java, C/C++, Go, NodeJS
- Distributed with multiple storage engines like XtraDB, TokuDB, Spider, MyRocks
- Advanced replication, sharding and clustering capabilities
- Backward-compatible to leverage existing MySQL tooling and admin skills
- Rich Kubernetes integrations through operators
So both technologies offer modular, versatile solutions for modern application stacks. Now let us focus on running them together using Docker.
Configuring MariaDB with Docker
Docker simplifies deploying MariaDB for development, testing and production purposes without environment hurdles. Like typical databases, the paths involved are:
- Installing & Running MariaDB Docker container
- Initializing Databases, Users, Tables externally
- Connecting clients like apps to interact with DB
- Persisting volumes for durable, portable data
- Clustering for high availability
- Backup & Recovery processes
We will next illustrate each of these critical workflows with examples. For ease of demonstration, we utilize Linux command line all through:
1. Install and Run MariaDB Container
Pull the latest official MariaDB image from Docker Hub registry:
docker pull mariadb
This downloads the Ubuntu 20.04 based image to our host. You can also choose specific versions like mariadb:10.6 based on compatibility needs.
Now run the image in detached background mode:
docker run -d --name mariadb
-e MYSQL_ROOT_PASSWORD=Pass@word123
-e MYSQL_DATABASE=mydb
-e MYSQL_USER=myuser
-e MYSQL_PASSWORD=Pass@word123
-p 3306:3306
mariadb
We pass relevant environment variables here for credentials, default database, exposed port etc. This creates a ready to use MariaDB server.
2. Initialize MariaDB Entities
The databases are ready for connections. We can initialize entities like databases, users and tables using preferred MariaDB clients.
Access the shell inside the container:
docker exec -it mariadb bash
mysql -u root -p
This allows running SQL commands directly:
-- Create tables
USE mydb;
CREATE TABLE projects (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
-- Insert rows
INSERT INTO projects (name) VALUES (‘Project One‘);
INSERT INTO projects (name) VALUES (‘Project Two‘);
However for UI based access, utilize admin tools like phpMyAdmin, Adminer or MySQL Workbench installed on the host / different container.
They can remotely connect using mariadb hostname and credentials set earlier.
3. Connect Application Code
Modern applications employ language frameworks with integrated libraries to interact with backend databases.
For instance, Python‘s Flask uses pymysql driver. So our sample app code would resemble:
import pymysql
from flask import Flask
# App config
app = Flask(__name__)
# DB Config
db_host = ‘mariadb‘
db_user = ‘myuser‘
db_pass = ‘Pass@word123‘
# Fetch projects
@app.route(‘/‘)
def get_projects():
conn = pymysql.connect(host=db_host, user=db_user, password=db_pass, db=‘mydb‘)
cursor = conn.cursor()
sql = "SELECT * FROM projects"
cursor.execute(sql)
rows = cursor.fetchall()
return str(rows) # Return DB results
Here mariadb refers to the linked Docker service hostname allowing apps to offload data processing.
4. Persist MariaDB Storage
Like all containers, any data written into a MariaDB container gets erased after restart or removal.
Hence its vital persisting the /var/lib/mysql path storing database files, logs onto a Docker volume. This retains durability through container recreation.
docker run -d --name mariadb
-v dbdata:/var/lib/mysql
mariadb
The host directory dbdata now mirrors all runtime data changes externally. This holds true even for scaled up clusters covered next.
5. Cluster Containers for High Availability
For production grade availability alongside scalability, its prudent running MariaDB as a multi-node Docker cluster.

This allows replication across nodes for eliminating single point failures. Docker Swarm streamlines such clustered deployments.
Here is an overview manifest for creating a 3 node setup using Swarm:
services:
dbnode1:
image: mariadb
volumes:
- node1:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: Pass@word123
CLUSTER_NAME: mariadb_cluster
WSREP_CLUSTER_ADDRESS: gcomm://mariadb,dbnode2,dbnode3
WSREP_NODE_ADDRESS: dbnode1
WSREP_NODE_NAME: dbnode1
ports:
- 3307:3306
dbnode2:
extends:
service: dbnode1
environment:
WSREP_NODE_ADDRESS: dbnode2
WSREP_NODE_NAME: dbnode2
dbnode3:
extends:
service: dbnode1
environment:
WSREP_NODE_ADDRESS: dbnode3
WSREP_NODE_NAME: dbnode3
volumes:
node1:
node2:
node3:
We utilize Docker image extensions and shared volume mounts to minimize repetition across the nodes. Such clustered MariaDB topology offers enhanced throughput, replication and resilience.
6. Backup and Restore
Since container data can‘t match durable disks, database backups are essential. This allows restoring past state in case of failures.
Hot backups refer to techniques where MariaDB remains online during dumps without blocking queries and transactions.
Connect to the container shell and save compressed SQL dump onto persisted storage:
docker exec mariadb sh -c ‘mysqldump -uroot -pPass@word123 --single-transaction --all-databases | gzip > /mnt/dbbackup/alldb.sql.gz‘
The sequence --single-transaction --all-databases enables a consistent snapshot of everything through a single transaction.
For managing incremental changes, utilize mysqldump‘s --databases, --tables arguments to limit scope. Plus explore built-in replication and tools like Percona XtraBackup.
During disaster recovery, extract and redirect the SQL dump again to overwrite everything:
zcat /mnt/dbbackup/alldb.sql.gz | docker exec -i mariadb sh -c ‘mysql -uroot -pPass@word123‘
So with appropriate backup mechanism you can prevent total data loss in ephemeral containers.
Alternative Orchestration Approaches
While Docker platform provides excellent local development and containerization capabilities, you need orchestration frameworks like Kubernetes for managing full-fledged application infrastructure.
Specifically for databases, options include:
1. Direct Docker setup – As followed so far, you can customize and link containers directly via Docker CLI or Compose. This works for simpler deployments.
2. StatefulSets on Kubernetes – For production grade clustering and high availability, StatefulSets offer stable network identities and persistent volumes.
3. Database PaaS Services – RDBMS heavyweights like AWS RDS, Azure SQL take over administration needs and offer turnkey DB instances.
4. DBaaS Operators – Emerging pattern for using custom resources fitting Kubernetes practices. Eg. Oracle MySQL operator.
Based on where your databases fit within the overall architecture, pick suitable platform both technology and operations wise.
Conclusion
This exhaustive guide should equip you with ample hands-on knowledge around containerizing your MariaDB workloads. We covered:
- Docker and MariaDB technology fundamentals
- Walkthrough from installations to backups
- Clustering for high availability
- Persistent storage and administrative best practices
You should now have enough contextual reference to architect and run MariaDB backed applications effectively on Docker plus added resources for alternatives like Kubernetes based orchestration.
Combined with increasing community contributions and rich tooling support, MariaDB on Docker provides high performance, portable and resilient data runtime for Cloud-native development stacks.


