How to Access Remote Containers with PowerShell
Wouldn't it be a novel idea if you could use a Docker container within VS code? Imagine the possibilities! This is one of the things that make Visual Studio Code such a powerful code editing application. It is highly extensible, and you can access remote containers with PowerShell in VS Code.
So, how do you do this, and why would you want to? Let's discuss all that in this post.
Why Access Docker Containers in VS Code?
PowerShell was once a Windows-only application. It has been the go-to choice for Windows administration for IT admins for years because it's primarily been the only shell environment available for Windows. Likewise, PowerShell was built with Windows and other Microsoft applications in mind. PowerShell has evolved, though.
In 2016 Microsoft released PowerShell Core. PowerShell Core was released in tandem with .Net Core. Both PowerShell Core and .Net Core are cross-platform and open-source.
Of course, the version of PowerShell included with Windows has additional features that PowerShell Core does not. That's because the native version of PowerShell has more access to system components that are expected to be installed on a Windows system. Unfortunately, there are also some parts of PowerShell native that contain proprietary intellectual property. Thus, those bits cannot be made open source just yet.
That means that some features of PowerShell will not work with PowerShell Core. Likewise, because PowerShell Core is still in development, new features are being added to it all the time. Therefore, as PowerShell Core is updated, those updates may break older components.
This can be detrimental when working with scripts, too. PowerShell is also used as a scripting language. In fact, PowerShell can be more powerful than other shell environments, like Bash, because of how it handles data. Data in PowerShell can be organized and manipulated as objects. Different shell environments handle data as strings. Because of that, other shell environments require additional applications to manage data conversion and manipulation. PowerShell does not.
The way PowerShell treats data means scripts can be much more powerful. For instance, the AWS SDK requires structured data to be submitted with calls to AWS functions. PowerShell can perform these calls natively with little hassle because PowerShell can pass data objects to the AWS SDK directly. Other environments can't.
This means that developers need a proper testing environment for working with their scripts, though. In addition, since changes between versions of PowerShell Core can break things, developers will need to test their scripts before updating PowerShell Core.
One of the best ways to create a testing environment is by using Docker containers. Containers compartmentalize applications in a sandboxed environment. That way, you can both run and test various versions of applications without making changes to your native development environment on your computer. You can also segregate and store code within those containers that will not impact the rest of your system too.
For this reason, developers may want to access a remote container with PowerShell in VS Code despite VS Code being able to directly access the native version of PowerShell on a Windows machine.
An Overview of How to Access Remote Containers With PowerShell [VIDEO]
In this video, Trevor Sullivan covers how to access remote containers with PowerShell and make sure your code works everywhere you deploy it.
How to Use a PowerShell Container With VS Code
Now you need to know how to link VS Code and containers together. So, let's walk through how you connect to remote containers with PowerShell in VS Code.
Before getting started, you need to ensure that Docker is installed on your computer. Docker is an application to run containers. Containers are like portable applications built around a Linux environment. However, unlike a virtual machine, a container only has the bits and pieces required to run an individual application. Containers also share the host computer's native resources. In contrast, a VM will have a slice of those resources dedicated to it.
So, how does Docker work on Windows? Years back, Microsoft released a framework called the Windows Subsystem for Linux (WSL). WSL has evolved far enough that Windows runs a Microsoft version of the Linux Kernel directly in Windows for WSL. WSL initially only replicated calls to Linux applications in Windows, like how Wine works in Linux. Because WSL has full access to a Linux kernel and Linux subsystems, it can run Docker natively.
If you are on Linux, use your Linux distribution's application manager (Eg. Apt, Pacman, etc.) to install Docker. If you are using Windows, you will need to install it manually from the Docker website.
Once Docker is installed, go ahead and open a console window in your operating system. Then look at the Docker Hub page for PowerShell.
Notice the command to pull a container with PowerShell to your computer is docker pull mcr.microsoft.com/powershell. This is going to tell Docker to use the Microsoft repository to pull the PowerShell container.
If you scroll down the Docker Hub page for PowerShell, though, you will notice that Microsoft has a ton of different docker images built on other core operating systems. This is useful to test PowerShell in an environment as close to your production environment as possible. So, for example, if your production environment is running on Ubuntu 18, you can use a PowerShell container based on Ubuntu18.
Let's continue with that example. Let's say that we want to use a PowerShell container built on Ubuntu 18. In that case, use the same command above but replace the 'PowerShell' at the end of that command with 'lts-ubuntu-18.04'.