# How to install MariaDB via Docker

Docker makes it easy to set up MariaDB quickly. First, download the appropriate image. Then configure and start the container with your preferred settings. This lets you work in a controlled environment, independent of your computer’s main system.

## What are the advantages of running MariaDB in Docker?

[Docker](https://www.ionos.ca/digitalguide/server/know-how/what-is-docker/) provides a straightforward way to run [MariaDB](https://www.ionos.ca/digitalguide/server/know-how/what-is-mariadb/) in **isolated environments**. You can start new MariaDB containers quickly and try different versions without them interfering with each other. This saves time, avoids conflicts with local installations and ensures consistent conditions for development and testing.

## What are the requirements?

Before getting started, make sure you have:

- Docker is installed and running on your system
- Access to a command line (Terminal, PowerShell, etc.) with administrator rights
- Optional for MariaDB: [Docker Compose](https://www.ionos.ca/digitalguide/server/configuration/docker-compose-tutorial/) for more complex setups

## How to install MariaDB in Docker

Below, we walk you through installing MariaDB in Docker in just a few steps. For details on setting up Docker itself, see our [Docker Tutorial](https://www.ionos.ca/digitalguide/server/configuration/docker-tutorial-installation-and-first-steps/).

### Step 1: Install Docker

If you haven’t installed Docker yet, use the official **installation script**:

```bash
curl -sSL https://get.docker.com/ | sh
```

For Windows or macOS, **Docker Desktop** is recommended. After installation, check this with:

```bash
docker --version
```

You should see a version number such as `Docker version 27.5.1, build cb74dfcd`. This means Docker has been installed correctly.

### Step 2: Download MariaDB image

Now download the official [MariaDB Docker image](https://hub.docker.com/_/mariadb "mariadb image on Docker Hub") from **Docker Hub**. This is a ready-to-use version of MariaDB:

```bash
docker pull mariadb:latest
```

To get a specific version, use:

```bash
docker pull mariadb:10.11
```

You can then check all locally stored images with the following command:

```bash
docker images
```

Look for `mariadb` and the version tag in the list.

### Step 3: Start the MariaDB Docker container

Create the container, using the following command:

```bash
docker run --name mariadb-container -e MYSQL_ROOT_PASSWORD=password -d -p 3306:3306 mariadb:latest
```

- `--name mariadb-container`: Gives the container a name, so you can refer to it later.
- `-e MYSQL_ROOT_PASSWORD=password`: Sets the root password for the MariaDB database. You will need this later to log in.
- `-d`: Runs the container in the background (in detached mode), so your console remains free.
- `-p 3306:3306`: Makes the database accessible on your computer through port 3306.

After running this command, Docker should start the container. To check the status, enter:

```bash
docker ps
```

In the list of running containers, `mariadb-container` should appear with the status `Up`. If the container isn’t running, view its log files for troubleshooting with:

```bash
docker logs mariadb-container
```

The log output helps you identify any configuration errors.

### Step 4: Access MariaDB

Once the MariaDB Docker container is running, you can connect with a **MySQL-compatible client**, such as the MySQL command line tool or graphical tools like DBeaver, HeidiSQL or Beekeeper Studio.

For example, from the command line:

```bash
mysql -h 127.0.0.1 -P 3306 -u root -p
```

Enter the password you set with `MYSQL_ROOT_PASSWORD`.

If successful, you’ll have access to the MariaDB console and can run **SQL commands** such as:

```sql
SHOW DATABASES;
```

This will list the default databases. You can then create your own databases and tables.

### Step 5: Use volumes to keep data between container runs

By default, container data is deleted when the container is removed. To secure your data, use a volume:

```bash
docker run --name mariadb-container -e MYSQL_ROOT_PASSWORD=password -d \
    -v mariadb_data:/var/lib/mysql \
    -p 3306:3306 mariadb:latest
```

Here `-v mariadb_data:/var/lib/mysql` creates a Docker volume named `mariadb_data` for storing MariaDB data.

You can view the stored volumes at any time using this command:

```bash
docker volume ls
```

And use this command to inspect a volume:

```bash
docker volume inspect mariadb_data
```

### Step 6: Manage MariaDB with Docker Compose

For more complex setups, you should use [Docker Compose](https://www.ionos.ca/digitalguide/server/configuration/docker-compose-tutorial/). First create a file named `docker-compose.yml` and add the following content:

```yaml
version: '3.1'
services:
    mariadb:
        image: mariadb:latest
        container_name: mariadb-compose
        environment:
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: example_db
        ports:
            - "3306:3306"
        volumes:
            - mariadb_data:/var/lib/mysql
volumes:
    mariadb_data:
```

Then, start the environment with:

```bash
docker-compose up -d
```

MariaDB will run in the background, with saved data and a preconfigured database.

### Step 7: Stop, start and remove container

You can manage the MariaDB Docker container at any time. If you’ve set up a volume, you can stop or restart the container without losing your database files.

**Stop container:**

```bash
docker stop mariadb-container
```

**Start container:**

```bash
docker start mariadb-container
```

**Remove container:**

```bash
docker rm mariadb-container
```

**Remove container and its volume:**

```bash
docker rm -v mariadb-container
```

### Step 8: Enable automatic

If you want your MariaDB Docker container to start automatically whenever your computer restarts, use the `--restart` option when creating it:

```bash
docker run --name mariadb-container -e MYSQL_ROOT_PASSWORD=password \
    -v mariadb_data:/var/lib/mysql \
    -p 3306:3306 \
    --restart unless-stopped \
    -d mariadb:latest
```

`--restart unless-stopped` tells Docker to start the container automatically after a reboot, unless you’ve stopped it manually.


This is a markdown version of: [https://www.ionos.ca/digitalguide/hosting/technical-matters/mariadb-docker/](https://www.ionos.ca/digitalguide/hosting/technical-matters/mariadb-docker/) for AI/LLM consumption.