Scripting Docker with Python for DevOps

Scripting Docker with Python for DevOps

Containers for Development

Containers allow developers to have a controlled environment to test and deploy their applications. Most developers have encountered problems which they said "it works on my machine but doesn't work when it gets deployed." The idea here is to control the environment.

Need for Containers

We can contrast containers with virtual machines. Virtual Machines(VM) is the virtualization or emulation of a computer system.VMs run a complete operating system-including its own kernel but a container is different. It is an isolated lightweight silo for running an application on the host's operating system. Containers build on top of the host operating system's kernel. This, therefore, means that the host and the container operating systems must be the same.

Some Common VM Providers

These include:

  • VMware vSphere
  • VirtualBox
  • Xen
  • Hyper-V
  • KVM

Common Container Providers

  • Linux Containers
    • LXC
      • LXD
      • CGManager
  • Docker
  • Windows Server Containers

Benefits of Containers for DevOps

There are several benefits that containers have when used in devops. These includes:

  • Less Overhead - Containers are typically faster than Virtual machines.
  • Reproducibility - Containers are much easier to reproduce.
  • Immutability -By default, containers are immutable which is essential for software testing.

Docker is the predominant container that is used today. It is the big player in the industries today. Most developers focus on the use of docker for software development so that it can work exactly the same when it is deployed

Scripting vs Hosting Python

As we advance, two things to think about. One is Scripting and the other is hosting.

Scripting Python

Scripting is used to automate the steps in managing Docker files, images, and containers.

Hosting Python

Hosting is what is done when Python runs in a Docker container.

Installing Docker SDK for Python

To install the docker SDK for python, we can use pip just as we do for other python modules. This is shown below:

pip install docker

We will advance further by looking at a simple example python script to automate docker.

Simple Example Script To Automate Docker

import docker

client = docker.from_env()

containers = client.containers.list()

container = client.containers.get(containers[0])

logs = container.logs()

container.stop()

From the above script, we imported docker and created a client which gets from the docker object on the docker module called from the environment. From the client, we get the list of containers. In the next line, we randomly get the first container(the first element in the list is [0]). This is being passed into a get() which returns a container object. Furthermore, we pull off the logs from the container and there is a string in python which represents the logs. Actually, this example shows the kinds of things a developer can do to automate multiple containers. The logs can be pulled out and passed into a python code in lots of places or may be submitted to a database or stored in local files and analyzed there. The next and final line in the script is used to stop the container.

Use Cases for Scripting

  • Run Images.
  • Run Commands in Containers.
  • Measure Resource Usage.
  • Manage Resource Allocation between containers.
  • Automate Log Analysis

Summary

In summary, it is imperative to note that python runs in the container when hosting python while python runs outside the container when scripting with python. Finally, we can do almost everything in Scripts that we can do from the command line. I believe this write-up gives you a basic understanding of DevOps and Build automation with python. Happy reading!.