Hi, my name is Luciana, I'm a
program manager for Python in Visual Studio code and in
this demo we're going to show you how you can
easily set up a dev container for your Python apps
in Visual Studio Code.
You've probably heard of Docker containers before.
For me the easiest way to explain them is to
think about "lightweight VM's",
but they are a great way to isolate the software from the
environment to make sure it works across different infrastructures.
In other words, it avoids the "it works on my
machine" phenomenon.
So in this video I'm going to be showing dev
containers,
which are these full feature development environment where I'll be
using Docker Visual Studio Code and the remote containers extension
and we're going to show you how this extension can
help you easily set up an isolated environment, since
it auto generates Dockerfile, docker-compose file as well as
VS Code settings for you,
so I'm going to jump right into the demo.
In this demo I'm going to be using a Fast
API application alongside PostgreSQL for
storing my data in the database,
so I'm going to open Visual Studio code.
And I have here my app,
which is a very simple Fast API application and I
have the Python extension installed, and if you come here
to the extensions view you can see that by looking for
Python. I also have the remote containers extension which is
the star of the show today,
so if you don't have it installed you're going to
see the install button.
You can click on it,
but I have it already installed it here. So once
you have it installed you will want to
open the command palette. For me,
it's Ctrl + Shift + P, but if you're on Mac that will
be Command + Shift + P.
And you're going to look for the "add development container
configuration files" command.
Once you click on it.
You can see that there are few configuration files I
can I can select from.
I'm going to select Python 3 and Postgres since that's
what I want to configure for my app.
And a couple of things are happening here.
So three files are created for me.
I have a dev container
json file, which is a configuration for my dev container,
it's going to set up the extensions that I want
to have installed as well as settings for VS code
and for extensions as well.
I also have a Docker compose file and we can
see that two services were defined for me,
one for my app, one for my database,
and I also have a Dockerfile is with instructions.
I do have a requirements.txt file.
And there are few lines here.
that are commented out by default,
but I can uncomment it and so I can have
my dependencies installed in the dev container
once I'm spinning this up.
I also need to configure a few environment variables here
in my docker-compose file.
So I'm going to go to do that.
And in this case I have a connection string and
a secret variable.
You typically don't want to have these things exposed publicly.
I'm doing this here because this is a very simple
app I'm working on.
It's not a real life software, if I would
make this a real life software, like deploy it somewhere
and make it safe,
I would probably use Azure Key Vault to store these
keys in a safe way, so I don't have to
exposed them publicly.
So all I'm trying to say here is don't
do this at home.
But once it's already set up,
I can go ahead and click on this notification that
was displayed by VS code to reopen this
folder in the container.
But if you miss the notification,
that's OK. You can run the command "reopen in container".
And once you do that,
VS Code is going to start
this dev container, it's going to be installing
itself as a server in the container,
so that basically means that the UI is running locally,
but VS Code itself and the debugger and Python and
all the tools that we use like linters and things
like that are running in the container.
So it gives this very nice isolated way while you're
still taking the best out of that local-feel experience
of development,
so that's great. It already started.
I can see that by checking on the bottom left
and I can check if my dependencies were installed.
And it seems they were installed nicely,
which is great, so that's awesome.
I'm going to go ahead and try to run this
application.
I'm going to open the debugger view and I'm going
to click on this,
create a launch.json file,
and then click on FastAPI.
And it's going to ask where is
the entry point for my application, which was just under
petgram.main.py,
and it generated the configuration file for the debugger
for me.
Great, so now I can go ahead and click run
and debug to get the configuration and
start running my app.
And I can open it in the browser and here we
go.
We have a very very simple app where I can
sign up or sign in,
but things are working well and this is all under
the container.
So if I come here and I do import,
oh, sorry, sys.
sys.platform it is going to say it's Linux because
I'm running a container even though I'm on Windows,
so that's pretty awesome. So that's it,
here are some resources of a few things we cover
it here so you can get started with Python in
Visual Studio code if you haven't already through that
link. You can also learn more about dev containers
in VS code by following the aka.ms/vscode-remote-containers
Link for remote containers and you can also learn more
about how you can safeguard keys and secrets with Azure
Key Vault,
which is something that I did not do here,
but I should have done if I was going to
make this a real life software.
Thank you so much for watching.
Make sure to follow us on Twitter @pythonvscode
and you can also connect with us on our
Discord. Looking forward to meeting you! See ya.