Docker Environment Variables

When you look up Docker Hub for a particular image, all the environment variables relevant for that particular image are listed there. Some of these environment variables are mandatory while others are optional.

Environment variables allow us to set certain properties when when we run a container from an image. Explaining this with an example is much easier. Let’s take MySQL Docker image for instance. On Docker Hub page for MySQL, you can find following section.

How to use this image section in Docker Hub

Here MYSQL_ROOT_PASSWORD is a mandatory environment variable for MySQL, which is why it is mentioned here. If you further scroll down, there is a section named Environment variables which lists down all the environment variables relevant for this image and their purpose.

Environment variables section in Docker Hub

As you can see, here it lists MYSQL_ROOT_PASSWORD as a mandatory environment variable, along with some other variables we can use.

Let’s pull this image from Docker Hub and run a container.

charith@ch MINGW64 ~
 $ docker run -d --name db \
   -e MYSQL_ROOT_PASSWORD=charith \
   -e MYSQL_DATABASE=myapp \
   -e MYSQL_USER=charith \
   -e MYSQL_PASSWORD=charith \
   mysql
   3987ccac109870e3534fdfe77f3d48d4ad1f4aeebae762ee89df735a16d96de9 

I have defined four environment variables here and I think they are all pretty much self explanatory.

Note that here I have used backslashes at the end of each line except the last so that I can type my command in multiple lines to improve readability. Unfortunately, Windows command line does not respond well to this backslash, so I had to use MinTTY based GitBash terminal. There are some quirks with GitBash as well, which is why I was using Windows command line in the first place. If you keep using it, you would find out.

Now let’s open an interactive terminal inside this container and see if we can perform some SQL operations. You see, the things we looked at in previous articles are now coming in handy!

Okay, switching back to Windows command line(quirks! :/ I would encourage you to find out on your own!)

C:\WINDOWS\system32>docker exec -it db sh
 mysql -u charith -p
 Enter password:
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 12
 Server version: 8.0.23 MySQL Community Server - GPL
 Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 Oracle is a registered trademark of Oracle Corporation and/or its
 affiliates. Other names may be trademarks of their respective
 owners.
 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 mysql>

So I have opened a shell using docker exec -it and then enter mysql -u <username> -p, which prompts me for the password. Once that is done, we get a mysql prompt where we can enter any SQL statement we want.

mysql> show databases;
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | myapp              |
 +--------------------+
 2 rows in set (0.00 sec)
 mysql>

So if I run a show database statement, it shows the ‘myapp’ database I defined in the environment variable as above.

Okay so do we have to go inside the container every time we want to do this? How can we expose our container to the outside so that it is accessible from the Docker Host machine itself? We’ll look into that in Docker Networks section!

Share this article if it was helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *