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:

2025-03-01 20:04:32+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.5.2+maria~ubu2404 started.
[...]
2025-03-01 20:04:33+00:00 [Note] [Entrypoint]: Starting temporary server
2025-03-01 20:04:33+00:00 [Note] [Entrypoint]: Waiting for server startup
[...]
2025-03-01 20:04:35+00:00 [Note] [Entrypoint]: Temporary server stopped

2025-03-01 20:04:35+00:00 [Note] [Entrypoint]: MariaDB init process done. Ready for start up.
[...]
2025-03-01 20:04:36 0 [Note] Server socket created on IP: '0.0.0.0'.
2025-03-01 20:04:36 0 [Note] Server socket created on IP: '::'.
2025-03-01 20:04:36 0 [Note] mariadbd: Event Scheduler: Loaded 0 events
2025-03-01 20:04:36 0 [Note] mariadbd: ready for connections.
Version: '11.5.2-MariaDB-ubu2404'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

MariaDB initializes, sets up the database if needed, and briefly runs a temporary server. Then, it starts the main server, loads InnoDB, and becomes ready for connections on port 3306.

Notice that we used the arguments -it (interactive/terminal) and our terminal is now attached to the process in the container. MariaDB does 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 our web shell, these shortcuts may 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

Let us connect to the container by starting a shell inside it:

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.

Detached containers

Running a Docker container in “detached” mode (background) can be done with -d:

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

Now, instead of seeing container logs, only the container ID is displayed. Verify your started container by listing currently running containers:

docker ps

We learned how to pass variables to a container, start a process like a shell, and attach or detach from running containers.

Since we don’t need both MariaDB containers running, we’ll clean that up in the next lab.