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. ๐Ÿš€