Running MariaDB in Docker lets you set up a self-hosted relational database for websites and apps without installing the database server directly on your system. You can pull the official MariaDB image, store database files in a persistent Docker volume, create a container with your database credentials, and connect your application to the database on port 3306 or via a private Docker network.
To run MariaDB in Docker:
- Prepare Docker, terminal access, storage, and database credentials.
- Pull the MariaDB Docker image and create a persistent volume.
- Start the MariaDB container with a root password, database name, user, and password.
- Use Docker Compose if you want a reusable setup for websites and apps.
- Deploy MariaDB with Hostinger’s Docker template if you prefer a faster VPS setup.
- Connect your website or app using the container name, localhost, or your VPS IP address.
- Secure the database with restricted access, backups, and VPS-level protection.
What do you need before installing MariaDB with Docker?
Before installing MariaDB with Docker, make sure your server or local machine has:
- Docker Engine installed and running to pull the MariaDB image and create containers.
- Terminal or SSH access to run Docker commands.
- Enough storage space for the MariaDB database files, backups, and logs.
- A Docker volume to keep database data after the container is stopped, removed, or recreated.
- Database credentials, including a root password, database name, username, and user password.
- A connection method for your website or app, such as a Docker network, localhost connection, or mapped port 3306.
For a production website or app, use a VPS rather than a local machine so MariaDB remains available after your computer shuts down. Docker Compose is also useful if you want to manage MariaDB alongside an application container, but it isn’t required for the initial installation.
How to install MariaDB using Docker
To install MariaDB using Docker, pull the official MariaDB image, create a persistent volume, and start a container with the database credentials your website or app will use.
1. Pull the MariaDB Docker image
First, download the MariaDB image from Docker Hub:
docker pull mariadb:latest
This command downloads the latest MariaDB image available for Docker. For production environments, use a specific version tag instead of latest to avoid unexpected version changes after future updates:
docker pull mariadb:11
After the download finishes, check that the image is available locally:
docker images mariadb
2. Create a Docker volume for MariaDB data
Next, create a Docker volume to store MariaDB data outside the container:
docker volume create mariadb_data
MariaDB stores its database files in /var/lib/mysql inside the container. Mapping this directory to a Docker volume keeps your databases, tables, and users available after the container is stopped, removed, or recreated.
3. Start the MariaDB container
Run the following command to create and start a MariaDB container:
docker run -d --name mariadb-server -e MARIADB_ROOT_PASSWORD=strong_root_password -e MARIADB_DATABASE=app_database -e MARIADB_USER=app_user -e MARIADB_PASSWORD=strong_user_password -p 3306:3306 -v mariadb_data:/var/lib/mysql mariadb:latest
This command starts MariaDB in the background and creates an initial database user for your application.
Here is what each option does:
- –name mariadb-server gives the container a readable name.
- MARIADB_ROOT_PASSWORD sets the root password for database administration.
- MARIADB_DATABASE creates the first database during initialization.
- MARIADB_USER creates a non-root database user.
- MARIADB_PASSWORD sets the password for that user.
- -p 3306:3306 maps MariaDB’s default port to the host machine.
- -v mariadb_data:/var/lib/mysql stores database files in the Docker volume.
- mariadb:latest tells Docker which image to run.
Replace the example passwords with strong values before running the command. Use the root account for administration, and use a separate application user for your website or app connection.
4. Check whether the container is running
Verify that the container is active:
docker ps
You should see mariadb-server in the list of running containers.
To inspect the MariaDB startup logs, run:
docker logs mariadb-server
The logs should show that MariaDB has initialized and is ready for connections. If the container stops immediately, check the logs for password, volume, or port conflicts.
5. Connect to MariaDB
To open the MariaDB shell from inside the container, run:
docker exec -it mariadb-server mariadb -u root -p
Enter the root password you set in MARIADB_ROOT_PASSWORD.
Once connected, test the server with:
SHOW DATABASES;
You should see the default system databases and the database you created with MARIADB_DATABASE.
6. Create a database and user manually
If you did not create a database with environment variables, or if you need another database later, create it manually from the MariaDB shell:
CREATE DATABASE IF NOT EXISTS app_database; CREATE USER IF NOT EXISTS 'app_user'@'%' IDENTIFIED BY 'strong_user_password'; GRANT ALL PRIVILEGES ON app_database.* TO 'app_user'@'%'; FLUSH PRIVILEGES;
This creates a database, adds an application user, and gives that user permission to manage the selected database.
To exit the MariaDB shell, run:
EXIT;
At this point, MariaDB is running in Docker with persistent storage and a dedicated database user. The next step is to decide whether to keep managing it with individual Docker commands or move the setup into Docker Compose for easier repeatable deployments.
How to run MariaDB with Docker Compose
Docker Compose lets you define the MariaDB container, database credentials, port mapping, restart policy, and storage volume in one file. This method is better for websites and apps because you can restart or recreate the database container without rewriting a long Docker run command.
First, create a new project folder for your MariaDB setup:
mkdir mariadb-docker cd mariadb-docker
Then, create a docker-compose.yml file:
nano docker-compose.yml
Add the following configuration:
services: mariadb: image: mariadb:latest container_name: mariadb-server restart: unless-stopped environment: MARIADB_ROOT_PASSWORD: strong_root_password MARIADB_DATABASE: app_database MARIADB_USER: app_user MARIADB_PASSWORD: strong_user_password ports: - "3306:3306" volumes: - mariadb_data:/var/lib/mysql volumes: mariadb_data:
This Compose file creates one MariaDB service with persistent storage. The mariadb_data volume stores the database files from /var/lib/mysql, so your data remains available after container restarts or updates.
Before running the file, replace the example passwords with secure values. Keep the root password for database administration and use the separate application user for your website or app.
Start MariaDB with Docker Compose:
docker compose up -d
The -d option runs MariaDB in the background. After the container starts, check its status:
docker compose ps
To view the startup logs, run:
docker compose logs mariadb
The logs should show that MariaDB has initialized and is ready for connections.
To open the MariaDB shell from the running container, use:
docker compose exec mariadb mariadb -u root -p
Enter the root password from MARIADB_ROOT_PASSWORD, then check the available databases:
SHOW DATABASES;
To stop the MariaDB container without deleting the database volume, run:
docker compose down
This command removes the container and network, but keeps the mariadb_data volume by default. Avoid using docker compose down -v unless you intentionally want to delete the database volume and its stored data.
How to deploy MariaDB faster with Hostinger’s Docker template
Hostinger’s MariaDB Docker template lets you deploy MariaDB on a VPS without manually pulling the image, creating the container, and configuring the base Docker setup. This option is useful if you want a self-hosted relational database for a website or app but prefer a faster setup process.
To deploy MariaDB with Hostinger’s Docker template:
- Go to the MariaDB Docker template page.
- Choose a VPS plan that matches your database workload.
- Click Deploy to start the MariaDB VPS setup.
- Wait until the VPS and MariaDB template are installed.
- Open your VPS management panel and copy the server connection details.
- Connect your website or app to MariaDB using the database host, port, database name, username, and password.
MariaDB is suitable for PHP applications, WordPress sites, Laravel projects, content management systems, ecommerce platforms, and SaaS apps that need structured data storage. Hostinger’s template gives you the same self-hosted database control as a manual Docker setup, while reducing the number of commands needed to get started.
After deployment, secure the database before connecting it to a production application. Use strong credentials, restrict access to port 3306, enable regular backups, and keep the MariaDB container up to date.
How to connect a website or app to MariaDB in Docker
To connect a website or app to MariaDB in Docker, use the MariaDB container name as the database host if both services run in the same Docker network. If the app runs directly on the server, connect through 127.0.0.1 and the mapped MariaDB port.
For most Docker-based websites and apps, the connection details look like this:
DB_HOST=mariadb-server DB_PORT=3306 DB_DATABASE=app_database DB_USERNAME=app_user DB_PASSWORD=strong_user_password
Use the same values you defined when creating the MariaDB container:
- DB_HOST is the MariaDB container name, such as mariadb-server.
- DB_PORT is MariaDB’s default port, 3306.
- DB_DATABASE is the database created for your website or app.
- DB_USERNAME is the non-root database user.
- DB_PASSWORD is the password for that user.
If your website or app runs in another Docker container, place both containers in the same Docker Compose file or Docker network. Then, use the MariaDB service name as the database host.
For example, a PHP, Laravel, or Node.js app running in Docker can connect to this MariaDB service with mariadb as the host:
services: app: image: your-app-image environment: DB_HOST: mariadb DB_PORT: 3306 DB_DATABASE: app_database DB_USERNAME: app_user DB_PASSWORD: strong_user_password depends_on: - mariadb mariadb: image: mariadb:latest environment: MARIADB_ROOT_PASSWORD: strong_root_password MARIADB_DATABASE: app_database MARIADB_USER: app_user MARIADB_PASSWORD: strong_user_password volumes: - mariadb_data:/var/lib/mysql volumes: mariadb_data:
In this setup, the app connects to MariaDB through Docker’s internal network. You do not need to expose port 3306 publicly unless an external service, database client, or remote server needs to connect to the database.
If the app runs directly on the same VPS instead of inside Docker, use 127.0.0.1 as the database host:
code
DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=app_database DB_USERNAME=app_user DB_PASSWORD=strong_user_password
This works only if the MariaDB container maps port 3306 to the host machine:
-p 3306:3306
For remote connections, use the VPS IP address as the database host. Only allow this setup when the connection is protected by firewall rules, strong credentials, and a trusted IP allowlist. For most production websites, keeping the database on a private Docker network is safer than exposing MariaDB to the public internet.
How to secure and maintain a MariaDB Docker database
To secure and maintain a MariaDB Docker database, use strong credentials, store database files in a persistent volume, restrict access to port 3306, and create regular backups. These steps help protect your website or app data and keep the database recoverable during container updates or server issues.
Start by using a separate database user for your application instead of connecting with the root account:
CREATE DATABASE IF NOT EXISTS app_database; CREATE USER IF NOT EXISTS 'app_user'@'%' IDENTIFIED BY 'strong_user_password'; GRANT ALL PRIVILEGES ON app_database.* TO 'app_user'@'%'; FLUSH PRIVILEGES;
This gives the application access to only the database it needs.
Next, store MariaDB data in a Docker volume:
-v mariadb_data:/var/lib/mysql
The volume keeps your database files separate from the container lifecycle, so the data remains available after container restarts, removals, or updates.
You should also avoid exposing MariaDB’s default port 3306 publicly. If your website or app runs in another Docker container, connect via a private Docker network rather than opening the database port to the internet. When remote database access is required, firewall rules, trusted IP allowlists, SSH hardening, and other VPS security best practices help protect the server that runs your MariaDB container.
Finally, create regular backups before updating or recreating the container:
docker exec mariadb-server mariadb-dump -u root -p app_database > app_database_backup.sql
For broader server protection, follow our VPS security best practices, including SSH hardening, firewall configuration, malware scanning, and regular system updates. This keeps the VPS that runs your MariaDB Docker container secure, not just the database itself.
Next steps for running MariaDB in Docker
After MariaDB is running in Docker, connect it to your website or app, test the database connection, and set up backups before storing production data. A working container is only the first step; the database also needs reliable storage, controlled access, and a clear update process.
Start by confirming that your application uses the correct database host, port, database name, username, and password. If the app runs in Docker, use the MariaDB service or container name as the database host. If the app runs on the same VPS outside Docker, use 127.0.0.1 with port 3306.
Then, add a backup routine so you can restore the database after failed updates, accidental changes, or server issues:
docker exec mariadb-server mariadb-dump -u root -p app_database > app_database_backup.sql
Store the backup outside the container and test the restore process before relying on it for production.
For long-term use, move the setup to Docker Compose if you started with a single Docker run command. Compose keeps the MariaDB image, credentials, volume, restart policy, and network configuration in one reusable file, making the database easier to update and redeploy.
If you want to skip the manual setup, Hostinger’s MariaDB Docker template provides a faster way to deploy a self-hosted relational database on a VPS. Once deployed, you can use it as the database backend for websites, PHP applications, Laravel projects, ecommerce stores, and other apps that need structured data storage.
All of the tutorial content on this website is subject to
Hostinger’s rigorous editorial standards and values.
Apply for Premium Hosting
Source Credit: https://www.hostinger.com/in/tutorials/mariadb-docker
