top of page
Search

When a Network Guy Does Programming.....

Writer: Mike ReshetarMike Reshetar

This week's adventure revolved around continuing development in Python for some automation at my Cisco job. As if learning GIT, Python, and VisualCode was not enough, I decided to try my hand making a docker image. Now, if you are like me, this sounds complicated and like something that will take most of the day to figure out. Surprisingly that was not the case.

  1. I already had docker installed - that helped.

  2. After using my google-fu, I found out you needed a dockerfile (yep - call it 'dockerfile' - with no extension.... )

  3. And you need needed your source code - in my case this was in python.

In the dockerfile, I specified I was using python3.9, specified the needed commands to add the requirements for a 'pip install' and specified the command to run to launch the app.


Here are the lessons learned though:

  1. Being a bit OCD and organized, I put the dockerfile and some related information into a 'docker' directory under the GIT repository. While this seems like a good idea, turns out 'docker build -t <tag_name> .' did not respect this approach. The catch is there was no real warning or indication not having the docker build in the same directory as the source code was a problem. My google-fu failed me so I reached out to a mentor (thank you Nathan!) who came to my rescue.

  2. 'pip install -r requirements.txt' requires 'requirements.txt' to be in the docker image -- meaning you have to have in the ADD statement. Hindsight, this seems like a 'duh - no kidding', but I wasn't sure at the time if docker ran the commands 'inside' or 'outside' the container during the build process.

  3. adding the '--no-cache' option to the 'pip install' command helped reduce the image size (again - thanks to Nathan for that tip!)

  4. Make sure you set the working directory in the dockerfile, otherwise chaos might ensue!

  5. When you run the docker image from the command line, I had to add the -it options to indicate interactive and tty redirect, otherwise it ran headless. (Our application needed input from the user).

  6. To map a local directory into the container so-as to pass data back and forth between the container and the local machine, used the -v option. I tried a few others that my googling turned up, but they were complicated.....

Outside of these things, the process went surprisingly smoothly! So why do this vs. just running the source locally using python? The reasons are few but may be significant:

  1. The local machine only needs docker and does not require python to run the application

  2. The dependancy on the local version of python is removed as the docker image contains the needed python version.

  3. Removing concerns around version dependency applies to the python modules as well

  4. Using 'docker build' instead of passing around a docker image file saves space in GIT and during the transfer process.

Best part was when someone else downloaded the GIT repository, did the docker build command it it worked!



 
 
 

Recent Posts

See All

Comments


  • LinkedIn

©2021 by Reshetar's Ramblings. Created with Wix.com

bottom of page