Deploy a Production Docker MariaDB Installation

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

MariaDB is a relational database management system developed by Michael Widenius and his colleagues at MySQL AB. MariaDB has been built from the ground up to work on all platforms, including Linux, Microsoft Windows, and Mac OS X. The goal of this guide is to help you deploy a production-ready image for your company’s MariaDB container environment.

The “docker-compose mariadb example” is a command that can be used to deploy MariaDB on production. It will create a container of MariaDB with the necessary configuration files and start it.

Deploy a Production Docker MariaDB Installation

If you’re going to install MariaDB on your system, you should isolate it from the rest of the system to avoid breaking anything. You could use a virtual machine, but it would demand a lot of resources since you’d be installing one full system on top of another. So why not use Docker to run MariaDB?

Docker can assist you if you just have restricted hardware. Docker is a portable virtualization platform. In addition, you’ll learn how to deploy a production Docker for MariaDB server installation inside a Docker container with minimal resource use in this article.

Continue reading and never squander your money or time again!

Prerequisites

This will be an interactive presentation. Make sure you have the following items if you want to follow along.

  • A Linux computer – Ubuntu 20.04 LTS is used in this demonstration, although any Linux distribution will work.

Ubuntu 20.04 LTS Installation Guide

MariaDB Docker Image in Action

The official MariaDB Docker image is the easiest way to get started with MariaDB on your PC. The image includes an installation of MariaDB’s most recent stable version, as well as some scripts to assist you in managing the container.

1. To make sure your system is up to date, use the apt update command. This command gets the most recent package lists from Ubuntu’s repositories and installs any packages that are available.

upgrading your computer systemupgrading your computer system

Associated:Examples of Ubuntu Apt Learning

2. Start the docker service using the systemctl command below.

systemctl start docker sudo

The Docker service does not provide any output when it is started. You’ll see the following error if you perform docker-related tasks when Docker isn’t running.

Running Docker Commands and Getting an ErrorRunning Docker Commands and Getting an Error

Systemd services may be controlled using Ubuntu systemctl.

3. Run the docker status command to see whether the docker service is up and running.

service docker status sudo

The Docker service is shown in the screenshot below (running).

The Docker Service is being examined.The Docker Service is being examined.

4. Finally, use the gpasswd command to join the docker group as your current user. This allows the user to utilize docker commands.

docker sudo gpasswd -a “$USER”

Adding your existing user to the Docker GroupAdding your existing user to the Docker Group

When performing a docker command, adding the current user to the docker group prevents the following error.

Docker Error Getting Permission DeniedDocker Error Getting Permission Denied

5. To find the MariaDB image, use the docker search command below. This command looks for images that match mariadb in the Docker Hub (official collection of repositories) and provides the results.

docker search mariadb sudo

Any image you wish to use to install MariaDB should be checked. Because it is well maintained, this example utilizes the official image called mariadb.

If a picture is official, the OFFICIAL column indicates that it is OK. The picture is submitted by a community developer if it does not have the OK status. If you have problems with the picture in such scenario, you must contact the developer.

looking for mariadb imageslooking for mariadb images

6. Use the docker pull command to get the image you selected in step 5 from the Docker repository. This command retrieves (pulls) the desired image from its official repository and saves it locally.

When the command is finished, your server will have the newest stable version of the MariaDB image, as seen below.

MariaDB Image DownloadMariaDB Image Download

You may utilize the tags provided for that image to install prior versions if you like. To see all the versions, go to the Image tag/s columns in dockerhub, then use the docker pull command and specify the specific version, as shown below. docker pull mariadb:10.2 sudo

7. Finally, use the docker images command to get a list of all the images on your server. You should now have a working MariaDB Docker image on your server.

The name of the docker image, mariadb, is shown below, along with the tag latest.

Docker Images ListDocker Images List

To operate a local MariaDB server, create a MariaDB Container.

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

To store the backup file you’ll produce in this tutorial, establish a new opt/mariadb/backup directory on your server. It’s critical to back up your data in your production environment before making changes in case anything goes wrong.

When you update the internal volume of a container, the change is replicated to the mapped host directory. You’ll construct the volume on the local server to store a backup copy of all container data.

To create the opt/mariadb/backup directory, use the command below. Before establishing this directory, the -p option guarantees that the parent directory exists.

The command will fail if any of the parent directories in the path do not exist.

mkdir -p opt/mariadb/backup sudo

How to Make a Backup DirectoryHow to Make a Backup Directory

2. To construct a Docker container from the mariadb image, use the docker run command below. This command does the following tasks:

  • Maps (-v) the default directory for MariaDB data in your Docker container (/var/lib/mysql) to your host directory (/opt/mariadb/backup).
  • Declares the MariaDB environment variable (-e) MYSQL ROOT PASSWORD to save the MariaDB server’s root password. Make sure ata123 is replaced with your strong password.
  • Docker will launch the container in detached mode (-d), which means it will operate in the background. This allows you to exit the current session and continue working in the background while this container runs. Otherwise, you’ll receive a blank prompt and will have to terminate the currently operating container.
  • Specifies the name of the container to construct (—name mariadbata). You may change mariadbata to any name you choose.
  • The default MariaDB port 3306 is made available to the host system (-p 3306:3306). (where the MariaDB service listens to your client connections). You’ll assign the default port to the production port on the production server. However, you may use any other production port number instead of 3306.

/opt/mariadb/backup:/var/lib/mysql -e MYSQL ROOT PASSWORD=ata123 -d mariadb

The container has been constructed and is operating in the background at this point, but you can never be too sure. As a result, the container will be verified in the following phase.

Creating a Docker Container for MariaDBCreating a Docker Container for MariaDB

3. Verify if your Docker container is active with the docker ps command.

The mariadbata container has been up for 7 minutes, listening on port 3306 on all host interfaces, as shown below. You can also see the container’s ID (3a7fa779b559).

You may connect to the container, list containers, stop containers, and remove the container using this unique ID.

4. Check the MariaDB server version within the mariadbata container using the docker exec command below.

sudo docker exec mysql -V mariadbata

The most recent version of mariadb is 10.6.5, as indicated below; however, your version may change. Docker utilized the mysql client as the default client to interface with the MariaDB service displayed below since MariaDB is a fork of MySQL.

Inside the Container, Checking MariaDB Server VersionInside the Container, Checking MariaDB Server Version

5. Enter the MariaDB shell (/bin/bash) using the command below.

/bin/bash sudo docker exec -it mariadbata

As illustrated below, the prompt switches from the Ubuntu shell to the MariaDB shell.

The container ID (3a7fa779b559) that you observed in step three is shown below. You’re presently inside the mariadbata container if you see the container ID in the popup.

Getting inside the MariaDB ShellGetting inside the MariaDB Shell

Since you are now the root user of this container, you may perform any commands with the sudo prefix while within it.

For example, to update the package database within the container, execute the apt update command without the sudo prefix.

Inside the Container, updating the Package DatabaseInside the Container, updating the Package Database

6. Finally, quit the MariaDB shell and return to your Ubuntu host shell using the exit command.

Getting out of the MariaDB shellGetting out of the MariaDB shell

MariaDB Shell Access from the Host Terminal

By MariaDB Client Installation on the host, you may also access the MariaDB shell directly from the host terminal. In circumstances when you don’t want to switch to the container shell every time, this is useful.

1. To install the MariaDB client on your Ubuntu server, use the apt install command.

mariadb-client sudo apt install -y

MariaDB Client InstallationMariaDB Client Installation

2. To determine the IP address of the running container, use the docker inspect command.

grep IPAddress | sudo docker inspect container-name

Obtaining the Running Container's IP AddressObtaining the Running Container’s IP Address

3. Now, use the mysql command below to connect to the database instance from your host (-h) using your root login and password (-p). Replace host ip with the IP address of your current Docker container.

sudo mysql -p -h host ip -u root

4. When asked, enter the password you created in step two (ata123), and you’ll see the MariaDB client prompt, as seen below.

You may then use the MariaDB server to execute whatever SQL code you require.

Accessing the MariaDB shellAccessing the MariaDB shell

5. Finally, use the ls command to list the MariaDB Container’s backup files (opt/mariadb/backup).

Below is a list of files that are similar. The report shows that a backup of the mariadbata container was successfully made. If necessary, you may transfer the files to another place on your server.

a list of backup filesa list of backup files

Container Stopping and Removal

You may wish to free up some RAM or CPU resources. If this is the case, use the docker stop command with either the container ID or the container name to stop the container.

1. To halt a particular container, use one of the instructions below.

# Stops a container based on its name (mariadbata) mariadbata sudo docker stop # Stops a container based on its ID (3a7fa779b559) docker stop sudo 3a7fa779b559

The container will be halted at this moment, but the data will stay on your host.

The mariadbata container is being stopped.The mariadbata container is being stopped.

2. Use the docker ps command to get a list of all (-a) containers.

The container is still visible, but it is in an Exited state, indicating that it has been halted.

The Stopped Container is shown.The Stopped Container is shown.

3. Finally, use the command below to delete the halted container (rm) (mariadbata). When the Docker image does not suit your requirements or you just want to start again, the option to delete a container comes in useful.

You must first stop the container before using the docker rm command to remove it.

docker rm mariadbata sudo

If you want to conserve resources by removing the whole container with its data volumes, use the sudo docker command with the -v option, as shown below.

docker rm -v mariadbata sudo

Delete the whole container, including all data volumesDelete the whole container, including all data volumes

Rerun the docker ps command to list all containers to ensure the container has been deleted fully.

The mariadbata container, along with all of its data, has vanished, as seen below.

Availability of ContainersAvailability of Containers

Conclusion

You learnt how to construct a MariaDB Docker container on your Ubuntu host system in this article. You also learnt how to back up the data in your Docker containers using data volume containers to provide persistent storage.

You can now use Ubuntu to construct and manage your own MariaDB Docker container.

With this newfound information, why not create a Buddy CI/CD process utilizing a MariaDB Cocker container as your database? Or perhaps make your own MariaDB service for your apps?

The “docker-compose mariadb volume” is a Docker compose file that can be used to deploy a production MariaDB installation.

Related Tags

  • mariadb docker
  • mariadb docker-compose
  • mariadb dockerfile example
  • mariadb docker tutorial
  • mariadb docker github