/** * Azure Container Instance sample for managing container instances with Azure File Share mount. * - Create an Azure container instance using Docker image "seanmckenna/aci-hellofiles" with a mount to a new file share * - Retrieve container log content * - Delete the container group resource */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACI", 15); string aciName = SdkContext.RandomResourceName("acisample", 20); string shareName = SdkContext.RandomResourceName("fileshare", 20); string containerImageName = "seanmckenna/aci-hellofiles"; string volumeMountName = "aci-helloshare"; try { //============================================================= // Create a container group with one container instance of default CPU core count and memory size // using public Docker image "seanmckenna/aci-hellofiles" which mounts the file share created previously // as read/write shared container volume. IContainerGroup containerGroup = azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() .WithNewAzureFileShareVolume(volumeMountName, shareName) .DefineContainerInstance(aciName) .WithImage(containerImageName) .WithExternalTcpPort(80) .WithVolumeMountSetting(volumeMountName, "/aci/logs/") .Attach() .WithDnsPrefix(aciName) .Create(); Utilities.Print(containerGroup); SdkContext.DelayProvider.Delay(20000); Utilities.Log("Container instance IP address: " + containerGroup.IPAddress); //============================================================= // Check the container instance logs containerGroup = azure.ContainerGroups.GetByResourceGroup(rgName, aciName); string logContent = containerGroup.GetLogContent(aciName); Utilities.Log($"Logs for container instance: {aciName}\n{logContent}"); //============================================================= // Remove the container group azure.ContainerGroups.DeleteById(containerGroup.Id); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (Exception) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } } }
public void GetContainerLogs(IAzure azure, string containerGroupName) { IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(this.configuration.ResourceGroupName, containerGroupName); SdkContext.DelayProvider.Delay(Constants.Delay); } foreach (var container in containerGroup.Containers.Values) { // Print the container's logs this.logger.Log(LogLevel.Debug, $"Logs for container '{container.Name}':"); this.logger.Log(LogLevel.Debug, containerGroup.GetLogContent(container.Name)); this.logger.Log(LogLevel.Debug, $"End logs for container '{container.Name}'"); } }
/** * Azure Container Instance sample for managing container groups with private image repositories. * - Create an Azure Container Registry to be used for holding the Docker images * - If a local Docker engine cannot be found, create a Linux virtual machine that will host a Docker engine * to be used for this sample * - Use Docker DotNet to create a Docker client that will push an image to Azure Container Registry * - Create a new container group with one container instance from the image that was pushed in the registry */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACI", 15); string acrName = SdkContext.RandomResourceName("acr", 20); string aciName = SdkContext.RandomResourceName("acisample", 20); string saName = SdkContext.RandomResourceName("sa", 20); string dockerImageName = "microsoft/aci-helloworld"; string dockerImageTag = "latest"; string dockerContainerName = "sample-hello"; try { //============================================================= // Create an Azure Container Registry to store and manage private Docker container images Utilities.Log("Creating an Azure Container Registry"); IRegistry azureRegistry = azure.ContainerRegistries.Define(acrName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithBasicSku() .WithRegistryNameAsAdminUser() .Create(); Utilities.Print(azureRegistry); var acrCredentials = azureRegistry.GetCredentials(); //============================================================= // Create a Docker client that will be used to push/pull images to/from the Azure Container Registry using (DockerClient dockerClient = DockerUtils.CreateDockerClient(azure, rgName, region)) { var pullImgResult = dockerClient.Images.PullImage( new Docker.DotNet.Models.ImagesPullParameters() { Parent = dockerImageName, Tag = dockerImageTag }, new Docker.DotNet.Models.AuthConfig()); Utilities.Log("List Docker images for: " + dockerClient.Configuration.EndpointBaseUri.AbsoluteUri); var listImages = dockerClient.Images.ListImages( new Docker.DotNet.Models.ImagesListParameters() { All = true }); foreach (var img in listImages) { Utilities.Log("\tFound image " + img.RepoTags[0] + " (id:" + img.ID + ")"); } var createContainerResult = dockerClient.Containers.CreateContainer( new Docker.DotNet.Models.CreateContainerParameters() { Name = dockerContainerName, Image = dockerImageName + ":" + dockerImageTag }); Utilities.Log("List Docker containers for: " + dockerClient.Configuration.EndpointBaseUri.AbsoluteUri); var listContainers = dockerClient.Containers.ListContainers( new Docker.DotNet.Models.ContainersListParameters() { All = true }); foreach (var container in listContainers) { Utilities.Log("\tFound container " + container.Names[0] + " (id:" + container.ID + ")"); } //============================================================= // Commit the new container string privateRepoUrl = azureRegistry.LoginServerUrl + "/" + dockerContainerName; Utilities.Log("Commiting image at: " + privateRepoUrl); var commitContainerResult = dockerClient.Miscellaneous.CommitContainerChanges( new Docker.DotNet.Models.CommitContainerChangesParameters() { ContainerID = dockerContainerName, RepositoryName = privateRepoUrl, Tag = dockerImageTag }); //============================================================= // Push the new Docker image to the Azure Container Registry var pushImageResult = dockerClient.Images.PushImage(privateRepoUrl, new Docker.DotNet.Models.ImagePushParameters() { ImageID = privateRepoUrl, Tag = dockerImageTag }, new Docker.DotNet.Models.AuthConfig() { Username = acrCredentials.Username, Password = acrCredentials.AccessKeys[AccessKeyType.Primary], ServerAddress = azureRegistry.LoginServerUrl }); //============================================================= // Create a container group with one container instance of default CPU core count and memory size // using public Docker image "microsoft/aci-helloworld" and mounts a new file share as read/write // shared container volume. IContainerGroup containerGroup = azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPrivateImageRegistry(azureRegistry.LoginServerUrl, acrCredentials.Username, acrCredentials.AccessKeys[AccessKeyType.Primary]) .WithoutVolume() .DefineContainerInstance(aciName) .WithImage(privateRepoUrl) .WithExternalTcpPort(80) .Attach() .Create(); Utilities.Print(containerGroup); //============================================================= // Check the container instance logs SdkContext.DelayProvider.Delay(15000); string logContent = containerGroup.GetLogContent(aciName); Utilities.Log($"Logs for container instance: {aciName}\n{logContent}"); } } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (Exception) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } } }
/** * Azure Container Instance sample for managing container instances. * - Create an Azure container group with two container instances using Docker images "microsoft/aci-helloworld" and "microsoft/aci-tutorial-sidecar" * - Retrieve container log content * - Delete the container group resource */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACI", 15); string aciName = SdkContext.RandomResourceName("acisample", 20); string containerImageName1 = "microsoft/aci-helloworld"; string containerImageName2 = "microsoft/aci-tutorial-sidecar"; try { //============================================================= // Create a container group with two container instances IContainerGroup containerGroup = azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() .WithoutVolume() .DefineContainerInstance(aciName + "-1") .WithImage(containerImageName1) .WithExternalTcpPort(80) .WithCpuCoreCount(.5) .WithMemorySizeInGB(.75) .Attach() .DefineContainerInstance(aciName + "-2") .WithImage(containerImageName2) .WithoutPorts() .WithCpuCoreCount(.5) .WithMemorySizeInGB(.75) .Attach() .Create(); Utilities.Print(containerGroup); SdkContext.DelayProvider.Delay(20000); Utilities.Log("Container instance IP address: " + containerGroup.IPAddress); //============================================================= // Check the container instance logs containerGroup = azure.ContainerGroups.GetByResourceGroup(rgName, aciName); string logContent = containerGroup.GetLogContent(aciName + "-1"); Utilities.Log($"Logs for container instance: {aciName}-1\n{logContent}"); logContent = containerGroup.GetLogContent(aciName + "-2"); Utilities.Log($"Logs for container instance: {aciName}-2\n{logContent}"); //============================================================= // Remove the container group azure.ContainerGroups.DeleteById(containerGroup.Id); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (Exception) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var credentials = SdkContext.AzureCredentialsFactory .FromServicePrincipal("", //Azure Id "", // Azure secret "", // Azure Tenant AzureEnvironment.AzureGlobalCloud); Console.WriteLine("credentials done"); var azure = Azure .Configure() .Authenticate(credentials) .WithDefaultSubscription(); string containerGroupName = ""; //name of the container group Console.WriteLine("azure config"); Console.WriteLine("start ACI"); IContainerGroup containerGroup = azure.ContainerGroups.Define(containerGroupName) .WithRegion(Region.USWest2) .WithExistingResourceGroup("newdotnet") .WithLinux() .WithPrivateImageRegistry("newdotcr.azurecr.io", "newdotcr", "") //password sent in teams .WithoutVolume() .DefineContainerInstance(containerGroupName) .WithImage("newdotcr.azurecr.io/newdotcr") .WithExternalTcpPort(80) .WithCpuCoreCount(1) .WithMemorySizeInGB(2.5) .WithEnvironmentVariable("SOURCECONTROL", "GitHub") .WithEnvironmentVariable("TEMPLATE_NAME", "Classlib") .WithEnvironmentVariable("GITHUB_NAME", "") //user name .WithEnvironmentVariable("VSTS_NAME", " ") .WithEnvironmentVariable("REPO", "") // repo name .WithEnvironmentVariable("EMAIL", "") //email .WithEnvironmentVariable("TOKENENGINE", "") // set breakpoint in startup to grab token .WithEnvironmentVariable("DESCRIPTION", " ") .WithEnvironmentVariable("USE_TRAVIS", "false") .WithEnvironmentVariable("ENCRYPTION_ENABLED", "false") .WithEnvironmentVariable("USE_KEYVAULT", "false") .WithEnvironmentVariable("KEYVAULT_NAME", " ") .WithEnvironmentVariable("AD_ID", " ") .WithEnvironmentVariable("AD_SECRET", " ") .WithEnvironmentVariable("AES_KEY", " ") .WithEnvironmentVariable("AES_IV", " ") .WithEnvironmentVariable("HMAC_KEY", " ") .WithEnvironmentVariable("BASEURL", " ")// set up ngrok .WithEnvironmentVariable("PROJECTID", "1") .Attach() .WithRestartPolicy(ContainerGroupRestartPolicy.Never) .Create(); Console.WriteLine("ACI Created"); //Console.WriteLine(containerGroup.ToString()); Console.WriteLine($" Exit before refresh: {containerGroup.Inner.Containers[0].InstanceView.CurrentState.ExitCode.ToString()}"); int i = 0; while (containerGroup.Inner.Containers[0].InstanceView.CurrentState.ExitCode != 0) { Console.WriteLine($" Exit: {containerGroup.Inner.Containers[0].InstanceView.CurrentState.ExitCode.ToString()}"); containerGroup.Refresh(); Console.WriteLine(containerGroup.Inner.Containers[0].InstanceView.CurrentState.DetailStatus); System.Threading.Thread.Sleep(3000); i++; } Console.WriteLine(containerGroup.GetLogContent("refresh4")); Console.WriteLine(containerGroup.Inner.Containers[0].InstanceView.CurrentState.DetailStatus); Console.WriteLine(i); Console.WriteLine("done"); while (true) { } }