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
- Docker 01 — first 3 steps of your web app
- Docker 02 — meet bash, variables and logs (this article)
- Docker 03 — persistence and compose multiple services (TODO)
- Docker 04 — hubs, deployments and cloud (TODO)
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
- Set environment variables
- Connect to a (running) container
- 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 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
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;"
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.
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.