Deploy MariaDB Docker Container with Docker Compose


MariaDB Server is an open-source relational database management system (RDBMS) that is a fork of MySQL. It was created by the original developers of MySQL after concerns arose over the acquisition of MySQL by Oracle Corporation. The development of MariaDB is led by the MariaDB Foundation, which ensures that the software remains open source and free.

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including services, networks, and volumes, in a single file called docker-compose.yml. With a single command, you can then spin up your entire application stack, simplifying the process of managing and deploying multi-container Docker applications.


  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in linux commands.
  • Internet connectivity.
  • Docker should installed on your machine.

To install Docker on ubuntu 22.04 LTS, We can use the given link.

In this post, We will deploy the MariaDB database docker container with docker compose on Ubuntu 22.04 LTS machine.

Step 1: Creating docker-compose.yml

To deploy a MariaDB container using Docker Compose, you’ll need to create a docker-compose.yml file that defines the service. Here’s a simple example:

To create a docker-compose.yml

sudo nano docker-compose.yml

Copy and paste the followings configuration.

version: '3'
    image: mariadb:latest
    restart: always
      MYSQL_ROOT_PASSWORD: your_root_password
      MYSQL_DATABASE: your_database_name
      MYSQL_USER: your_database_user
      MYSQL_PASSWORD: your_database_password
      - mariadb_data:/var/lib/mysql
      - "3306:3306"


Save and exit from the nano text editor and in this above example we are using following config.

  • mariadb is the name of the service.
  • The image specifies the Docker image to use (mariadb:latest).
  • restart: always ensures that the container restarts automatically if it stops.
  • The environment section sets up environment variables for configuring MariaDB, including the root password, database name, user, and password.
  • The volumes section mounts a volume to persistently store MariaDB data.
  • The ports section maps port 3306 on the host to port 3306 in the container, allowing you to connect to the MariaDB instance from your host machine.

Step 2: Deploy MariaDB Container

To deploy this MariaDB service, follow these steps, Run the following command to start the MariaDB container:

sudo docker-compose up -d

The -d flag runs the containers in the background, It will take few sec or mins to pull the image and deploy the MariaDB.

Step 3: Validate the Container

To check the running containers launched by Docker Compose, you can use the following command:

sudo docker-compose ps

This command provides a summary of the status of each service defined in your docker-compose.yml file. It shows information such as the service name, container ID, status, ports, and names.

Here’s an example of what the output might look like:

   Name                 Command            State               Ports
mariadb mysqld   Up>3306/tcp

Step 4: Logging

Some time we need to also check the real time logs, So we can use the given commands.

sudo docker-compose logs -f mariadb

Step 5: MariaDB connection PHP

To establish a connection between a PHP script and a MariaDB (or MySQL) database, you can use the MySQLi (MySQL Improved) extension, which provides an object-oriented interface to interact with the database. Here’s an example PHP script that connects to a MariaDB database:


$servername = "localhost";  // Change this to your MariaDB server hostname or IP address
$username = "your_username"; // Change this to your database username
$password = "your_password"; // Change this to your database password
$dbname = "your_database";   // Change this to your database name

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";

// Perform your database operations here

// Close the connection


Replace the MariaDB IP address, Port, Username and password before save the file and testing.

Step 6: Destroy MariaDB Container

To destroy (stop and remove) the MariaDB container launched with Docker Compose, you can use the following command in the directory where your docker-compose.yml file is located:

sudo docker-compose down

This command stops and removes all the containers, networks, and volumes defined in your docker-compose.yml file.

The down command stops and removes the containers but retains the data volumes by default. If you want to remove the volumes as well, you can use the -v option:

sudo docker-compose down -v

Make sure you are in the correct directory containing your docker-compose.yml file when running these commands. This ensures that Docker Compose identifies the correct configuration file.

After running the docker-compose down command, you can use the docker-compose ps command to verify that the containers are no longer running. The output should be empty, indicating that no containers are currently running.

Remember that this command will stop and remove all the services defined in your docker-compose.yml file, not just the MariaDB service. If you only want to remove a specific service, you can specify the service name:

docker-compose down -v <service_name>

Replace <service_name> with the actual name of the service you want to remove.


We have successfully deployed MariaDB database server on docker’s container using docker compose on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.


Deploy MariaDB Docker Container with Docker Compose

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top