The Nice Way To Use Docker With VSCode | by Yash Prakash | Towards Data Science

Your next project might contain a lot of complex machine learning code with numerous different libraries and their versions threatening to wreak havoc on your machine. Next comes the process of making sure your colleague can also run your app with the right dependencies installed. This brings forward a new round of headaches.
It does not have to be this way.
Developing inside a docker container can save you a lot of time, and help smooth out the process of deployment of your app in the long run. In another article, I’ve discussed the complete set of steps to manually build and run a docker image for your project. You can find it here.
But there’s a simpler way to automate the setup of Docker for your next project and it comes built-in with VSCode.
In this story, let me show you how I learned to start a new development environment within a container in a few simple steps.
Here we go 👇
The VSCode Docker extension is the backbone of all your Docker workflows. Make sure you have it installed by visiting the extensions marketplace or simply go to this URL: Docker Extension.
Now, go ahed and start up Docker Desktop on your machine.
This extension makes it trivial to build, run, and deploy containerised applications, all without leaving your code editor!
Make sure Docker Desktop is running before moving on to the next step.
The purpose of this tutorial is to demonstrate how easy it is to develop an app in a dockerised manner. Keeping this in mind, I’ll show a simple example on using FastApi to build a HelloWorld app.
To get started, make a new file called helloworld.py and paste this block of code within it:
from fastapi import FastAPIapp = FastAPI()@app.get("/")async def root():    return {"message": "Hello World"}
This will make sure we have a simple API that displays ‘Hello World’ message at the exposed URL.
Now comes the part where we play with Docker.
First, you need to make a requirements.txt file that will contain all the dependencies for the app we’re building.
Owing to the simplicity of this test application, we only have these two for now:
fastapi[all]==0.63.0uvicorn[standard]==0.13.4
Finally, press CMD + Shift + P or CTRL + Shift + P to open up the command pallete of VSCode. Type in and select “Add Docker Files...”
The below window will pop up, select the FastApi option.
This process significantly simplifies our workflow.
So, even if we hadn’t placed our FastApi dependency in the requirements.txt file, it would’ve added it anyway. Ain’t that neat?
Now go ahead and select the port option like in the below screenshot too. The default should work just fine. Change it to your requirement, if you wish.
Finally, the step to build your Docker Image is here. Go ahead and right click the Dockerfile generated and select Build Image.
You should now get the following files in your Editor:
Excellent! We can now move to the last step in order to test our API!
Now that we have the image ready, you can go ahead and switch to the Docker tab on your left hand side of VSCode sidebar.
Here, you’ll see a list of images you already have in your system. The new image we built just now will also show up here like this:
This is great. Now you only need to right click the ‘latest’ under your image name and select the Run option.
You can view the startup logs by right clicking the built container and clicking on ‘View logs’ option. This should produce the output like so:
This means our API is running.
Fire up your web browser and navigate to the above URL or simply: 0.0.0.0:8000. You should now see the Hello World message!
Great! You should now be able to build your own project centred around this very process.
This article, I hope, serves as a good starting point to get you up to speed on the convenient way to use Docker for your Python apps. I would recommend you experiment with the other features of Docker in VSCode by reading up on this official documentation on the same. I know I will. 😃
If you find this article helpful, follow me here as I do write every week, to make sure you never miss another article ever again.
Also, the codebase and resources for all my data science articles is located here. Happy learning!
Here are a couple other stories of mine that might interest you: