Protect Your Data: Run MariaDB using Docker

Oct 25, 2022

MariaDB is a completely free database server that provides access to data using a structure-based query language (SQL). It comes with security tools such as passwords, role-based access controland more that will protect your information.

To deploy MariaDB and grant data access to clients, you must host the database server. It is possible to choose an online service provider, or managed or shared hosting services.

This article will teach you how to use MariaDB using the Docker container. You can configure and run the database server, and connect to a WordPress site.

What Is Docker?

Usually, containers use your operating system as a host. This means the host machine's kernel is able to access hardware and software such as CPUs, memory as well as the file system. As a result, this does not require traditional virtualization, like virtual machines. There are several advantages of using Docker to host an MariaDB instance.

  • It's got a tiny digital footprint that allows for an efficient use of the system's resources.
  • It's stable, which allows developers to use their apps on tests and in production with just a few adjustments.
  • It is a flexible system to allow sharing of resources.
  • It's flexible -- you can host multiple containers on the same host.

How to deploy MariaDB using Docker

In this part in this section, you'll create containerized environments to run MariaDB by using Docker. You'll learn about the container technology offered by Docker. Docker can be used on all versions of Windows, macOS, and Linux distributions. In order to follow this tutorial, you'll need to be running Windows 10/11, Ubuntu 20.04 or an macOS X machine to follow along.

1. Install Docker

Ubuntu 20.04

The first step is to update Ubuntu's packages list.

sudo apt-get update

Then, allow the access of online repository sites using HTTPS.

sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Installing docker through the terminal.
Then, you can add the Docker's GPG key.
sudo mkdir -p /etc/apt/keyrings
 
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Now, add the Docker repository.

echo deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Then, update Ubuntu's packages to incorporate Docker's repository.

sudo apt-get update

Then, you can finally, install Docker Engine.

sudo apt-get install docker-ce

If you're using a different Linux distribution such as Debian or Fedora and refer to the official instructions to Installing Docker in Linux.

Windows 10/11

Docker is available for Windows 10 or 11 using Windows Subsystem for Linux version 2 (WSL 2) as the backend. Follow these steps to install Docker.

To begin, you must activate first, the Virtual Machine Platform feature on your Windows machine. It allows you to download WSL 2 and install and operate an emulated Linux version on your Windows machine.

Next, install WSL.

Visit the Microsoft Store to install Ubuntu 20.04.

Then you can download Docker Desktop for Windows. Download the file and kickstart installing the program.

After the installation, search "Docker Desktop" from your taskbar and open the application.

( Note:You'll have to install PowerShell for your console in order to use Docker command lines.)

macOS X

Docker can be downloaded on macOS computers through the Apple App Store. There are two installers available that target both Intel as well as Apple chips.

The first step is to download the correct installer from one of these links. After that, double-click the installer to open the download .dmg file.

Then, drag and drop the Docker icon to The Application folder.

Then, launch the Docker app from the Applications folder. Then, follow the steps to finish the setting.

Once the installation process completes When the installation is completed, double-click the Docker icon in your screen's status bar to launch it.

Utilize the default terminal for start Docker commands.

2. Download the MariaDB Image

A Docker image provides a collection of commands and configurations that make the Docker container. It is responsible for installing all the components needed to run an application. You can find the MariaDB official image at the Docker Hub.

To download MariaDB's image on Docker Hub, you'll need to make use of the Docker Pull command:

docker pull mariadb

It is also possible to view a list of downloaded Docker images through the following commands:

docker images

This is the output of the command:

Listing Docker images.
List Docker image listings.

3. Create a Container

Containers are software unit with all the code, dependencies, as well as the tools used by system to operate a program. The image will be download to build an MariaDB container.

docker create mariadb --name mariadb-wp -i -t

This creates a MariaDB container called mariadb-wp. This is because the -i flag permits an interactive session. Additionally, the option -t option creates an untrusted terminal. The official documentation contains information on all available variables.

4. Stop, Run, or Pause the Container, then End the Container

Docker provides developers with the freedom to design environments with a variety of options. In this chapter we'll configure MariaDB's container with environment variables for setting up the particular system parameters for your container.

MariaDB provides a number of variables you can set, like passwords for databases, database names and users of the database. For more information on supported environment variables, refer to Docker's documentation for MariaDB.

docker run -d --name mariadb-wp -p 3306:3306 -v '/path/on/host/:/var/lib/mysql' -e "MARIADB_ROOT_PASSWORD=" -e "MARIADB_DATABASE=wordpress" -e "MARIADB_USER=wordpress" -e "MARIADB_PASSWORD=" mariadb

This command sets MariaDB's root password, the database user and password. It then runs MariaDB on port 3306. You can decide to pause an application running in a container via the command below:

docker stop mariadb-wp

Finally, you can also shut down an application within an environment by calling the following commands:

docker stop the mariadb-wp

5. Connect the Containerized MariaDB to a WordPress Site

In the root directory of the WordPress website, open the wp-config.php file in your editor for code. Find the section of code that determines the variables that are used for the database. Edit the file as illustrated below. Be sure to use the database name, password, as well as the port number in the process of creating MariaDB. MariaDB container.

define('DB_NAME', 'wordpress');
 
 define('DB_USER', 'wordpress');
 
 define('DB_PASSWORD', '');
 
 define('DB_HOST', 'http://localhost:3306');

After you've exported your database, install the database dump inside the container.

docker exec -i mariadb-wp sh -c 'exec mysql -u root -p "$MARIADB_ROOT_PASSWORD" 

The docker exec command lets developers use shell commands in the container. The database was imported into MariaDB by exporting a document in the above command.

6. Create a new post on Your WordPress Site

We will create a sample blog post with the WordPress administrator account in order to test the integration.

Then, sign in to WordPress then click "Posts" >> Add New. Complete the form as shown below, after which click to publish. After creating the post, you can click View Post to see the new posted post.

Adding a new post in WordPress Gutenberg editor.
Making a new entry in the WordPress editor.

And that's all there is to it!

7. MariaDB and Docker with Dev

For a start, download, install, and then launch Dev onto your personal computer. The app gives you three options: create your own WordPress site, import an existing one from , or create a custom site.

Dev's new create new site screen.
The new Dev's Create New Site screen.

Creating a new WordPress website is as simple as filling in a quick form and clicking to create a site.

Dev's new WordPress site creation screen.
Dev's new WordPress page design screen.

You've successfully created a containerized WordPress website using Dev!

Summary

Docker is a developer-friendly tool for containerizing software , which operates databases like MariaDB. Its minimalistic environments help maintain performance of the system but without losing the functionality.

This tutorial taught the steps to install Docker and set up MariaDB as well as connect a WordPress website to your containersized MariaDB database. In addition, you learnt how to utilize Dev to make a containerized WordPress site.

Save time, costs and increase site performance:

  • Help is available immediately 24/7 support from WordPress Hosting experts 24/7.
  • Cloudflare Enterprise integration.
  • Global audience reach with 35 data centers around the world.
  • Optimization with our built-in Application Performance Monitoring.