Podman series: Deploying a MariaDB Database in a Podman Container

Podman series: Deploying a MariaDB Database in a Podman Container

use environment variables

In this blog post, we'll guide you through creating a MariaDB database container using Podman. We'll also show you how to use environment variables to configure the database and connect to it using a MySQL client.

Prerequisites: Podman installed on your system.

Steps:

  1. Create the MariaDB container:

We'll use the podman run command to create a MariaDB container named mdb. The -d flag detaches the container from the current terminal session, allowing it to run in the background. We'll also map the container's port 3306 to the host's port 3306 using the -p flag. Finally, we'll set several environment variables to configure the database:

  • MYSQL_USER: Username for accessing the database (set to admin here)

  • MYSQL_PASSWORD: Password for the admin user (set to saysoyeah here)

  • MYSQL_ROOT_PASSWORD: Password for the root user (set to SQLp4ss here)

  • MYSQL_DATABASE: Name of the database to create (set to testDB here)

Here's the command to run:

podman run -d --name=mdb -p 3306:3306 -e MYSQL_USER=admin -e MYSQL_PASSWORD=saysoyeah -e MYSQL_ROOT_PASSWORD=SQLp4ss -e MYSQL_DATABASE=testDB docker.io/mariadb:latest

Once you run this command, verify that the container is running using the podman ps command.

  1. Connect to the MariaDB container:

Before interacting with the database, ensure you have a MySQL client installed on your system. You can typically install it using your system's package manager. For instance, on Debian-based systems, you can use:

apt install mariadb-client-core-10.6

Now, let's connect to the testDB database using the mysql command. We'll provide the following credentials:

  • Username: admin

  • Password: saysoyeah

  • Host: localhost (since the container is running on the same host)

  • Port: 3306 (mapped from the container)

We'll use the echo command to pipe the show databases; statement to the mysql client to check if the database was created successfully:

echo "show databases;" | mysql -uadmin -h localhost -psaysoyeah -P 3306
  1. Interact with the database:

We've created a sample SQL file named test.sql containing statements to:

  • Use the testDB database

  • Create a table named employee with columns for ID, name, and location

  • Insert some sample employee data into the table

  • List the contents of the employee table

Here's the content of the test.sql file:

SQL

use testDB;
CREATE TABLE employee (
  empid INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255),
  location VARCHAR(255)
);
INSERT INTO employee (name, location) VALUES ('John Doe', 'New York');
INSERT INTO employee (name, location) VALUES ('Jane Smith', 'Los Angeles');
INSERT INTO employee (name, location) VALUES ('Alice Johnson', 'Chicago');
INSERT INTO employee (name, location) VALUES ('Bob Williams', 'Houston');
INSERT INTO employee (name, location) VALUES ('Emily Brown', 'San Francisco');

Let's execute the commands in the test.sql file using the mysql client:

Bash

mysql -uadmin -h localhost -P 3306 -psaysoyeah < test.sql 
echo "use testDB;select * from employee;" | mysql -h localhost -P 3306 -uadmin -psaysoyeah

This will create the table, insert data, and then list the contents of the employee table.

Summary

In this blog post, we demonstrated how to create a MariaDB database container using Podman and configure it with environment variables. We also connected to the database container using a MySQL client and performed basic operations using an SQL script.

Feel free to leave a comment below if you have any questions or would like to see more complex Podman use cases!