Python and Docker - an alternative to Virtual Environments
Tags: python, docker
Published: 2022-12-28 00:21:07

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.

**Problem:**
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31aa6pxbluit2xag2oag.jpg)|![Anaconda Navigator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7k0bukxb7lv2s4ghmk2.png)|![Python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjuqnju2rdu12ifqlpgh.png)|
 


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.

**Solution:**
Breaks are always good, especially when writing code....

![Take a break](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxwie9ie5t5f91jobkym.jpeg)

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

![Docker Icon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86zfvwhcmxaibmi5rz7k.png)

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
WORKDIR /app
COPY requirements.txt requirements.txt
COPY AppConfig.py AppConfig.py
COPY Stock.py Stock.py
COPY StockTweet.py StockTweet.py
COPY StockTwitterFeeder.py StockTwitterFeeder.py 
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/StockerTwitterFeeder.py"]
```

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.

 
**Conclusion:**
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.