Docker 02: Bash, variables and logs

A 3 minutes story written on Dec 2018 by Adrian B.G.

Containers in general, but Docker, in particular, is a harder concept to grasp. I will try to write the simplest and most straightforward tutorial for a first hands-on experience for a developer.

Pragmatic Docker series for developers

For this tutorial I presume you already installed Docker, went through the first part and you not afraid of using Bash.

What are we going to do today

  1. Set environment variables
  2. Connect to a (running) container
  3. Read the logs from a (running) container

To keep this tutorial as language and technology agnostic as possible we will use MariaDB, a popular and improved fork of MySQL.


🖥 Environment variables

… are pretty much a standard way to dynamically assign parameters / variables that change with the running environment like database or passwords (each developer and cluster has their own values).

Docker can relay the env vars values to the container process in multiple ways: at docker run or trough docker-compose inlineor in an .env file. We will keep it simple:

$ docker run --name mymaria -d --env MYSQL_DATABASE=play --env MYSQL_ROOT_PASSWORD=mysecretsanta --env MYSQL_USER=santa --env MYSQL_PASSWORD=claus mariadb:10.4

We instructed docker to start a mariadb instance, create a new database and user and setup a custom secret root password.

Usually docker images provide all the possible environment options in the documentation. The container can also “inherit” a local env variable by omitting its value, the same command can be written as:

$ export MYSQL_DATABASE=play  
$ docker run --name mymaria -d --env MYSQL_ROOT_PASSWORD=mysecretsanta --env MYSQL_USER=santa --env  MYSQL_PASSWORD=claus mariadb:10.4

✎ Bash

Now that we have a running database we can use it. Instead of installing on our machine a SQL Client we can use the one from the container itself:

$ docker exec -it mymaria bash  
[email protected]:/#

This command will execute the bash command inside the mymaria container, and -it will keep the console (CLI) in the interactive mode, relaying our input (typing keyboard) to the container process and the output back to us. You can exit at anytime by typing exit: [email protected]:/# exit

Because now we are inside the container we have access to all the SQL utilities from the MariaDB Image, here are a few examples:


If you already know the basics of MySQL go ahead and play with the console mysql, if not you can type these commands to insert and read some data:

$ mysql --password=mysecretsanta --database=play --execute="CREATE TABLE nice_list (name TEXT);"  
$ mysql --password=mysecretsanta --database=play --execute="INSERT INTO nice_list (name) VALUES('Adrian');"  
$ mysql --password=mysecretsanta --database=play --execute="SELECT * FROM nice_list;"
Feel free to use your name :)

You see that the env variables worked, we can use our ROOT password and connect to our new database. You can close the current CLI/bash sessionwith exit and move to the next chapter.

📜 Logs

The main container process, in our case the database server usually outputs logs (messages). You will find this feature of value when debugging is in need. You can read them by using the docker logs command as follows:


$ docker logs mymaria

You can use the --follow flag to keep watching the output and Ctrl-C to exit.

For more details see the official do commandline/logs/

When you are finished with the container you can remove it from your system with these commands: docker stop mymaria,docker rm mymaria and for the image: docker rmi mariadb:10.4.

Thanks! 🤝

Please share the article, subscribe or send me your feedback so I can improve the following posts!

comments powered by Disqus