>> Hey folks, my name is Burke.
In this video,
we're going to take a look at how to modify
the container configuration for
a project that is running with
remote containers in Visual Studio Code.
Let's go. What we have here is
a Python project that's running
inside of a devcontainer right now.
We know that because it says so down here in the left-hand corner.
In a previous video,
we added the container configuration and we
talked about the folder and said that there's two files in it,
but we didn't say what those files do.
Let's take a look at the devcontainer.json file.
This file allows you to modify and configure
certain aspects of the project and
the environment without actually touching the dockerfile.
Let's take a look at the different things in this file.
There's a name and then there's a build object.
In that object, the dockerfile is specified and
this dockerfile is actually pointing to this file right here.
Next, there's a context.
The context just specifies where your project is located.
For most people, that's the route and that's what that dot means.
But if it was located somewhere else,
you could specify that here in the context.
Then we get into args,
and args are arguments that are passed
to the docker container when docker build is actually run.
You can see here that we're passing in the variant of Python,
so if we wanted to change
the Python version, we could do that here.
We're also saying that we don't want to install Node.
In the previous video when we add the container configuration
and asked us if we wanted to install Node and we simply said no.
We could toggle that on here,
and if we did that the next time we rebuilt
the container, notably installed.
These settings block specifies
certain settings that we want to have copied into the container.
These settings here just set up
Visual Studio Code for Python development.
As you can see here, it's setting things
like the Integrated Shell to bash.
It's setting the path for Python,
turning on Linting, things like that.
These are settings that a developer would normally have to make
themselves in order to get the optimal development environment,
but we're going do that for them here.
Then there are extensions
that we want to include in the container.
Right now, just the Python extension is being included.
You should note that just because
you have an extension installed locally,
it doesn't mean it will be in the container.
Your local extensions are not automatically all in the container.
You have to specifically install extensions
in the container in order for that to work.
Then you can see the forward ports,
which we just did in the last video.
We can specify any ports that we want to forward.
Then there's this post create command
which allows us to do anything that
we'd like to do before the project actually starts up.
That's something like running the pip3 install,
which installs all of the dependencies.
Then lastly, this remote user specifies
that we want to run the container as the user VS Code.
By default, containers run as the root user.
That's usually insecure and we don't like to do that,
so you want to run it as this user here,
which is pre-baked into the container.
Let's modify certain aspects of this and see how that works.
The first thing I want to do is add in
an extension for this container.
We have a template right now,
here and this template uses something called Jinja.
Jinja is the templating language
and if you scroll down far enough, you'll see it.
But we're not getting any syntax highlighting for that.
It would be nice if we had some.
Well, we searched for Jinja,
we can find an extension to do that, which is right here.
We want to add this to our container configuration.
To do that, we're just going to right-click it and say,
add to devcontainer.json.
You can see it's been added,
and if we check by opening that file and scroll down,
you'll see it's now in the array of extensions
to be included with this devcontainer.
Now let's go ahead and forward the port.
It's best practice to do this,
to go ahead and be specific about
which ports you want to forward with the project,
instead of relying on Visual Studio Code to do that for you.
Also, if you remember from an earlier video,
we actually had to run this pip3 command from our readme.
If you jump over here to the readme,
this is the command that we had to run.
Let's just copy this and let's put it here
inside of the post create
commands so that it always run automatically.
What this means is that the developer that opens this doesn't even
have to run the command to install
the dependencies, it just works.
We've made these tweaks to our container,
it's time to rebuild it and see this in action.
Let's go ahead and do that.
Just we rebuild in container like this from the command palette.
Visual Studio Code will reopen and it will rebuild the container.
The container has been rebuilt and all of
our changes should now be baked into this new container.
We can verify the ports by going to
the Remote Containers Explorer and
seeing that port 5,000 has already been forwarded,
and that was done by this setting here.
If you remember we installed an extension here,
we can check to make sure that's installed by going to
extensions and looking at what's inside of our devcontainer.
You'll see, it's here.
Now let's go back to our index file and we should get,
we do syntax highlighting,
and that's thanks to that extension.
Then lastly, we went ahead and installed our dependencies.
Instead of having to do that step,
we should be able to just run Python app.py and
this should just work, and it does.
Again, we didn't get a notification that the port was
forwarded because we've already done that here.
If we were to click on this,
get to open it in a browser and see it running.
By using the devcontainer.json file,
we were able to accomplish and automate a few additional things.
We included an extension called the
Jinja extension that provided syntax highlighting.
We were able to automatically forward the port to make
sure that that was done and not
something the user has to worry about,
and we were able to go ahead and run the command then installs
the dependencies so that they don't have to run that step either.
All they have to do is start
the program and everything should just work.
But there are times when you're going to want to do even more,
like maybe, possibly install additional software in the container.
In the next video,
we'll take a look at how to use that other file.
The dockerfile to do exactly that.