Obtaining PowerShell in Photon Linux container

4 minute read

With my previous blog posts about installing PowerShell on the following Linux distros:

I will be demonstrating on how you can install PowerShell 6 in Photon distro container.

Like my previous blog post, I will not be providing a break down of the steps in the Dockerfile with explainations except including comments in the Dockerfile to indicate what it is doing.

PowerShell for Photon Linux container

In this section, I will provide a Dockerfile text document file that contains all the commands to build a Photon Linux container and install PowerShell to the Photon Linux distro when you use DockerCLI docker build command.


Top


Creating the Dockerfile

Let us create a new Dockerfile text document file to begin with. Next, copy the code below and paste it into that Dockerfile text document file. Remember to save the Dockerfile file and exit from your editor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Pull Photon Linux container image
FROM photon:2.0-20181017 AS stage

# Define Args and Env needed to create links
ENV PS_INSTALL_FOLDER=/opt/microsoft/powershell/6 \
    # Define ENVs for Localization/Globalization
    DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \
    LC_ALL=en_US.UTF-8 \
    LANG=en_US.UTF-8

# Download the PowerShell binary package
ADD https://github.com/PowerShell/PowerShell/releases/download/v6.1.0/powershell-6.1.0-linux-x64.tar.gz \
    /tmp/powershell-linux.tar.gz

# Installation and configuration
RUN \
    # update package list
    tdnf distro-sync \
    # install dependencies
    && tdnf -y install \
      gzip \
      tar \
    # create powershell folder
    && mkdir -p ${PS_INSTALL_FOLDER} \
    # uncompress powershell linux tar file
    && tar zxf /tmp/powershell-linux.tar.gz -C ${PS_INSTALL_FOLDER}

# Start a new stage so we lose all the tar.gz layers from the final image
FROM photon:2.0-20181017 as build

# Copy only the files we need from the previous stage
COPY --from=stage ["/opt/microsoft/powershell", "/opt/microsoft/powershell"]

# Define Args and Env needed to create links
ARG PS_INSTALL_VERSION=6
ENV PS_INSTALL_FOLDER=/opt/microsoft/powershell/$PS_INSTALL_VERSION \
    \
    # Define ENVs for Localization/Globalization
    DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \
    LC_ALL=en_US.UTF-8 \
    LANG=en_US.UTF-8 \
    # Opt out of SocketsHttpHandler in DotNet Core 2.1 to use HttpClientHandler
    DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER=0 \
    # Set terminal to linux because there is no xterm library in Photon image
    # when docker allocate pseudo-tty
    TERM=linux

RUN \
    # update package list
    tdnf distro-sync \
    # install dependencies
    && tdnf -y install \
      # required for locales
      glibc-i18n \
      # required for uncompressing locale files in /usr/share/i18n/charmaps/
      gzip \
      # required for International Components for Unicode
      icu \
      # required for help in powershell
      less \
    # generate locale
    && locale-gen.sh \
    # create the pwsh symbolic link that points to powershell
    && ln -s ${PS_INSTALL_FOLDER}/pwsh /usr/bin/pwsh \
    # upgrade packages
    && tdnf -y upgrade \
    # clean cached data
    && tdnf clean all

# Configure entrypoint for the container
ENTRYPOINT ["pwsh", "-c"]

# Configure PowerShell as default shell for the container
CMD ["pwsh"]

Top


Building from the Dockerfile

Now, you will have to use the DockerCLI docker build command to build the Photon Linux container from the instructions in the Dockerfile text document file.

1
2
3
4
5
# Change to your working directory
cd \The\directory\where\your\Dockerfile\is\saved

# Build the container with the Dockerfile
docker build -t photon .

Top


Conclusion

Once the container build has completed, you can use the DockerCLI docker run command to run the container and look at PowerShell instead of Bash.

1
docker run --rm --interactive photon

And if you need to go back into Bash from the pwsh in the container, type bin/bash to switch into Bash.

But if you requires to run the container in bash, you can do it on the same container with the command below.

1
docker run --rm --interactive --tty photon bin/bash

And if you need to go back into PowerShell from the bash in the container, type pwsh to switch into PowerShell.

Now if you happen to have VMware vSphere Integrated Containers environment and is interested in deploying VMware Photon OS container with PowerShell Core 6 to manage your VMware vSphere environment using PowerShell, you can try installing VMware.PowerCLI PowerShell module from PowerShell Gallery.

After installing VMware.PowerCLI module in the Photon container you created just now, you can start managing that environment like this example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Install VMware.PowerCLI PowerShell module
Install-Module `
     -Name 'VMware.PowerCLI' `
     -Scope 'CurrentUser' `
     -Force

# Import the VMware.VimAutomation.Core PowerShell module
Import-Module `
     -Name 'VMware.VimAutomation.Core'

# Get a list of commands available from the module
Get-VICommand

# Define your VMware vSphere variables
$script:Hostname = 'vsphere.myorganization.com'
$script:Username = 'admin@myorganization.com'
$script:Password = 'MyPassw0rd'

# Connect to VMware vSphere
Connect-VIServer `
     -Server $script:Hostname `
     -Credential (New-Object `
          -TypeName 'System.Management.Automation.PSCredential' `
          -ArgumentList `
               $script:Username, `
               (ConvertTo-SecureString `
                    -String $script:Password `
                    -AsPlainText `
                    -Force))

Once you are connected to your vSphere, you will get a similar output below.

1
2
3
Name                           Port  User
----                           ----  ----
vsphere.myorganization.com     443   myorganization\admin

And you can manage your VMware vSphere environment in PowerShell Core 6 from a dedicated VMware’s Photon OS Linux container within your virtualization environment.

That is it on how to get PowerShell Core 6 in this distro for VMware vSphere and Photon Linux folks.

If you find that this information useful, feel free to bookmark this or share it with your colleagues and friends.


Top


References


Top



Top