4.2: Launching MariaDB: Using -e (environment variables)

The groundwork is now laid: we have our networkDockerand our project directory structure.
We’re ready to launch the first service in our stack: the database.

In most web applications, the database is the heart of the system. The website will connect to it to read or save information.

For this project, we’ll be using MariaDB, a very popular database that’s fully compatible with MySQL.

📦 MariaDB images are already available

Good news: we don’t need to install MariaDB manually.
An official image already exists onDockerHub.

Upon first launch,Docker will automatically download the image if it is not present on your machine.

⚙️ Configuration with environment variables

Unlike a standard installation, we don’t configure MariaDB using a wizard or a configuration file.

Configuration takes place when the container is launched using environment variables.

In Docker, we use the option:

-e

This allows you to define a variable within the container.

For MariaDB, certain variables are automatically recognized by the official image:

VariableRole
MYSQL_ROOT_PASSWORDroot account password
MYSQL_DATABASEDatabase to be created automatically
MYSQL_USERApplication user
MYSQL_PASSWORDPassword for this user

These variables ensure that a database is ready to use right from the start.

🚀 Start the MariaDB container

We will now start our database:

docker run -d \
--name mariadb \
--network monprojet-network \
-v $(pwd)/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=rootpassword \
-e MYSQL_DATABASE=monsite \
-e MYSQL_USER=monsiteuser \
-e MYSQL_PASSWORD=monsitepassword \
--restart unless-stopped \
mariadb:11

Let’s break down this command.

🔍 Understanding each option

-d

Launches the container in the background (detached mode).

Without this option, the terminal would remain stuck on the logs.

--name mariadb

Gives the container a name.

This will allow other containers on the network to contact the database simply by using:

mariadb

sinceDockerit provides automatic internal DNS.

--network monprojet-network

Connects the container to the network we created in the previous lesson.

This is what will allow the future PHP container to communicate with the database.

-v $(pwd)/data:/var/lib/mysql

This is the data folder mount.

HostContainer
./data/var/lib/mysql

In MariaDB, the folder/var/lib/mysqlcontains all the databases.

Result:
the data will be stored on your machine, not just in the container.

-e

These settings automatically configure MariaDB on first startup:

-e MYSQL_ROOT_PASSWORD=rootpassword
-e MYSQL_DATABASE=monsite
-e MYSQL_USER=monsiteuser
-e MYSQL_PASSWORD=monsitepassword

MariaDB will therefore:

  • create the database mysite
  • create the user `monsiteuser`
  • grant them access to this database

--restart unless-stopped

This option is very important in production.

DockerIt instructs:

Automatically restart this container if it stops.

The behavior is as follows:

SituationResult
Server rebootcontainer restarted
container crashcontainer restarted
manual shutdown (docker stop)container remains stopped

This is a widely used option for critical services such as databases.

🔎 Check that MariaDB is running

To verify that everything started successfully:

docker ps

You should see something like:

CONTAINER ID   NAME      IMAGE        STATUS
xxxxxxx mariadb mariadb:11 Up 10 seconds

If you want to view the logs:

docker logs mariadb

On first startup, MariaDB will:

  • initialize the database
  • create users
  • prepare the data files

This may take a few seconds.


📌 Key takeaways

To launch MariaDB,Dockerwe used several important concepts:

  • -e to configure the application via environment variables
  • -v to store data
  • --network to communicate with other containers
  • --restart unless-stopped to ensure service availability

The database is now up and running.

In the next lesson, we’ll start the Apache/PHP server and connect it to MariaDB to display our first containerized website. 🚀