8 Steps to Running Your First Containerized Python Application on zCX
In this step-by-step tutorial, BMC Software’s Sujit Neb explain how to run a Docker container using zCX
In this step-by-step tutorial, I am going to share a sample Python program that launch the server exposed to port 8080 to say “Hello from Docker!” Readers should already be familiar with basic concepts related to Docker, containerization, Python and zCX.
First, we will test the concept on a Windows system, and then we will implement the same on zCX.
Step 1: Write a Python Script to Print the Docker Logo and the Text MessageWe start with coding a Python script that displays the Docker logo on the server. We’ll name the Python script ‘app.py.’ It is assumed here that the reader uses a machine running Microsoft Windows OS. The script can be saved anywhere on the local machine. We have saved our script in the documents folder under “Docker-init-python-app.”
The script will look something like this:
Figure 1. Python script code to display the Docker logo and the text message “Hello from Docker!”
The script makes use of the http.server Python module. We make use of the VS Code IDE to write this Python program, but you may choose an IDE that you are comfortable with. Here, we are using port 8080 to expose the server.
Warning: http.server is not recommended for production. It only implements basic security checks. Read more on http.server library here.
Step 2: Test the Python Script ‘app.py’To ensure that the Python script works as expected, execute the script from your Windows command prompt or any terminal you’re comfortable using. Change directory (using command ‘cd’) to the location where your script has been saved and execute the command python "app.py.”
If the script executes successfully, you should see the message “Hello from Docker!” along with the Docker logo on the web browser. (See Figure 2.)
Figure 2. Output on web browser (Microsoft Edge, in this case) after the successful execution of the Python script “app.py”
Step 3: Get the Dockerfile and the ‘compose.yaml’ File to Containerize the Python Script ‘app.py’You can write a Dockerfile manually or you can use the feature ‘docker init’’ with the folder where you have a source code present. For the purpose of our demonstration, we’ll use the ‘docker init’ command to get a relative Dockerfile and the ‘compose.yaml’ files created.
Before you do this step, ensure that you have Docker installed for Windows or any other OS you are using.
Once you run the ‘docker init’ command you will get following prompts, and you must supply a few inputs to those to create the Dockerfile and ‘compose.yaml’ files. (See Figure 3)
I have highlighted user inputs in bold in the terminal window below.
$ docker init Welcome to the Docker Init CLI! This utility will walk you through creating the following files with sensible defaults for your project: - .dockerignore - Dockerfile - compose.yaml Let's get started! ? What application platform does your project use? [Use arrows to move, type to filter] Go - suitable for a Go server application > Python - suitable for a Python server application Node - suitable for a Node server application Other - general purpose starting point for containerizing your application Don't see something you need? Let us know! Quit ? What version of Python do you want to use? (3.10.9) >> Press Enter (It uses default version of python if not supplied) ? What version of Python do you want to use? 3.10.9 ? What port do you want your app to listen on? (8000) 8080 ? What port do you want your app to listen on? 8080 ? What is the command to run your app (e.g., gunicorn 'myapp.example:app' --bind=0.0.0.0:8080)? python app.py ? What is the command to run your app (e.g., gunicorn 'myapp.example:app' --bind=0.0.0.0:8080)? python app.py CREATED: .dockerignore CREATED: Dockerfile CREATED: compose.yaml ✔ Your Docker files are ready! Take a moment to review them and tailor them to your application. WARNING: No requirements.txt file found. Be sure to create one that contains the dependencies for your application before running it. When you're ready, start your application by running: docker compose up --buildYour application will be available at http://localhost:8080
Figure 3. Output on VS Code resulting after the successful execution of the “docker init” command
Step 4: Start the Container and Run the Python Script 'app.py'Now that you have successfully created the Dockerfile and ‘compose.yaml’ files, let’s test the containerized Python app. On Windows system, I will demonstrate Docker Compose, using ‘compose.yaml’ to start the container running our Python app.py. Run the command ‘docker compose up --build’ to get the container running. Verify that using the web browser displaying an output of your application that is available at http://localhost:8080 (see Figure 4).
Figure 4. Output on VS Code and the web browser after the successful execution of the ‘docker compose up --build’ command
Step 5: Copy Files to zCXI have copied files from my Windows system to the server running a zCX instance. Files are copied under ‘/home/username/pyapp_zcx’ directory (see Figure 5). You can create these files directly on zCX server. You can see that I have copied the Dockerfile and the ‘app.py’ script. That’s enough for us to create and run the container.
Figure 5. Files copied to and present on the directory /home/ssn/pyapp_zcx
Dockerfile contents are as below (see Figure 6).
Figure 6. Dockerfile contents to launch the containerized application on zCX
Step 6: Build the Docker Image on zCXRun the command ‘docker build -t pyapp-server-zcx’ to build a Docker image (see Figure 7). Make sure to run this command where you have the Dockerfile and ‘app.py’ script present in the directory.
Figure 7. Output of the command ‘docker build -t pyapp-server-zcx’
Step 7: Run the Docker Container on zCXNow you have all required things to run your first Python application in the containerized form on zCX.
Run the command ‘docker run -d -p 8080:8080 --name pyapp-server-container-zcx pyapp-server-zcx’ to run a Docker container using the Docker image that we created in Step 6. Here, the port being used in 8080 and the Docker image used is ‘pyapp-server-zcx.’ This command will create a container with the name ‘pyapp-server-container-zcx pyapp.’ (See Figure 8.)
Figure 8. Output of the command ‘docker run -d -p 8080:8080 --name pyapp-server-container-zcx pyapp-server-zcx’
Step 8: Test and Verify the Docker Container Running on zCXVerify that the Docker container is running before you test your Python application. Run the command ‘docker ps’ to display status of the Docker containers running on the system. (See Figure 9.)
Figure 9. Output from running container that has a python application exposed on the port 8080
Now, you can run either the ‘curl localhost:8080’ or the ‘curl ipaddress-of-zcx-server:8080’ command to test your container and get the desired output (i.e., Docker logo and the text message “Hello from Docker!”) (See Figure 10.)
Figure 10. Output from running container that has a Python application exposed on the port 8080
This concludes the tutorial. To summarize, we looked at how to create Docker image and run a Docker container using Dockerfile and Docker Compose. If you don’t have Docker Compose available on zCX, then Dockerfile can be used to run your container. You can also apply these concepts and methods to similar use cases as necessary.
About the author
Sujit Neb is a staff specialist automation engineer at BMC Software and a part of the BMC AMI DevOps for Db2 product development team.
See more by Sujit Neb