Create a project

To create a Docker application, you need an application and a (Docker) image file. In this example we’ll use a very simple Python script as the application, so we have to create 2 files:

  • A “py” file (the python script that will contain the code to be executed)
  • A ‘Dockerfile’ file (the Docker file that will contain the necessary commands to create the environment and launch the application).

Python files are called modules and they are identified by the . py file extension. A module can define functions, classes, and variables. In this case, there is going to be only one Python script and it will be contained in the “main” module, in the main.py file.

The Dockerfile is simply a text-based script of commands that are used to create a container image. And launch the Python application. To create this file, you can use a text-editor (like notepad) but you need to save the file as “Dockerfile” (include the quotes) and no extension:

Writing the Python Script

Once the files are created, you can add the Python script to the “main.py” file:

The simple script will display a message and then list of all files and directories in the current directory using the listdir and Print Python functions.

Adding Docker Commands

Now you have to update the Dockerfile. What we want to do is to launch the Python application (run the Python script). To accomplish this, the Dockerfile must contain all the dependencies necessary to launch Python.

The first step to take when you create a Docker file is to access the DockerHub website. This site contains many pre-designed images to save your time (for example: all images for linux or code languages). In our case, we will type ‘Python’ in the search bar. The first result is the official image created to execute Python. That’s what we need.

Creating a Docker image

Once the Python script and the Dockerfile are both ready, all you have to do is create an image to contain the application. You do this with the docker build command. The command uses the -t parameter to set the name of the image (jimsdockerfile). Take note that the dot “.” Is required to use the Dockerfile in the local directory

docker build -t jimsdockerfile .

To run this command (and other docker commands) you need to open a terminal session using Windows PowerShell (or similar). Notice I had to change the directory to the folder where my files reside (KBDocker):

Running the Docker image

Once the image has been created, the application is ready to be initiated. This is done with the docker run command,  stating the name of the image after “docker run”:

docker run jimsdockerfile

You should see the following displayed in the terminal:

Something Different

The Python script (application) I used isn’t very interesting especially since its terminal based. To make it a little more fun, we can edit the main.py file changing it to the following sample script that uses Termgrah (a command-line tool that draws basic graphs in the terminal, written in Python):

Rebuilding and Rerunning

For this script to work, we need to add 1 new command to our Dockerfile. That is shown below:

RUN pip install requests termgraph

This uses pip (the package manager for Python packages) to install the termgraph Python library which the script requires. Once both files are saved, I can again use the docker build command (to recreate the docker image) and the docker run command (to run the Python application). The following output should be generated: