CodeWithSaar.Extensions.ContainerId 0.0.1

There is a newer version of this package available.
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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeWithSaar.Extensions.ContainerId" Version="0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeWithSaar.Extensions.ContainerId --version 0.0.1
#r "nuget: CodeWithSaar.Extensions.ContainerId, 0.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

  1. Register the services:

    services.TryAddContainerIdServices();
    
  2. 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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.2 419 6/9/2022
0.0.1 384 5/27/2022