PowerShell Development on Containers (PowerShell Core)
With PowerShell V7 on the verge, the importance of building solutions that are cross-platform in PowerShell is greater than ever. One issue running tests is that your local PowerShell environment isn’t “clean.”
You install modules and software and change configurations that your target audience or servers won’t share. Your solution can act differently in those environments, and you also risk not understanding the full dependencies list and limitation of a target environment. That’s why you need a clean PowerShell environment.
Sometimes you may just want to try the new PowerShell versions or maybe test how it’s affecting your tools before making the switch. While installing a new server, whether virtual or physical is a time-consuming task, running a clean PowerShell core in a container can take two minutes or less.
What Containers Are
Containers provide the ability to run an application in an isolated environment. The isolation and security of containers allow you to run many containers simultaneously on your PC. Containers are lightweight because they don’t require the extra load of a hypervisor and run directly within your machine’s kernel. This means you can run more containers on a given hardware combination than if you were using virtual machines.
Prerequisites
Before you can run containers, you need a containers engine. The most popular one is Docker. However, you can’t use Docker on Windows 10 home edition due to Hyper-V limitations.
Docker for Windows
- Windows 10 64-bit: Pro, Enterprise or Education (1607 Anniversary Update, Build 14393 or later).
- Virtualization enabled in BIOS. Typically, virtualization is enabled by default. This is different from having Hyper-V enabled.
- CPU SLAT-capable feature.
- At least 4 GB of RAM.
Docker For Linux
- A 64-bit installation
- Version 3.10 or higher of the Linux kernel. The latest version of the kernel available for your platform is recommended.
-
iptablesversion 1.4 or higher -
gitversion 1.7 or higher - A
psexecutable, usually provided byprocpsor a similar package. - XZ Utils 4.9 or higher
- A mounted
cgroupfshierarchy; a single, all-encompassingcgroupmount point isn’t sufficient.
If you meet the prerequisites, install the Docker engine from the Docker site.
Starting with Docker
Docker uses images to build containers. A container is a runnable instance of an image. An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. In our example, the PowerShell image is based on Linux, with PowerShell installed.
1 PS /> $PSVersionTable
2
3 Name Value
4 ---- -----
5 PSVersion 6.2.1
6 PSEdition Core
7 GitCommitId 6.2.1
8 OS Linux 4.9.125-linuxkit
9 Platform Unix
10 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
11 PSRemotingProtocolVersion 2.3
12 SerializationVersion 1.1.0.1
13 WSManStackVersion 3.0
You can create, start, stop, move, or delete a container. You also can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. A container is defined by its image and any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that aren’t stored in persistent storage disappear.
Pull Docker Image
Before you can create any containers, you need to pull the image you want to use from the Docker hub repository.
Each image link is built by two parts:image URL:tag.
Image tag defines the version or configuration variation of the image.
You can find more info about them in the image repository source.
Begin with opening PowerShell and use the Docker pull command to get the image you want:
Latest Stable Edition of PowerShell
1 Docker pull mcr.microsoft.com/powershell:latest
Latest Preview Edition of PowerShell
1 docker pull mcr.microsoft.com/powershell:preview
You can choose what best fits your needs. When the Docker engine finishes downloading the image, you can move to the next step.
Creating a Container Based on PowerShell Image
Now, you can create containers based on the image and image tag you pulled.
Create a new container using the Docker run command:
1 Docker run --name ps-core --interactive --tty mcr.microsoft.com/powershell:latest
Because you used the --interactive --tty switches, when the command executes, the session is switched to the container session context.
Managing Existing Containers
Finally, when you finish your tests, you need to decide what to do with the container. The container is persistent, which means you can continue work on it, even if its stopped. All the data will stay until you delete the container. Do you want to keep it for future purpose? Or you don’t need it anymore and want to delete it?
Delete the Container
If you don’t need the container anymore, you can remove it.
Pass the ID or the name of the container to the Docker rm command:
1 Docker rm 'Container name'
If you don’t remember the container ID or Name, you can get it with the command Docker ps.
1 Docker ps
Keep The Container
If You decide to keep the container, you can re-use it at any time.
First, you need to make sure the container is running.
You can use the Docker ps command to check it.
If it’s not running, you can start it with the command Docker start.
1 Docker start 'Container Name'
Now to actively interact with it, you use the Docker attach command.
1 Docker attach 'Container Name'
Use Visual Studio Code to Connect and Develop on a Container
Now you know how to create containers with PowerShell core inside. But that PowerShell session isn’t a development environment. You want to properly develop in an environment that supports development operations like debugging, task running, version control, and more.
The Remote—Containers Extension
The Remote—Containers extension lets you attach VS Code to a running container.
The extension lets you work with VS Code as if everything were running locally on your machine, except now they’re isolated inside a container.
To install the Remote-Containers extension, open the Extensions view by pressing Ctrl+Shift+X and search for “Remote-Containers” to filter the results.
Attach VS Code to a Container
You can attach VS Code to a container in three steps:
- Press F1 to open the command pallet.
- Type “Remote-containers: Attach To Running Container…”
- Choose the Container you want to attach to.
Now you’re connected to the container. You can run your scripts, modules, or any other solution on a clean and isolated environment.
The Docker Extension
The Docker extension makes it easy to build, manage, and deploy containerized applications from VS Code.
To install the Docker extension, open the Extensions view by pressing Ctrl+Shift+X and search for “Docker” to filter the results.
After installing the extension, you can add it to the activity bar by right-clicking on the activity bar and choosing Docker.
You can use it to manage your containers, Images, and more.
Manage Containers
In the Docker view, the first box is used for containers. In this box, you can see all the containers available in your PC. Each container has a status icon next to it. Green Play is for running containers and Red Stop is for stopped Containers. By right clicking on a container, you can Attach, Start, Stop, Restart, or remove it.
Manage Images
Under the Images box, you can see which images are available on your PC. Those are the images you can use to create containers. You can expand each image to see which tags you pulled. By right-clicking on the tag of the images, you can remove it, or run a temporary instance container.
Summary
The combined abilities of VS Code as a development environment and the Docker containers as a test environment are endless. When one test environment fails, a new one rises. You may find a new world of possibilities with containers. You can try the newest preview version of PowerShell on containers. You can also try different modules from the internet, without messing up your environment. Most importantly, you can develop and test your solutions much better. Hopefully this chapter will help you start with containers and help you test your solutions better.