Mastering Docker: Customizing Container Behavior with Environment Variables

Welcome back! In this practical demo, we’re going to dive into one of the fundamental features of Docker – environment variables. They provide you with the ability to customize your containers’ behavior with ease. By the end of this tutorial, you’ll understand how environment variables work, how to use them effectively, and why they are crucial in a real-world production environment.

Setting the Stage

To begin, we’ve prepared a simple demonstration using a Docker container and a shell script. The script’s task is straightforward: it prints the message “Hello World!” five times, with a one-second pause between each message. This script acts as a basis for showcasing how environment variables can help you customize the container’s behavior.

In your Docker environment, you may need to set environment variables for various use cases, such as configuring a Flask web application, fine-tuning a Node.js application, or defining parameters for databases like Cassandra or MySQL.

We’ll start by running the demo without environment variables and then move on to explore how to use them to tailor the container’s behavior.

The Demo

Our demo begins with two key files: the Dockerfile and a shell script called magic.sh. The Dockerfile defines the container’s environment, while magic.sh holds the script we want to run inside the container.

Here’s an overview of our Dockerfile:

# Use an Ubuntu 20.10 base image
FROM ubuntu:20.10

# Update package repositories
RUN apt-get update

# Create a directory and copy the script into it
WORKDIR /tmp/scripts
COPY magic.sh .

# Set the default command to run the script
CMD [ "bash", "./magic.sh" ]

Our Dockerfile is minimal, based on Ubuntu 20.10, and updates the package repositories. It creates a directory, /tmp/scripts, and copies the magic.sh script into it. The default command is to run the magic.sh script.

Building the Image

To build the Docker image, use the following command:

docker image build -t ubuntu_scripts:v1 .

This command creates a Docker image tagged as ubuntu_scripts:v1.

Running the Container

Now, let’s run the container and observe its behavior:

docker container run -d --name myscripts1 ubuntu_scripts:v1

This command launches a detached container with the name myscripts1 using the ubuntu_scripts:v1 image.

Accessing the Container

To access the container’s terminal, use the following command:

docker exec -it myscripts1 bash

This command runs an interactive bash shell in the container.

Running the Script

Inside the container, if you run ./magic.sh, you’ll observe the script’s default behavior: it displays the message “Hello World!” five times with a one-second pause between each message.

Customizing the Container with Environment Variables

To customize the container’s behavior using environment variables, we’ll make changes to the script and Dockerfile. Specifically, we’ll use two environment variables: MY_COUNTER and MY_MESSAGE.

In the modified magic.sh script:

#!/bin/bash

# Use environment variables to customize behavior
for ((i=1; i<=$MY_COUNTER; i++))
do
  echo "$MY_MESSAGE"
  sleep 1
done

The script now uses these environment variables to determine how many times it should print a message and the content of that message.

In the Dockerfile, we make no changes.

Building Version 2 of the Image

We create a new version of the image with the modified magic.sh script:

docker image build -t ubuntu_scripts:v2 .

Running a Container with Environment Variables

Now, let’s run a container with the ability to customize its behavior using environment variables:

docker container run -d --name myscripts2 -e MY_COUNTER=10 -e MY_MESSAGE="Welcome to CodingGears.io" ubuntu_scripts:v2

This command launches a detached container named myscripts2 using the ubuntu_scripts:v2 image and sets the MY_COUNTER and MY_MESSAGE environment variables.

Access the container’s terminal with:

docker exec -it myscripts2 bash

And run ./magic.sh. You’ll see that the script behaves according to the values you set for the environment variables. In this case, it prints the message “Welcome to CodingGears.io” ten times.

Leveraging Host Environment Variables

You can also pass environment variables from your host machine to the container. For example, let’s create two environment variables MY_COUNTER and MY_MESSAGE on the host:

export MY_COUNTER=3
export MY_MESSAGE="Docker is easy to learn!"

You can now launch another container with these variables:

docker container run -d --name myscripts3 -e MY_COUNTER -e MY_MESSAGE ubuntu_scripts:v2

The MY_COUNTER and MY_MESSAGE values from your host machine are automatically injected into the container.

Access the container and run ./magic.sh to see the custom behavior.

Conclusion

In this hands-on Docker session, we explored the significance of environment variables in customizing container behavior. By passing environment variables, you can easily adapt containers to your needs, making them highly versatile and adaptable to different scenarios.

Whether you’re developing a web application, managing databases, or running complex services, understanding and using environment variables is a fundamental skill for mastering Docker. It allows you to streamline the deployment of containers with minimal manual intervention.

Now that you’ve learned how to harness the power of environment variables in Docker, you’re well-equipped to tailor your containers to your specific requirements and take your Docker skills to the next level. Experiment, adapt, and deploy with confidence!

Thank you for joining this Docker journey, and stay tuned for more exciting tutorials in the world of containerization. See you in the next lecture!

Leave a Comment

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

Scroll to Top