Enterprise Development: Rebuilding Your Image

Alexander Greene
3 min readFeb 13, 2018

Hello Developers,

Welcome to the second article in my Enterprise Development series. In the previous tutorial, we created and ran our first image from scratch. In this tutorial, we will learn how to rebuild images and provide environment variables to your image at runtime.

As covered earlier, images are immutable, meaning that once an image is created it cannot be changed. Therefore, whenever we introduce changes to our project the image must be rebuilt, otherwise the image will run outdated code. You will regularly have to update your images for change requests, bugs, and more — so it’s an important concept to learn.

In this example, let’s pretend that you have been asked to update the project created in the previous tutorial to count down from any positive number. Let’s get started!

In a rush? The source code is available here.

Useful Vocabulary:

Environment Variables: According to Wikipedia, an environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs.

Updating Your Python Script

First we need to update our Python script to accept an argument that it will count down from:

We can test these changes locally with the following command:

python ./scripts/Countdown.py 3
3 second(s) remaining...
2 second(s) remaining...
1 second(s) remaining...
Beep! Beep! Beep! 3 second(s) have passed!

The Dockerfile we created in the previous article must be updated to provide the script with the expected argument at runtime. The easiest way to pass an argument into a container is as an environment variable at runtime, which can be done with the following changes.

You may have noticed that we are running the CMD as a shell and have removed the array syntax, you can see why here.

With these changes, we are ready to update our image so it may run the script.

Rebuilding and Running Your Docker Image

We can rebuild our image using the same build command that we used in the previous article:

docker build -t countdown .
Sending build context to Docker daemon 86.02kB
Step 1/3 : FROM python:3
---> c1e459c00dc3
Step 2/3 : ADD ./scripts ./scripts
---> Using cache
---> c322fda1b871
Step 3/3 : CMD [ "python", "./scripts/Countdown.py" ]
---> Using cache
---> 735c1901de2d
Successfully built 735c1901de2d
Successfully tagged countdown:latest

The rebuilt image must be provided with a TIME environment variable at runtime. Luckily for us, the run command has a built in flag to define environment variables. The flag can be used multiple times in a single run command and follows the following format:

-e "<VARIABLE_NAME>=<VALUE>"

Let’s run the image while specifying that TIME is equal to three seconds.

docker run -t -e "TIME=3" countdown
3 second(s) remaining...
2 second(s) remaining...
1 second(s) remaining...
Beep! Beep! Beep! 3 second(s) have passed!

We can change which number the script counts down from by updating the value of TIME, as shown below:

docker run -t -e "TIME=7" countdown
7 second(s) remaining...
6 second(s) remaining...
5 second(s) remaining...
4 second(s) remaining...
3 second(s) remaining...
2 second(s) remaining...
1 second(s) remaining...
Beep! Beep! Beep! 7 second(s) have passed!

With that we have successfully update our image to count down from any specified positive number.

In this lesson, you learned how to update an image and how to pass environment variables into an image at runtime. These skills will prove invaluable as you continue to learn how to develop containerized applications. If you’re interested in learning how containers optimizes building images, I highly encourage you to read the Official Docker Documentation on Layers and Images.

I’m very excited about the progress we have made and I look forward to seeing you again soon!

I hope that you enjoyed this guide. If you have any suggestions or questions, please let me know in the comments.

--

--