How to Access Remote Containers with PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and welcome back in the last video we took a look at the debugger and how you can use it to inspect your code in a really granular level figure out exactly where the bug is and fix it now that's all fine and dandy but sometimes your code is going to act differently on different versions of powershell believe it or not there are actually sometimes breaking changes between let's say powershell version 6.1.2 and version 7.1 just as an example so what we're going to do in this video is take a look at how you can use visual studio code along with docker to actually create a powershell container that's got its own isolated file system so that you can actually easily test your code out against different versions of powershell let's jump in so here we are over in our desktop and if you just search for docker hub powershell this will pull up the repository on the docker hub for the powershell container images now in order to use containers you do need to have docker installed and if you're on the windows or mac platform you can go ahead and just install a tool called docker desktop basically in short it docker desktop is just a tool that gets you a docker environment up and running as easily as possible it spins up a linux virtual machine behind the scenes for you or it can use the windows subsystem for linux wsl and it basically connects the docker cli on your windows system to that remote environment so that you can actually run linux containers even though you're on the windows platform so it's a super cool tool but go ahead and get that installed if you don't have it but over here on the docker hub if you scroll down all the way towards the bottom here you'll notice that there is a full tag listing for powershell so it's got this link over to an alternative container image registry called mcr.microsoft.com and if you click on that link it will go ahead and give you a list of all the different powershell container images that are available now there is a massive massive list here in fact there's more than 2 000 different container images available depending on what version of powershell you want to run and which base image from docker you actually want to utilize so a lot of these are using an ubuntu base image if you want to use the windows container feature which is kind of beyond the scope of this particular video there's actually container images based on windows nano server or windows server core and things like that now in this case i'm going to go ahead and spin up a container running one of the newer versions of powershell which is version 7.2.0 preview 2. so preview 2 actually just came out a few days ago and you know locally on my system here i've actually got version 7.1.0 installed and so just as an example let's say that i would like to test out a piece of code that i've written against this preview version of powershell without actually having to worry about installing it and overwriting my local installation of 7.1.0 well this is where container images or containers in general come in very useful so i'm going to find a container image that's based on ubuntu focal fossa here which is one of the latest actually maybe i'll use yeah ubuntu focal here so that's going to be the latest version of ubuntu which is 20.04 and basically i'm going to grab the preview 2 version because that's the one that we're talking about here so basically i'm going to scan through this list of container images image tags and find one that works for me so for example i'll just grab one that's built on top of the debbie and linux so 7.2.0 preview 2 debian 11. and what i'm going to do is do docker pull and then mcr.microsoft.com powershell so that's pointing to the registry and then i need to specify the tag after that so i'll just go ahead and hit ctrl v to paste that into my terminal here and this is going to download this container image locally on my system so that i can start to launch new containers from it awesome so now that i've downloaded this container image i'm going to go ahead and spin up a brand new powershell container using that image so i'm going to do docker run dash dash rm-it and then i'm going to go ahead and just copy this container id hit enter and you'll see that it spins up a fresh powershell container here now i'm going to leave this container running but i'm going to detach from it using control p q and so now i'm detached from it but it should still be running so when i run docker ps sure enough you can see that that container is still running right here in the background so now what we're going to do is switch back over to visual studio code and actually connect to it alright so we're back in visual studio code here and one of the features that's offered by the remote containers extension for visual studio code is the ability to attach to a remote container so if you just search for attach here and look through the list of search results you should find a remote containers attached to running container option and when you select that command it's going to list out the containers that are currently running on your system now i only have a single container running right now so it's going to show that and if i hit enter or just click on it what's going to happen is it's going to pop open a new visual studio code window and down here on the lower right hand side you'll see that it's going to install some components that are necessary in order for the visual studio code to actually communicate with that container so once that installation process is completed we'll be able to write powershell code directly inside of this container all right so we're good to go here and we now are running inside of this remote container however we can also install extensions in that remote container so i'm going to hit ctrl shift x in this brand new window and if we search for powershell here really quick you can see that the powershell extension has this option that says install inside of the container so we have this powershell extension installed locally but we can also install it directly inside of that container so that we get all the features of the powershell extension inside of this containerized environment so that'll take just a moment to install and meanwhile i'm just going to go ahead and create a new script file here and let's go ahead and do something like get child item and because we're running inside of a linux container i'll do path root with just a forward slash here so now that we're running inside of this container i can actually just use the typical commands that i'm accustomed to using locally like f5 and that will go ahead and run my script also there's the ability to do debugging so if i had a function like add and i had maybe an a and a b parameter and then just return a plus b i can actually debug this as well so i'll do add one and two and then i'll go ahead and save this to the local file system as script.ps1 in the root folder and then if i hit f9 here on line number two and then hit f5 to invoke the debugger you can see that it's very similar to how i was able to invoke the debugger on my local system i'm actually now debugging inside of a container so now i've got this debug context and if i run dollar ps version table you can see that i'm now debugging against powershell version 7.2.0 preview two however if i switch back over to my local system here and just spin up a new powershell tab i haven't affected my kind of production local installation of powershell 7.1.0 so this is a really cool capability it's actually one of my favorite features about visual studio code is just having that capability of debugging your code inside of a docker container so that you can run conflicting versions of software side by side very easily i hope this has been informative for you and i'd like to thank you for viewing
Info
Channel: CBT Nuggets
Views: 641
Rating: 5 out of 5
Keywords: cbt nuggets, it training, it jobs, it certification, it learning, it education, it training program, tech learning, tech education, tech training, certification training, training courses, it career, career progression, powershell, visual studio code, powershell scripting, remote containers with powershell, powershell remote, Docker, docker powershell, powershell docker
Id: ynqH-bNNC70
Channel Id: undefined
Length: 8min 20sec (500 seconds)
Published: Thu Jul 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.