Enterprise Development: Your First Image

Alexander Greene
4 min readFeb 7, 2018

If you work in software development, chances are that you have heard of images and containers. Containers has taken the tech industry by storm because of their ability to allow teams to quickly package and ship code. Containers enables developers to write code once and deploy that same code on any host. Furthermore, containers saves businesses money by maximizing utilization on existing server infrastructure. Needless to say, Containers area great tool that should be in every programmer’s arsenal.

In this article, I will not be covering what containers are or how to get started with containers. If you need an overview I highly encourage you to check out the Official Docker Documentation. Instead, my goal is to help you get hands-on experience with Docker and enable you to create functional applications. With that said, I am excited to have you here and I hope that you enjoy this introductory piece!

Prerequisites

Useful Vocabulary:

Image: An immutable executable package that includes all information needed to run an application.

Container: A runtime instance of an image.

Creating Your Own Image

Before we can run a container, we need an image and before we can create an image, we need to build an application to run. Let’s create a project that will countdown from ten and then terminate. The following bash commands will build our project layout. We’ll be updating the Countdown.py file and the Dockerfile in this tutorial.

mkdir countdown;
mkdir ./countdown/scripts;
touch ./countdown/Dockerfile;
touch ./countdown/scripts/Countdown.py;
cd ./countdown;

Now that we have a project to work within, we will work on the python script that will count from ten to zero. It’s important to note that containers will run until the process it starts is completed, so our script will break out of the while loop once ten seconds have passed and allow the container to exit.

Insert the following code into the Countdown.py python script that your created earlier:

#!/usr/bin/python
from time import sleep
seconds = 10
while(seconds > 0):
print(str(seconds) + " second(s) remaining...")
seconds -= 1
sleep (1)
print("Beep! Beep! Beep! 10 second(s) have passed!")

Next, we need a container that can run our Python script. We could grab a Linux image, install Python, and run our script, but there is a better way to get a working environment. Images are often built on top of existing images, saving people from reinstalling dependencies each time they create an image. Cloud-based registry services such as Docker Hub or Quay allows developers to store and share images. A quick search for Python on Docker Hub will bring us to the official Python Image, which we will use in our project.

Now that we know which image to build from, we want to build a Dockerfile that contains the instructions on how to build our image and run our container.

Insert the following code into the Dockerfile file that your created earlier:

# Build from the latest Python image
FROM python:latest
# Add the scripts folder from our project to the image
ADD ./scripts ./scripts
# Run our Python script
CMD [ "python", "./scripts/TenSecondCountdown.py" ]

Now that we have our Dockerfile and the script we want to run, we can create an image using the build in the countdown folder:

docker build -t countdown .

The -t flag allows us to assign a human readable name to the image, useful when you have trouble remembering randomly generated strings. The . means to use the Dockerfile in this directory. After running this command, you should have a local image on your computer, which you can confirm with the images command:

docker images

Running Your Docker Container

You are now ready to run the image you have created. You can do this with the run command, we’ll add the -t tag to allocate a pseudo-tty:

docker run -t countdown

Expected Output:

10 second(s) remaining...
9 second(s) remaining...
8 second(s) remaining...
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! 10 second(s) have passed!

If you see the response above, congratulations! In this demo, you have successfully written a python script, searched Docker Hub for a Python image, created a Dockerfile, and ran your container!

In the coming articles we will start taking on larger and more complex projects that we can build and deploy containers. I hope that you are excited!

If you liked my article, claps and feedback are always appreciated!

GitHub Code:

--

--