CodeWithSaar.Extensions.ContainerId
0.0.1
See the version list below for details.
dotnet add package CodeWithSaar.Extensions.ContainerId --version 0.0.1
NuGet\Install-Package CodeWithSaar.Extensions.ContainerId -Version 0.0.1
<PackageReference Include="CodeWithSaar.Extensions.ContainerId" Version="0.0.1" />
paket add CodeWithSaar.Extensions.ContainerId --version 0.0.1
#r "nuget: CodeWithSaar.Extensions.ContainerId, 0.0.1"
// Install CodeWithSaar.Extensions.ContainerId as a Cake Addin #addin nuget:?package=CodeWithSaar.Extensions.ContainerId&version=0.0.1 // Install CodeWithSaar.Extensions.ContainerId as a Cake Tool #tool nuget:?package=CodeWithSaar.Extensions.ContainerId&version=0.0.1
Get the container id
Getting container id is sort of important for diagnostic / logging. It has been working by fetching /proc/self/cgroup
for container id until lately, cGroupV2
rolls out, and there need to be new ways to get it.
This repo is a yield of the investigation to aggregate various ways to get container ids for both cGroupV1 or cGroupV2. Shall be able to extend to support other cases as time goes by.
Get Started
docker pull saars/cid
docker run saars/cid
Output:
Container Id: 7f1477ac7aa29f3e52d8d4f25632e9df461a3855cc8cd8ad071ad94181381215
See inspect logs from the worker
- Deploy
saars/cid-worker
into a Kubernetes cluster. - Check the logs of the pod:
kubectl logs <podName>
info: CodeWithSaar.Extensions.ContainerId.CGroupV1ContainerIdProvider[0] No container id matched.
info: CodeWithSaar.Extensions.ContainerId.CGroupV2ContainerIdProvider[0] Matched. Value: 01d1ad1a7b528295f2d03619b35f995be9ddddcfd2a83c013df129aa6cc4d332
info: CodeWithSaar.ContainerId.Worker.Worker[0] Container Id: 01d1ad1a7b528295f2d03619b35f995be9ddddcfd2a83c013df129aa6cc4d332. Wait for 10 seconds.
- Notice that the first provider missed the container since it doesn't exist. But the second provider got it.
Use the NuGet Package
Add reference
dotnet add package CodeWithSaar.Extensions.ContainerId --prerelease
Use static class
string containerId = await ContainerService.GetContainerIdAsync();
Or use it with dependency injection
Register the services:
services.TryAddContainerIdServices();
Call inject and use the service:
private readonly IContainerService _containerService; class Consumer(IContainerService containerService) { _containerService = containerService ?? throw new ArgumentNullException(nameof(containerService)); } public Task ShowContainerIdAsync() { Console.WriteLine(await _containerService.GetContainerIdAsync()); }
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.