Last active
February 27, 2025 13:44
-
-
Save Guiorgy/7c983ec949b7982470db82582dc654ea to your computer and use it in GitHub Desktop.
Publish a .NET AOT project for Linux on a Windows host using Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Copyright © 2025 Guiorgy | |
This program is free software: you can redistribute it and/or modify it under | |
the terms of the GNU General Public License as published by the Free Software | |
Foundation, either version 3 of the License, or (at your option) any later | |
version. | |
This program is distributed in the hope that it will be useful, but WITHOUT | |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
FOR A PARTICULAR PURPOSE. | |
You can see the full GNU General Public License at | |
<https://www.gnu.org/licenses/> for more details. | |
#> | |
# Check whether Docker is running | |
docker version *>$null | |
if (-not $?) { | |
Write-Error "Docker is not running" | |
exit 1 | |
} | |
# Define the project parameters | |
$csproj = Get-ChildItem -Filter *.csproj | Select-Object -First 1 | |
$project = $csproj.Basename | |
$sdk_version = '9' | |
$container = "$project-net-dev-$sdk_version" | |
# Define the volume mapping | |
$host_dir = $PWD.Path | |
$container_dir = "/usr/src/${project}" | |
# Ensure that the sdk container is available and configured | |
docker container inspect "$container" *>$null | |
if (-not $?) { | |
# Ensure the sdk image is available | |
$sdk = 'mcr.microsoft.com/dotnet/sdk:$sdk_version.0' | |
docker image inspect $sdk *>$null | |
if (-not $?) { | |
Write-Output "Pulling $sdk" | |
docker pull $sdk | |
} | |
# Create the container with correct volume mappings | |
Write-Output "Creating container `"$container`"" | |
docker run -d --name "$container" -v "${host_dir}:${container_dir}" $sdk sh -c 'trap "exit 0" TERM; tail -f /dev/null & wait' | |
# Install dependencies | |
Write-Output "Installing dependencies: Clang, zlib" | |
docker exec -it "$container" bash -c 'apt-get -qq update && apt-get -qq install clang zlib1g-dev' | |
# Stop container | |
docker stop "$container" *>$null | |
} | |
try { | |
# Start container | |
docker start $container *>$null | |
# Run dotnet publish inside the container | |
Write-Output "Publishing" | |
docker exec -w "$container_dir" "$container" dotnet publish -r linux-x64 -c Release $csproj.Name | |
} finally { | |
# Stop container | |
docker stop "$container" *>$null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes
The script assumes that the script and the project to be published are both in the root directory of the solution.
If the target project is in a subdirectory, then modify the following:
$csproj
$project
to the name of the project (no spaces)$csproj.Name
argument inside thedocker exec
command to the relative path to the.csproj
file of the target project (surround it with quotes if the path contains spaces)If the script is in a subdirectory, then, allong with the above, modify the following:
$host_dir
to the path to the solution direcoryThe script assumes that the target framework is
.NET 9.0
, if this is not the case, then change the value of the$sdk_version
variable. You can find the full list of available .NET tags here.