Docker’ize’ Python

What is Docker?

Docker is a computer program that performs operating-system-level virtualization, also known as “containerization”. It was first released in 2013 and is developed by Docker, Inc. source: Wikipedia

How Docker works?

Docker containers wrap up software and its dependencies into a standardized unit for software development that includes everything it needs to run: code, runtime, system tools, and libraries. This guarantees that your application will always run the same and makes collaboration as simple as sharing a container image.
source: www.docker.com

Why Docker?

Docker unlocks the potential of your organization by giving developers and IT the freedom to build, manage and secure business-critical applications without the fear of technology or infrastructure lock-in. source: www.docker.com

Background:

We have a Python script which collects the VMAX capacity report using Unisphere REST API. For this script to work, it needs access to port 8443 and certain IP addresses which are in different network and data centers.
I must thank the complex network infrastructure and TAT to fix routing requests. Because that’s WHY our team decided to dockerize the Python script which would be scheduled to run on a timely manner using CRON until network issues are addressed.

Below are the steps followed to dockerize the python script

0. Install Docker: Use this link and follow the instructions to install Docker on Debian OS.

1. Create a file named ‘Dockerfile’ without extension and please remember the camel case ‘D’
2. Add the following to the Dockerfile

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./
COPY vec_vmax_unisphere_ips.csv ./
RUN pip install --no-cache-dir -r requirements.txt

CMD [ "python", "./vmax_restapi_capacity_report_dock.py", "-u", "username", "-p", "password", "-k",  "xoxb-22320000-2342343434-ABVXFWERWR#$#$" ]

FROM python:3 – Use Python 3 version image

WORKDIR /usr/src/app – Working Directory

COPY requirements.txt ./ – requirements.txt is a file which has a list of modules/libraries required to execute the script. We can term them as dependencies to be satisfied before running the script

#requirements.txt sample file
requests==2.21.0
requests-cache==0.4.13
requests-ntlm==1.1.0
numpy==1.16.1
pandas==0.24.1
slacker==0.12.0

COPY vec_vmax_unisphere_ips.csv – Source file having a list of VMAX ID’s, IPs of Unisphere server and version information

RUN pip install –no-cache-dir -r requirements.txt  -Install modules/libraries listed in a requirements.txt file

CMD [ “python”, “./vmax_restapi_capacity_report_dock.py”, “-u”, “username”, “-p”, “password”, “-k”, “xoxb-320000-2342343434-ABVXFWERWR#$#$”] – command to execute the script with arguments REST API username, password and slack bot’s token. We have a customized slack bot to send the python script generated excel report to respective Slack Channel. BTW, What is slack?

3. Build the docker image – $sudo docker build -t my-python-app .

4. Run the Python script or App – $sudo docker run -it –rm –name my-running-app my-python-app

5. Check whether script execution succeeded or not by downloading the output from slack. If any execution errors, then duckduckgo and stackoverflow are the saviours 🙂

If you’re interested to replicate the same at your end, please reach out to me. I believe REST API method of generating the report is better than using SYMCLI server.

References:

  1. https://docs.docker.com/samples/library/python/
  2. https://www.itzgeek.com/how-tos/linux/debian/how-to-install-docker-on-debian-9.html
  3. https://en.wikipedia.org/wiki/Docker_(software)
  4. https://github.com/docker-library/docs/tree/master/python/

Please continue to stop by this blog and share your comments below.

Up Next – Manage Alerts of VMAX array using REST API

Advertisements

Written by Vinay

Proud and Tolerant Indian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s