Containers and where Python fits in with containers
Containers are small packages of software that serve as a unique runtime containing all of the necessary resources to run a small facet of an application, or sometimes the entire application itself.
The containers themselves are written in Go, as is the container orchestration service, Kubernetes. Those parts do not require Python unless the application code itself is written in Python. Where Python comes in handy is as the glue between the various containerized services. Orchestrating the orchestration, if you will.
Now, we are going to learn about the role that Python plays in the overall container picture. Python, as always, has a lot of libraries that support the use of containers and Kubernetes, and we will explore some of these libraries and how using Python can simplify your DevOps work where these important infrastructural elements are concerned.
Simplifying Docker administration with Python
Keeping Docker images together and organized is tricky. It’s why they invented Kubernetes. But Kubernetes itself is tricky. This leaves two gaps:
- First, when there are multiple Docker images but complete orchestration with Kubernetes is not required
- Second, when Kubernetes APIs need to be frequently called or the cluster needs to be frequently updated
For both of these purposes, Python can be a useful tool. Remember this is about support, not refactoring. We’re the auxiliary player here.
So, in this section, we will look at an example of how to use Python to administer multiple containers.
We will write a script to get a specific Docker image and create a container for it. This script can be rerun to do the same thing over and over again. You may also use the restart command if a container malfunctions. Now let’s look at the code to pull Docker images and start a container:

Figure 4.15 – Code to pull Docker images and start a container
It’s simple, but that’s the key to it. Its simplicity provides building blocks to improve upon.
But even if we keep it simple, sometimes the use of containers gets complex, which is where Kubernetes comes in. With Kubernetes comes its own challenges. These challenges can also be simplified and managed using Python.