DEV Community

Cover image for Python and Docker - an alternative to Virtual Environments
Brandon Michael Hunter
Brandon Michael Hunter

Posted on

Python and Docker - an alternative to Virtual Environments

Hey Readers,
I wanted to share with you an issue that I recently face and how I solved it. Also I would like to get your feedback as well.

I created a #Python app that will pull data from twitter and save it to a MongoDB. #Python 3.9 was used to create this app and I used #Anaconda Navigator as a package and environment manager. Once I completed the app, next I created an Azure Web Job to run app my on a set schedule.

Azure Web Jobs Anaconda Python
Azure Web Jobs Anaconda Navigator Python

As I was going through the process of creating the necessary resources to create a web job, I noticed that Azure's python support goes up to 3.6.4. My next thought was "OK, I'll just downgrade my app to 3.6.4 using Anaconda Navigator and then deploy once completed."...Simple enough right?

NO, IT WAS NOT. Next I decided to rewrite my app using virtual environments and the virtual environment setup on my machine bricked. Not sure what happened, but "venv" decided to stop working. At this point I was too tired and frustrated to uninstall and then reinstall everything, so I decided to take a break.

Breaks are always good, especially when writing code....

Take a break

The next day I came back and I stared at the docker icon on at the bottom of my screen.

Docker Icon

After 10 minutes of deep thought and debate, I said to myself....."Let's use a docker image to create a Python 3.6.4 environment where you can develop and test your app on."

Next I created an docker image that created my environment, installed the required libraries my app needed, and deployed and executed my code.

FROM python:3.6.4
COPY requirements.txt requirements.txt
RUN  python -m pip install --upgrade pip
RUN  pip install tweepy
RUN  pip install pymongo
RUN  pip install azure-keyvault-secrets azure-identity
CMD ["python", "/app/"]
Enter fullscreen mode Exit fullscreen mode

Success!!! My code compiled and executed without any issues within a Python 3.6.4 environment. After a little code clean up, I deployed my app to my newly created Azure Web Job and now things are working as I expected.

I know now Anaconda and Python virtual environment solutions are great for setting up multiple environments using different versions of Python, but sometimes in life, you just have to figure out how to push through and get things done. In my case, I decided to use Docker. Was it overkill? Maybe, but it worked for me and I'm happy with the final solution.

Please feel free to provide your feedback and\or any questions you may have.

Thank you for reading, and I hope you have a wonderful day.

Top comments (0)