Powershell 模块导入不会保留在 docker 容器映像中

Powershell 模块导入不会保留在 docker 容器映像中

我在 php debian docker 镜像中安装了 powershell,现在我在 docker 镜像中安装 PowerCLI 模块以访问 vsphere 信息并使用 laravel 显示。问题出在 PowerCLI 安装上。Powershell 似乎没有保留已安装和导入的模块。当我导入模块并使用时RUN pwsh -c connect-viserver,它似乎在 docker 镜像中工作。但是当我在 laravel 容器中调用 cmdlet 时$process = new Process(['pwsh', '-c','Connect-VIServer', 'SERVERNAME']);失败了。我通过访问容器检查它是否已导入 powershelldocker exec -it app bash但模块未安装。我手动将模块保存在文件夹 powershell/Modules 中并将其添加到 $env:$PSModulePath

我不明白我错过了什么。

这是我的 docker 文件。

FROM php:7.4-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
RUN pwd
# Set working directory
WORKDIR /var/www
RUN pwd

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libonig-dev \
    locales \
    libzip-dev \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    wget \
    apt-utils

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl mysqli
RUN docker-php-ext-configure gd --enable-gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-enable mysqli

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

#########################
RUN pwd
# Download the Microsoft repository GPG keys
RUN wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
RUN dpkg -i packages-microsoft-prod.deb

# Update the list of products
RUN apt-get update

# Install PowerShell
RUN apt-get install -y powershell

# Start PowerShell
#RUN pwsh

# Allow installation from PSGallery
SHELL ["pwsh", "-command"]

RUN Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

#RUN Install-Module VMware.VimAutomation.Core -Confirm:$false
#RUN Import-Module VMware.VimAutomation.Core; Get-Module
#RUN Set-PowerCLIConfiguration -ParticipateInCEIP $false -Confirm:$false
#RUN Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
##RUN connect-viserver 10.21.24.9


RUN Get-Module -ListAvailable VMware.VimAutomation.Core | Import-Module
RUN $env:PSModulePath = $env:PSModulePath + ":/var/www/powershell/Modules"

RUN mkdir -p /home/www/.config/powershell
RUN touch /home/www/.config/powershell/Microsoft.PowerShell_profile.ps1
RUN echo "Get-Module -ListAvailable VMware.VimAutomation.Core | Import-Module" >> /home/www/.config/powershell/Microsoft.PowerShell_profile.ps1

RUN Get-Module
SHELL ["/bin/sh", "-c"]
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
RUN pwd
RUN ls -la && ls -la /var/www/powershell/Modules/

答案1

这可能与Debian Stretch 上的 PowerCLI:“VMware.VimAutomation.ViCore.Util10.SettingsManager”的类型初始化程序引发异常

我在使用 salt cmd.run 调用 pwsh 时遇到问题,直到将 HOME 环境设置为 /tmp

例子:

salt redacted.server cmd.run 'pwsh -nol -noni -c Import-Module VMware.VimAutomation.Core'
redacted.server:
Import-Module: The type initializer for 'VMware.VimAutomation.ViCore.Util10.SettingsManager' threw an exception.

相关内容