1.2. Environment variables

Why was there an error in the previous lab?
The MariaDB server cannot run without a proper configuration. Docker can pass configuration variables into the setup process via environment variables. Environment variables are passed with the -e parameter, as in:

docker run -it -e MARIADB_ROOT_PASSWORD=my-secret-pw mariadb

After running the command, you will see output similar to this:

[Note] Starting MariaDB 12.0.2-MariaDB-ubu2404 source revision aab83aecdca15738d114cf5a2f223f1d12e4e6bd server_uid tdvmDBxbrAQFTMTS1CBpEc7NqKY= as process 1
[...]
[Note] mariadbd: Event Scheduler: Loaded 0 events
[Note] mariadbd: ready for connections.
Version: '12.0.2-MariaDB-ubu2404'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

Notice that we used the arguments -it (interactive terminal). Locally MariaDB would not respond to the usual CTRL-C. To exit, Docker provides an escape sequence to detach from a container while leaving it running: press CTRL-p, then CTRL-q. In this web shell, these shortcuts do not work, so close the terminal and reopen it to continue.

To verify the container is running, use:

docker ps

Output should look like this:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7cb31f821233        mariadb             "docker-entrypoint..."   5 minutes ago       Up 5 minutes        3306/tcp            upbeat_blackwell

Accessing the container

To reconnect to the container, use:

docker exec -it <container> bash

Here, <container> can be the CONTAINER ID (typically the first two characters suffice) or the NAMES from docker ps. In the example above, this could be 7cb31f821233 or upbeat_blackwell.

Executing the command should display:

root@7cb31f821233:/#

Now that we’re connected, let’s check if MariaDB is working:

mariadb -uroot -pmy-secret-pw

If successful, the MariaDB command line should appear:

Welcome to the MariaDB monitor. Commands end with ; or \g.
...
MariaDB [(none)]>

Type exit; to leave the MariaDB client, then type exit again to leave the container.