MariaDB is a popular open-source relational database that serves as a drop-in replacement for MySQL. It’s fast, reliable, and widely used in web development. If you want a quick and clean way to run MariaDB without installing it directly on your system,
Docker is the way to go.
In this guide, you’ll learn how to install and run
MariaDB using Docker, set up users and passwords, and connect to the database using tools like DBeaver or the MariaDB command-line interface.
Let’s walk through the steps!
Why Use Docker to Run MariaDB?
Running MariaDB in Docker has many advantages:
- No installation required on your actual system
- Easy to start, stop, or delete
- Perfect for testing and development environments
- Clean and isolated
- Works across platforms
Prerequisites
Make sure Docker is installed before starting. If not:
Or for Ubuntu/Linux:
sudo apt update && sudo apt install docker.io
Verify Docker is working:
docker --version
Step 1: Pull the MariaDB Docker Image
Open your terminal and run:
docker pull mariadb
This will download the latest MariaDB image from Docker Hub. To pull a specific version (e.g., 10.6):
>docker pull mariadb:10.6
Step 2: Run the MariaDB Container
Now, let's start a MariaDB container with your own database, username, and password:
docker run --name mariadb-container \
-e MARIADB_ROOT_PASSWORD=secret123 \
-e MARIADB_DATABASE=mydb \
-e MARIADB_USER=myuser \
-e MARIADB_PASSWORD=mypassword \
-p 3306:3306 \
-d mariadb:latest
What this does:
- Creates a new container called
mariadb-container
- Sets the root password to
secret123
- Creates a database called
mydb
- Adds a user
myuser
with password mypassword
- Exposes the database on port 3306 (default)
- Runs it in the background (
-d
)
Step 3: Persist Data Using Docker Volumes
If you want your data to survive container restarts or deletions, use a volume:
docker run --name mariadb-container \
-e MARIADB_ROOT_PASSWORD=secret123 \
-e MARIADB_DATABASE=mydb \
-e MARIADB_USER=myuser \
-e MARIADB_PASSWORD=mypassword \
-p 3306:3306 \
-v mariadb-data:/var/lib/mysql \
-d mariadb:latest
The volume
mariadb-data
will hold your database files safely.
Step 4: Check If MariaDB Is Running
To verify the container is running:
docker ps
You should see something like:
CONTAINER ID IMAGE COMMAND STATUS PORTS
xxxxx mariadb "docker-entrypoint..." Up 10 seconds 3306/tcp
Step 5: Connect to MariaDB
Option 1: Using the CLI (Inside the Container)
docker exec -it mariadb-container mariadb -u root -p
Enter the root password (
secret123
) and you'll enter the MariaDB shell.
Option 2: Using GUI Tools (DBeaver, HeidiSQL, etc.)
Use these settings:
- Host:
localhost
- Port:
3306
- Username:
myuser
- Password:
mypassword
- Database:
mydb
You’re now connected and can manage your database easily.
Step 6: Manage the Container
Useful Docker commands:
Task |
Command |
Stop container |
docker stop mariadb-container |
Start container |
docker start mariadb-container |
Remove container |
docker rm -f mariadb-container |
Remove volume |
docker volume rm mariadb-data |
Use Docker Compose
Want an even simpler way to manage your database setup? Use a
docker-compose.yml
file:
version: '3.8'
services:
mariadb:
image: mariadb:latest
container_name: mariadb-container
environment:
MARIADB_ROOT_PASSWORD: secret123
MARIADB_DATABASE: mydb
MARIADB_USER: myuser
MARIADB_PASSWORD: mypassword
ports:
- "3306:3306"
volumes:
- mariadb-data:/var/lib/mysql
volumes:
mariadb-data:
Start the container with:
docker-compose up -d
Stop it with:
>docker-compose down
Best Practices
- Use strong passwords for production setups.
- Always use volumes to prevent data loss.
- Avoid using the
root
account in applications—create separate users.
- Use
docker-compose
for team projects and multi-service setups.
image quote pre code