MariaDB Docker Effortless Deployment

Published:1 March 2022 - 8 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

If you plan to install MariaDB on your system, you might want to isolate it from the rest of the system so that you won’t break anything. Granted, you can use a virtual machine, but it would require lots of resources since you’ll have to install an entire system on top of another. So why not deploy a Docker for MariaDB?

If you have limited hardware, not a problem, Docker can help! Docker is a lightweight virtualization solution. And in this tutorial, you’ll learn to deploy MariaDB Docker server installation inside a Docker container with less use of resources.

Read on and never waste your resources again!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

  • A Linux machine – This demo uses Ubuntu 20.04 LTS, but any Linux distributions will work.

Working with MariaDB Docker Image

The best way to start working with MariaDB on your machine is by using the official Docker image for MariaDB. The image contains an installation of the latest stable version of MariaDB, plus some scripts to help you manage the container.

1. Run the apt update command below to ensure your system is up-to-date. This command fetches the latest package lists from Ubuntu’s repositories and installs any available packages.

sudo apt update -y
updating your system
updating your system

2. Next, run the systemctl command below to start the docker service.

sudo systemctl start docker

Starting the Docker service doesn’t provide an output. But if you run docker-related commands while the Docker is not running, you’ll get the following error.

Getting an Error while Running Docker Commands
Getting an Error while Running Docker Commands

3. Execute the docker status command below to ensure that the docker service is running.

 sudo service docker status 

Below, you can see the Docker service is active (running).

Checking the Docker Service
Checking the Docker Service

4. Finally, run the gpasswd command below to add your current user to the docker group. Doing so permits the user to run docker commands.

sudo gpasswd -a "${USER}" docker
Adding your Current User to the docker Group
Adding your Current User to the docker Group

Adding the current user to the docker group saves you from the following error when running a docker command.

Getting Permission Denied Error Docker
Getting Permission Denied Error Docker

5. Run the below docker search command to search for the MariaDB image. This command searches the Docker Hub (official set of repositories) for images that match mariadb and returns the results.

sudo docker search mariadb

Check any image you want to use to install MariaDB. This demo uses the official image named mariadb because it’s well maintained.

The OFFICIAL column shows an OK status if an image is an official one. If the image doesn’t have the OK status, the image is contributed by a community developer. In that case, you will have to contact the developer if you have issues with the image.

searching mariadb images
searching mariadb images

6. Now run the docker pull command to download the image you picked (in step five) from the Docker repository. This command downloads (pull) the selected image from its official repository and saves it on your local machine.

sudo docker pull mariadb

When the command completes, you’ll get the latest stable version of the MariaDB docker image on your server, as shown below.

Downloading MariaDB Image
Downloading MariaDB Image

If you prefer to install previous versions, you can use the tags available for that image. Navigate to Image tag/s columns in dockerhub to view all the versions, then run the docker pull command and specify the exact version, as shown below. sudo docker pull mariadb:10.2

7. Finally, run the docker images command to list all images present on your server. At this point, you should have a valid MariaDB Docker image on your server.

sudo docker images

Below, you can see mariadb is the name of the docker image with a tag as latest.

Listing Docker Images
Listing Docker Images

Creating a MariaDB Container to run a Local MariaDB server

Now that you have the MariaDB image on your server, you can run a container using that image. A container is an isolated, resource-controlled, and portable runtime environment that runs on top of an operating system.

You must first create a new opt/mariadb/backup directory on your server to store the backup file you’ll create in this demo. Backing up your data in your production environment is crucial before making changes if anything goes wrong.

When you change a container’s internal volume, that change gets copied to the mapped host directory. So to keep a backup copy of all container data, you’ll create the volume on the local server.

1. Run the below command to create the opt/mariadb/backup directory. The -p option ensures that the parent directory exists before creating this directory.

If any of the parent directories in the path does not exist, the command will fail.

sudo mkdir -p opt/mariadb/backup
Creating a Backup Directory
Creating a Backup Directory

2. Next, execute the docker run command below to create a Docker container from the mariadb image. This command performs the following:

  • Maps (-v) the default directory to hold the MariaDB data (/var/lib/mysql) on your Docker container on your host directory (/opt/mariadb/backup).
  • Declares the MariaDB environment variable (-e) MYSQL_ROOT_PASSWORD to hold the root password for the MariaDB server. Be sure to replace ata123 with your secure password.
  • Tells Docker to start the container in detached mode (-d), so the container runs in the background. Doing so lets you leave the current session and continue working while this container runs in the background. If not, you’ll get a blank prompt, and you’ll have to kill the running container.
  • Specifies the name of the container (--name mariadbata) to create. You can replace mariadbata with your unique preferred name.
  • Exposes the default MariaDB port 3306 to the host machine (-p 3306:3306) (where the MariaDB service listens to your client connections). In the case of the production server, you’ll map the default port to the production port. But you can replace 3306 with some other production port number.
sudo docker run --name mariadbata -p 3306:3306 -v /opt/mariadb/backup:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=ata123 -d mariadb

At this point, the container is created and running in the background, but you can never be too sure. So you’ll verify the container in the next step.

Creating a MariaDB Docker Container
Creating a MariaDB Docker Container

3. Run the docker ps command to verify if your Docker container is active or not.

sudo docker ps

You can see below that the mariadb docker container has been Up since 7 minutes ago, listening on port 3306 on all host interfaces. You can also see the ID (3a7fa779b559) of the container.

You can use this unique ID to connect to the container, list containers, stop containers, or delete the container.

4. Now, run the docker exec command below to check the version of the MariaDB server inside the mariadbata container.

sudo docker exec -it mariadbata mysql -V

As of writing, the latest version of mariadb is 10.6.5, as shown below, and your version may be different. Since MariaDB is a fork of MySQL, Docker used the mysql client as the default client to interact with the MariaDB service shown below.

Checking MariaDB Server Version Inside the Container
Checking MariaDB Server Version Inside the Container

5. Run the below command to enter the MariaDB shell (/bin/bash).

sudo docker exec -it mariadbata /bin/bash

The prompt changes from the Ubuntu shell to the MariaDB shell, as shown below.

You can see below the container ID (3a7fa779b559) you saw in step three. Seeing the container ID in the prompt indicates you’re currently inside the mariadb docker container.

Accessing the MariaDB Shell
Accessing the MariaDB Shell

When inside the container, you can run any commands with the sudo prefix since you are now the root user of this container.

For example, you can run the apt update command below to update the package database inside the container without the sudo prefix.

apt update -y
Updating the Package Database Inside the Container
Updating the Package Database Inside the Container

6. Finally, run the exit command to leave the MariaDB shell and return to your Ubuntu host shell.

exit
Leaving the MariaDB shell
Leaving the MariaDB shell

Accessing MariaDB Shell from the Host Terminal

You can also access the MariaDB shell directly from the host terminal by installing the MariaDB client on the host. Doing so is helpful in situations where you don’t want to switch to the container shell every time.

1. Run the apt install command below to install the MariaDB client on your Ubuntu host.

sudo apt install -y mariadb-client
Installing the MariaDB Client
Installing the MariaDB Client

2. Next, run the docker inspect command below to find the IP address of the running container.

sudo docker inspect container-name | grep IPAddress
Finding the IP address of the Running Container
Finding the IP address of the Running Container

3. Now run the mysql command below to connect to the database instance from your host (-h) with your username (root) and password (-p). Remember to replace host_ip with your running Docker container’s IP address.

sudo mysql -u root -p -h host_ip

4. Enter the password you set up earlier (ata123) in step two when prompted, and you’ll be presented with the MariaDB client prompt, as shown below.

You can then run any SQL code that you need to run on the MariaDB server.

Logging in to the MariaDB shell
Logging in to the MariaDB shell

5. Finally, run the ls command below to list the backup files (opt/mariadb/backup) of the MariaDB docker Container.

ls -l opt/mariadb/backup

You will see a list of similar files below. The output indicates you have successfully created a backup of the mariadb docker container. You can copy the files to another location on your host if needed.

Listing the Backup Files
Listing the Backup Files

Stopping and Removing the Container

Perhaps you want to free up RAM or CPU resources. If so, you can stop the container by running the docker stop command and specifying either the container ID or the container name.

1. Run either of the commands below to stop a specific container.

# Stops Container by Container Name (mariadbata)
sudo docker stop mariadbata
# Stops Container by Container ID (3a7fa779b559)
sudo docker stop 3a7fa779b559

At this point, the container is stopped but will still live on your host, and the data remains.

Stopping the mariadbata container
Stopping the mariadbata container

2. Now, run the docker ps command below to list all (-a) containers.

docker ps -a

As you see below, the container still shows but is in an Exited state, which indicates the container is stopped.

Showing the Stopped Container
Showing the Stopped Container

3. Finally, run the below command to remove (rm) the stopped container (mariadbata). The capability to remove a container comes in handy when perhaps the Docker image does not meet your requirement, or you just want to start over.

Note that you must stop the container first before removing it with the docker rm command.

sudo docker rm mariadbata

If you prefer to remove the entire container with its data volumes to save your resources, append the -v option with the sudo docker command like the one below.

sudo docker rm -v mariadbata
Removing the Entire Container with its Data Volumes
Removing the Entire Container with its Data Volumes

Now re-run the docker ps command below to list all containers to verify the container is completely removed.

docker ps -a

As you see below, the mariadb docker container is gone along with all its data.

Checking Available Containers
Checking Available Containers

Conclusion

In this tutorial, you’ve learned to create a MariaDB Docker container on your Ubuntu host machine. You also learned to create persistent storage for your Docker containers using data volume containers to back up the container data.

At this point, you can now create and manage your own MariaDB Docker container on Ubuntu.

Now, why not build a Buddy CI/CD workflow using a MariaDB Cocker container as your database with this newfound knowledge? Or even create a MariaDB service for your applications?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!