Protect Your Data: Run MariaDB using Docker

Oct 27, 2022

MariaDB is a free database server that provides access to your data via an underlying structured query language (SQL). MariaDB comes with security features like passwords, role-based access controland more that will protect your information.

To install MariaDB and grant data access to users, you need to create a database server. There is the option of choosing an online service provider or even managed or shared hosting services.

This guide will show you how to utilize MariaDB using it's Docker container. You are able to set up and operate the database server, and connect to the WordPress website.

What's Docker?

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

  • It has a small digital footprint that allows for an efficient use of the resource of the system.
  • It's stable and lets developers use their applications on test and in production with just a few modifications.
  • It's a system that can be adapted that allows the sharing of resources.
  • It's flexible -- you can host multiple containers within the same host.

How do I deploy MariaDB using Docker

In this part in this section, you'll create containers to use MariaDB with the help of Docker. Then, you'll be introduced to the technology for containers offered by Docker. Docker can be used on every version of Windows, macOS, and Linux distributions. For this guide, you'll need to have Windows 10/11, Ubuntu 20.04 and a macOS X machine to follow the steps.

1. Install Docker

Ubuntu 20.04

The first thing to do is update Ubuntu's packages list.

sudo apt-get update

Then, allow the access to online repository websites by 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 connect your 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, download and install Docker Engine.

sudo apt-get install docker-ce

If you're running a different Linux distribution like Debian or Fedora then refer to the official guidelines for Installing Docker in Linux.

Windows 10/11

Docker is compatible with Windows 10 or 11 using Windows Subsystem for Linux version 2 (WSL 2) as the backend. Follow these steps for installing Docker.

In order to begin, you need to enable to activate the Virtual Machine Platform feature on the Windows machine. This feature allows you to install WSL 2 and install and operate the emulated Linux version of the software on your Windows computer.

Following, Install WSL.

Visit the Microsoft Store to download and install Ubuntu 20.04.

After that, you'll be able to download the Docker Desktop to Windows. Start downloading the file and installation of the application.

Following installation, type "Docker Desktop" from your taskbar and open the application.

( Note:You'll need to install PowerShell to your console in order to utilize Docker command line.)

macOS X

Docker is available for download for use on macOS computers through the Apple App Store. Two different installers are accessible which target Intel along with Apple chips.

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

After that, drag and drop the Docker icon to The Applications folder.

After that, open then the Docker app from within the Applications directory. After that, follow the steps to set up the settings.

When the installation process is completed After the installation has been completed Double-click the Docker icon in your display's status bar to start the application.

Utilize the default terminal for the start Docker commands.

2. Get the MariaDB Image

A Docker image is a set of commands and configurations which create it the Docker container. It's the one responsible for installing all necessary components for running an application. The MariaDB official image on the Docker Hub.

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

docker pull mariadb

You can also look up a list of the downloaded Docker images through these commands:

docker images

The output is the command:

Listing Docker images.
List Docker image listings.

3. Make a container

Containers are software components that include the entire code, all dependencies in addition to the software used by the system for operating a program. The image will be download to create a MariaDB container.

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

It creates the MariaDB container dubbed mariadb-wp. This is because the -i flag permits an interactive session. Additionally the option of -t option creates an non-trusted terminal. The official document provides information about all accessible variables.

4. Stop, Run, or stop the Container after which you can close the container.

Docker allows developers to have the flexibility to build environments with a variety of options. In this chapter we'll configure MariaDB's container using the environment variables needed to configure the particular system parameters for your particular container.

MariaDB has a range of variables you can set, like passwords for databases as well as database names, and the users of the database. For more information on available environment variables, go 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, as well as the database user's password and the database. It then runs MariaDB at port 3306. You can decide to pause an application in a container via the command below:

docker stop mariadb-wp

In addition, you are able to close an application in an environment using these commands:

docker stops the mariadb-wp

5. Connect the Containerized MariaDB to an WordPress Site

The root directory on the WordPress site you can open your wp-config.php file in your editor for code. Look for the line of code that determines the variables that are utilized by the database. Edit the file according to the example below. Remember to include your database's name, password, as well as the port number during the process of creating MariaDB. MariaDB container.

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

Once you've exported your database, install the database dump into the container.

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

The docker exec command lets developers run shell commands in the container. The database was transferred to MariaDB through exporting a document using the command above.

6. Write a brand new blog post for Your WordPress Site

Then, we will make a test blog post with an administrator account on the WordPress administrator account in order to evaluate the integration.

After that, log into WordPress and then select "Posts" >> Create New. Complete the form as shown below. Then, you can press for publish. Once you have created the post, you can select "View Post" to view the newly published post.

Adding a new post in WordPress Gutenberg editor.
The process of creating a new entry on The WordPress editor.

And that's all there is to it!

7. MariaDB as well as Docker in conjunction with Dev

For a start, download the application, install it, and launch Dev onto your personal computer. The app gives you three optionsto create your personal WordPress website, transfer an existing site from another , or create a custom website.

Dev's new create new site screen.
The Dev's Create New Screen for the site.

The process of creating a brand new WordPress site is as simple as filling out a simple 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 an containerized WordPress website using Dev!

Summary

Docker is a user-friendly program to containerize software, that manages databases, such as MariaDB. Its minimalistic environments help ensure the efficiency of the system but without loss of functionality.

This video tutorial explained how of installing Docker and to set up MariaDB as well as connect a WordPress website to your containersized MariaDB database. You also learned how to utilize Dev to build a containerized WordPress site.

Reduce time, cost and improve site performance

  • Help is available immediately 24/7 assistance by WordPress Hosting experts 24/7.
  • Cloudflare Enterprise integration.
  • Reaching a global audience with 35 data centers all over the world.
  • Optimization through our integrated Application to monitor performance.

Article was first seen on here