Container management can be a daunting task, but if you're familiar with PowerShell, the learning curve might not be as steep. You can install the PowerShell Docker module to manage Windows container components.
You probably already know that you can manage almost all Windows OS features and roles using PowerShell. Microsoft developers designed PowerShell cmdlets to specifically manage Hyper-V and the VMs running on it. However, the underlying architecture of Docker differs from Hyper-V, so you can't apply those same cmdlets to containers. The Docker Command-line interface (CLI) is the primary method for managing Docker components, but you can also use PowerShell for Docker. PowerShell for Docker uses the Docker REST API to connect to Docker Engine.
Benefits of using PowerShell Docker over Docker CLI
There are a few reasons why you might use PowerShell cmdlets over Docker CLI. One of the reasons is that Docker CLI syntaxes are complex. For example, to create a network stack using Docker CLI, you must use several parameters and also make sure the complete command syntax is lowercase. Secondly, PowerShell offers greater flexibility and helps simplify command use. To get help, simply run the Get-Help command in the PowerShell Docker module. Finally, PowerShell is a long-established technology, so you most likely have more experience with it than Docker CLI.
Install the PowerShell for Docker module
To install PowerShell for Docker, you must use the NuGet package manager. Execute the PowerShell command below on a Windows host machine running Windows Server 2016 to install the NuGet package:
Install-PackageProvider –Name NuGet –MinimumVersion 184.108.40.206 –Force
Once the above command processes, the NuGet package installs on the machine, as shown in Figure A below:
The next step is to register the PowerShell for Docker repository using the Register-PSRepository PowerShell cmdlet as shown in the command below:
Register-PSRepository –Name DockerPS–Dev –SourceLocation https://ci.appveyor.com/nuget/docker-powershell-dev
To make sure the PowerShell Docker repository registered successfully, execute the following PowerShell command:
Get-PSRepository –Name DockerPS-Dev
After registering the repository, install the PowerShell for Docker module with the following command:
Install-Module –Name Docker –Repository DockerPS-Dev
If you only need to install the PowerShell Docker module for the current user, add the -CurrentUser parameter as shown below:
Install-Module –Name Docker –Repository DockerPS-Dev –Scope CurrentUser
Validate the module installation
To validate the PowerShell Docker installation, execute the Get-InstalledModule –Name Docker command. You should see the version information for the installed module, as shown in Figure B below:
Before you use the module, import it to the current PowerShell session using the Import-Module –Name Docker command. To validate that it loaded successfully, run the Get-Module –Name Docker command.
Get existing containers and create new containers
Now, you can use PowerShell Docker cmdlets to work with containers. To list all the containers available, execute the Get-Container PowerShell cmdlet, as shown in Figure C below:
To create a container, use the New-Container PowerShell cmdlet. An example PowerShell script appears below, for testing purposes:
$isolation = [Docker.PowerShell.Objects.IsolationType]::HyperV
$container = New-Container -Id "$ImageName" -Isolation $isolation -Command @("cmd", "/c", "echo Worked")
$container | Start-Container
$container | Wait-Container
To get a list of the all the PowerShell cmdlets available to work with containers, execute the Get-Module –Module Containers command in a PowerShell window.