Skip to content

Commit bcffc1c

Browse files
author
James Brundage
committed
feat: Simplifying Dockerfile and Improving Container.init ( Fixes 2bitdesigns#74, Fixes 2bitdesigns#75 )
1 parent 407411c commit bcffc1c

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

Container.init.ps1

+51-36
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
(this does nothing, but most likely will be used in the future)
2424
#>
2525
using namespace 'mcr.microsoft.com/powershell AS powerShell'
26-
using namespace 'ruby:3.3.5-slim-bullseye AS jekyllrb'
2726

2827
param(
2928
# The name of the module to be installed.
@@ -37,60 +36,76 @@ param(
3736
}
3837
),
3938
# The packages to be installed.
40-
[string[]]$InstallPackages = @(
41-
if ($env:InstallPackages) { $env:InstallPackages -split ',' }
42-
),
39+
[string[]]$InstallAptGet = @($env:InstallAptGet -split ','),
4340
# The modules to be installed.
44-
[string[]]$InstallModules = @(
45-
if ($env:InstallModules) { $env:InstallModules -split ',' }
46-
else { }
47-
)
48-
)
41+
[string[]]$InstallModule = @($env:InstallModule -split ','),
42+
# The Ruby gems to be installed.
43+
[string[]]$InstallRubyGem = @($env:InstallRubyGem -split ','),
4944

50-
51-
# Get the root module directory
52-
$rootModuleDirectory = @($env:PSModulePath -split '[;:]')[0]
53-
54-
# Determine the path to the module destination.
55-
$moduleDestination = "$rootModuleDirectory/$ModuleName"
56-
# Copy the module to the destination
57-
# (this is being used instead of the COPY statement in Docker, to avoid additional layers).
58-
Copy-Item -Path "$psScriptRoot" -Destination $moduleDestination -Recurse -Force
45+
# If set, will keep the .git directories.
46+
[switch]$KeepGit = $($env:KeepGit -match $true)
47+
)
5948

6049
# Copy all container-related scripts to the root of the container.
6150
Get-ChildItem -Path $PSScriptRoot |
6251
Where-Object Name -Match '^Container\..+?\.ps1$' |
6352
Copy-Item -Destination /
6453

65-
# If we have packages to install
66-
if ($InstallPackages) {
67-
# install the packages
68-
apt-get update && apt-get install -y @InstallPackages '--no-install-recommends' && apt-get clean | Out-Host
69-
}
54+
# Create a profile
55+
New-Item -Path $Profile -ItemType File -Force | Out-Null
56+
57+
if ($ModuleName) {
58+
# Get the root module directory
59+
$rootModuleDirectory = @($env:PSModulePath -split '[;:]')[0]
60+
61+
# Determine the path to the module destination.
62+
$moduleDestination = "$rootModuleDirectory/$ModuleName"
63+
# Copy the module to the destination
64+
# (this is being used instead of the COPY statement in Docker, to avoid additional layers).
65+
Copy-Item -Path "$psScriptRoot" -Destination $moduleDestination -Recurse -Force
7066

71-
# Create a new profile
72-
New-Item -Path $Profile -ItemType File -Force |
7367
# and import this module in the profile
74-
Add-Content -Value "Import-Module $ModuleName" -Force
68+
Add-Content -Path $profile -Value "Import-Module $ModuleName" -Force
69+
}
70+
7571
# If we have modules to install
76-
if ($InstallModules) {
72+
if ($InstallModule) {
7773
# Install the modules
78-
Install-Module -Name $InstallModules -Force -AcceptLicense -Scope CurrentUser
74+
Install-Module -Name $InstallModule -Force -AcceptLicense -Scope CurrentUser
7975
# and import them in the profile
80-
Add-Content -Path $Profile -Value "Import-Module '$($InstallModules -join "','")'" -Force
76+
Add-Content -Path $Profile -Value "Import-Module '$($InstallModule -join "','")'" -Force
77+
}
78+
79+
# If we have packages to install
80+
if ($InstallAptGet) {
81+
# install the packages
82+
apt-get update &&
83+
apt-get install -y @InstallAptGet '--no-install-recommends' &&
84+
apt-get clean |
85+
Out-Host
8186
}
82-
# In our profile, push into the module's directory
83-
Add-Content -Path $Profile -Value "Get-Module $ModuleName | Split-Path | Push-Location" -Force
8487

85-
# Remove the .git directories from any modules
86-
Get-ChildItem -Path $rootModuleDirectory -Directory -Force -Recurse |
87-
Where-Object Name -eq '.git' |
88-
Remove-Item -Recurse -Force
88+
if ($InstallRubyGem) {
89+
# Install the Ruby gems
90+
gem install @InstallRubyGem
91+
}
92+
93+
if ($ModuleName) {
94+
# In our profile, push into the module's directory
95+
Add-Content -Path $Profile -Value "Get-Module $ModuleName | Split-Path | Push-Location" -Force
96+
}
97+
98+
if (-not $KeepGit) {
99+
# Remove the .git directories from any modules
100+
Get-ChildItem -Path $rootModuleDirectory -Directory -Force -Recurse |
101+
Where-Object Name -eq '.git' |
102+
Remove-Item -Recurse -Force
103+
}
89104

90105
# Congratulations! You have successfully initialized the container image.
91106
# This script should work in about any module, with minor adjustments.
92107
# If you have any adjustments, please put them below here, in the `#region Custom`
93108

94109
#region Custom
95-
gem install jekyll
110+
bundle config --global silence_root_warning true
96111
#endregion Custom

Dockerfile

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
# Thank you Microsoft! Thank you PowerShell! Thank you Docker!
22
FROM mcr.microsoft.com/powershell AS powershell
33

4-
FROM ruby:3.3.5-slim-bullseye AS jekyllrb
5-
6-
# Copy the module into the container
7-
# Copy essentially everything from the PowerShell image into the final image
8-
COPY --from=powershell /usr /usr
9-
COPY --from=powershell /lib /lib
10-
COPY --from=powershell /lib64 /lib64
11-
COPY --from=powershell /bin /bin
12-
COPY --from=powershell /opt /opt
13-
144
# Set the module name to the name of the module we are building
155
ENV ModuleName=4bitcss
16-
ENV InstallPackages="build-essential","git"
6+
ENV InstallAptGet="build-essential","ruby-full","bundler","git","curl","ca-certificates","libc6","libgcc1"
7+
ENV InstallModule="ugit","PSJekyll"
8+
ENV InstallRubyGem="jekyll"
179

1810
# Copy the module into the container
1911
RUN --mount=type=bind,src=./,target=/Initialize /bin/pwsh -nologo -command /Initialize/Container.init.ps1

0 commit comments

Comments
 (0)