|
| 1 | +# Use the official PowerShell image as the base |
| 2 | +FROM mcr.microsoft.com/powershell:lts-ubuntu-22.04 |
| 3 | + |
| 4 | +# Add a label to name your image |
| 5 | +LABEL name="dscresource-docgenerator" |
| 6 | + |
| 7 | +# Set environment variables for versions |
| 8 | +# Debian version should be the same as the base image. |
| 9 | +ENV UBUNTU_VERSION=22.04 |
| 10 | +ENV DOTNET_SDK_VERSION=8.0 |
| 11 | +ENV POWERSHELL_VERSION=7.4.5 |
| 12 | +ENV POWERSHELL_PACKAGE_REVISION=1 |
| 13 | +ENV GITVERSION_VERSION=5.* |
| 14 | + |
| 15 | +# Install necessary packages |
| 16 | +RUN apt-get update && \ |
| 17 | + apt-get install -y --no-install-recommends \ |
| 18 | + git \ |
| 19 | + wget \ |
| 20 | + curl \ |
| 21 | + unzip \ |
| 22 | + sudo \ |
| 23 | + nano \ |
| 24 | + locales \ |
| 25 | + apt-transport-https \ |
| 26 | + ca-certificates \ |
| 27 | + gpg \ |
| 28 | + gnupg2 \ |
| 29 | + software-properties-common && \ |
| 30 | + apt-get clean -y && \ |
| 31 | + rm -rf /var/lib/apt/lists/* |
| 32 | + |
| 33 | +# Install .NET SDK |
| 34 | +# hadolint ignore=DL3008 |
| 35 | +RUN wget https://packages.microsoft.com/config/ubuntu/$UBUNTU_VERSION/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \ |
| 36 | + dpkg -i packages-microsoft-prod.deb && \ |
| 37 | + rm packages-microsoft-prod.deb && \ |
| 38 | + apt-get update && \ |
| 39 | + apt-get install -y --no-install-recommends dotnet-sdk-$DOTNET_SDK_VERSION && \ |
| 40 | + apt-get clean -y && \ |
| 41 | + rm -rf /var/lib/apt/lists/* |
| 42 | + |
| 43 | +# Remove the existing powershell-lts package to prevent conflicts instead of forcing |
| 44 | +# dpkg to overwrite the existing files. Forcing dpkg is generally not recommended |
| 45 | +# as it can lead to package management issues down the line. |
| 46 | +RUN apt-get update && \ |
| 47 | + apt-get remove -y powershell-lts && \ |
| 48 | + apt-get clean -y && \ |
| 49 | + rm -rf /var/lib/apt/lists/* |
| 50 | + |
| 51 | +# Update PowerShell to the specified version |
| 52 | +RUN wget https://github.com/PowerShell/PowerShell/releases/download/v$POWERSHELL_VERSION/powershell_${POWERSHELL_VERSION}-${POWERSHELL_PACKAGE_REVISION}.deb_amd64.deb -O powershell.deb \ |
| 53 | + && dpkg -i powershell.deb \ |
| 54 | + && apt-get update \ |
| 55 | + && apt-get install -f \ |
| 56 | + && rm powershell.deb \ |
| 57 | + && apt-get clean -y \ |
| 58 | + && rm -rf /var/lib/apt/lists/* |
| 59 | + |
| 60 | +# Set the default shell to PowerShell |
| 61 | +ENV SHELL pwsh |
| 62 | + |
| 63 | +# Set up a user (optional) |
| 64 | +ARG USERNAME=vscode |
| 65 | +ARG USER_UID=1000 |
| 66 | +ARG USER_GID=$USER_UID |
| 67 | + |
| 68 | +# Explanation: |
| 69 | +# 1. groupadd: Creates a new group with the specified GID ($USER_GID) |
| 70 | +# 2. useradd: Creates a new user with the specified UID ($USER_UID) and GID ($USER_GID) |
| 71 | +# The -m flag creates a home directory for the user |
| 72 | +# 3. echo: Adds a sudoers entry for the new user, allowing them to use sudo without a password |
| 73 | +# 4. chmod: Sets the permissions of the sudoers file to be read-only (0440) |
| 74 | +# |
| 75 | +# This setup creates a non-root user with sudo privileges, which is a common |
| 76 | +# practice in Docker containers for better security and to avoid running |
| 77 | +# processes as root. |
| 78 | +RUN groupadd --gid $USER_GID $USERNAME \ |
| 79 | + && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ |
| 80 | + && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ |
| 81 | + && chmod 0440 /etc/sudoers.d/$USERNAME |
| 82 | + |
| 83 | +# Switch to non-root user |
| 84 | +USER $USERNAME |
| 85 | + |
| 86 | +# Set the working directory |
| 87 | +WORKDIR /workspace |
| 88 | + |
| 89 | +# Install GitVersion as a dotnet tool for the vscode user |
| 90 | +RUN dotnet tool install --global GitVersion.Tool --version $GITVERSION_VERSION |
| 91 | + |
| 92 | +# Add alias for gitversion, ensuring a newline before the alias |
| 93 | +RUN printf "\nalias gitversion=\"dotnet-gitversion\"\n" >> /home/$USERNAME/.bashrc && \ |
| 94 | + mkdir -p /home/$USERNAME/.config/powershell && \ |
| 95 | + echo 'New-Alias -Name gitversion -Value dotnet-gitversion' >> /home/$USERNAME/.config/powershell/Microsoft.PowerShell_profile.ps1 |
0 commit comments