/// <summary> /// Creates a <see cref="FileSystemXmlRepository"/> with keys stored at the given directory. /// </summary> /// <param name="directory">The directory in which to persist key material.</param> /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param> public FileSystemXmlRepository(DirectoryInfo directory, ILoggerFactory loggerFactory) { if (directory == null) { throw new ArgumentNullException(nameof(directory)); } Directory = directory; _logger = loggerFactory.CreateLogger <FileSystemXmlRepository>(); try { if (DockerUtils.IsDocker && !DockerUtils.IsVolumeMountedFolder(Directory)) { // warn users that keys may be lost when running in docker without a volume mounted folder _logger.UsingEphemeralFileSystemLocationInContainer(Directory.FullName); } } catch (Exception ex) { // Treat exceptions as non-fatal when attempting to detect docker. // These might occur if fstab is an unrecognized format, or if there are other unusual // file IO errors. _logger.LogTrace(ex, "Failure occurred while attempting to detect docker."); } }
private static void ConfigureDockerClientServices(IServiceCollection services) { services.AddSingleton<DockerClientConfiguration>(provider => { var configuration = provider.GetRequiredService<IConfiguration>(); var dockerSection = configuration.GetSection("docker"); return new DockerClientConfiguration( dockerSection.GetValue("endpoint", DockerUtils.getDefaultDockerEndpoint()), new AnonymousCredentials(), dockerSection.GetValue("timeout", TimeSpan.Zero)); }); services.AddTransient<IDockerClient>(provider => { var dockerClientConfiguration = provider.GetService<DockerClientConfiguration>(); return dockerClientConfiguration.CreateClient(); }); }
public static void MyClassInitialize(TestContext testContext) { DockerComposeFileName = "docker-compose452AppOn462StatusMonitor.yml"; AppNameBeingTested = TestConstants.WebAppName; VersionPrefix = "rddp"; VersionPrefixSql = "rddp"; if (!Apps.ContainsKey(AppNameBeingTested)) { Apps.Add(AppNameBeingTested, new DeployedApp { ikey = TestConstants.WebAppInstrumentationKey, containerName = TestConstants.WebAppContainerName, imageName = TestConstants.WebAppImageName, healthCheckPath = TestConstants.WebAppHealthCheckPath, flushPath = TestConstants.WebAppFlushPath }); } // Forcefully remove the image to ensure SM gets installed properly. DockerUtils.RemoveDockerImage(Apps[AppNameBeingTested].imageName, true); DockerUtils.RemoveDockerContainer(Apps[AppNameBeingTested].containerName, true); MyClassInitializeBase(); }
/** * 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 App Service sample for deploying from an Azure Container Registry. * - 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 Java to create a Docker client that will push/pull an image to/from Azure Container Registry * - Pull a test image from the public Docker repo (tomcat:8-jre8) to be used as a sample for pushing/pulling * to/from an Azure Container Registry * - Deploys to a new web app from the Tomcat image */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACR", 15); string acrName = SdkContext.RandomResourceName("acrsample", 20); string saName = SdkContext.RandomResourceName("sa", 20); string appName = SdkContext.RandomResourceName("webapp", 20); string appUrl = appName + ".azurewebsites.net"; Region region = Region.USWest; string dockerImageName = "tomcat"; string dockerImageTag = "8-jre8"; string dockerContainerName = "tomcat-privates"; 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) .WithNewStorageAccount(saName) .WithRegistryNameAsAdminUser() .Create(); Utilities.Print(azureRegistry); var acrCredentials = azureRegistry.ListCredentials(); //============================================================= // 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 + "/samples/" + dockerContainerName; Utilities.Log("Commiting image at: " + privateRepoUrl); var commitContainerResult = dockerClient.Miscellaneous.CommitContainerChanges( new Docker.DotNet.Models.CommitContainerChangesParameters() { ContainerID = dockerContainerName, RepositoryName = privateRepoUrl, Tag = "latest" }); //============================================================= // Push the new Docker image to the Azure Container Registry var pushImageResult = dockerClient.Images.PushImage(privateRepoUrl, new Docker.DotNet.Models.ImagePushParameters() { ImageID = privateRepoUrl, Tag = "latest" }, new Docker.DotNet.Models.AuthConfig() { Username = acrCredentials.Username, Password = acrCredentials.Passwords[0].Value, ServerAddress = azureRegistry.LoginServerUrl }); //============================================================ // Create a web app with a new app service plan Utilities.Log("Creating web app " + appName + " in resource group " + rgName + "..."); IWebApp app = azure.WebApps.Define(appName) .WithRegion(Region.USWest) .WithExistingResourceGroup(rgName) .WithNewLinuxPlan(PricingTier.StandardS1) .WithPrivateRegistryImage(privateRepoUrl + ":latest", "http://" + azureRegistry.LoginServerUrl) .WithCredentials(acrCredentials.Username, acrCredentials.Passwords[0].Value) .WithAppSetting("PORT", "8080") .Create(); Utilities.Log("Created web app " + app.Name); Utilities.Print(app); // warm up Utilities.Log("Warming up " + appUrl + "..."); Utilities.CheckAddress("http://" + appUrl); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + appUrl + "..."); Utilities.Log(Utilities.CheckAddress("http://" + appUrl)); } } 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 Registry sample for managing container registry. * - 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/pull an image to/from Azure Container Registry * - Pull a test image from the public Docker repo (hello-world:latest) to be used as a sample for pushing/pulling * to/from an Azure Container Registry * - Create a new Docker container from an image that was pulled from Azure Container Registry */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACR", 15); string acrName = SdkContext.RandomResourceName("acrsample", 20); string saName = SdkContext.RandomResourceName("sa", 20); Region region = Region.USEast2; string dockerImageName = "hello-world"; string dockerImageTag = "latest"; string dockerContainerName = "sample-hello"; string dockerImageRelPath = "samplesdotnet"; 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) .WithNewStorageAccount(saName) .WithRegistryNameAsAdminUser() .Create(); Utilities.Print(azureRegistry); RegistryListCredentials acrCredentials = azureRegistry.ListCredentials(); //============================================================= // 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 + "/" + dockerImageRelPath + "/" + 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.Passwords[0].Value, ServerAddress = azureRegistry.LoginServerUrl }); //============================================================= // Verify that the image we saved in the Azure Container registry can be pulled and instantiated locally var pullAcrImgResult = dockerClient.Images.PullImage( new Docker.DotNet.Models.ImagesPullParameters() { Parent = privateRepoUrl, Tag = dockerImageTag }, new Docker.DotNet.Models.AuthConfig() { Username = acrCredentials.Username, Password = acrCredentials.Passwords[0].Value, ServerAddress = azureRegistry.LoginServerUrl }); Utilities.Log("List Docker images for: " + dockerClient.Configuration.EndpointBaseUri.AbsoluteUri); 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 createAcrContainerResult = dockerClient.Containers.CreateContainer( new Docker.DotNet.Models.CreateContainerParameters() { Name = dockerContainerName + "fromazure", Image = privateRepoUrl + ":" + dockerImageTag }); Utilities.Log("List Docker containers for: " + dockerClient.Configuration.EndpointBaseUri.AbsoluteUri); 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 + ")"); } } } 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 DeterminesFolderIsMounted(string directory) { Assert.True(DockerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab)); }
/** * Azure Container Registry sample for managing container registry with webhooks. * - Create an Azure Container Registry and setup couple Webhooks to be triggered on registry related actions (push, delete) * - 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/pull an image to/from Azure Container Registry * - Pull a test image from the public Docker repo (hello-world:latest) to be used as a sample for pushing/pulling * to/from an Azure Container Registry * - List the container registry webhook event notifications after pushing a container image to the registry */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACR", 15); string acrName = SdkContext.RandomResourceName("acrsample", 20); string saName = SdkContext.RandomResourceName("sa", 20); Region region = Region.USEast2; string dockerImageName = "hello-world"; string dockerImageTag = "latest"; string dockerContainerName = "sample-hello"; string dockerImageRelPath = "samplesdotnet"; string webhookName1 = "webhookbing1"; string webhookName2 = "webhookbing2"; string webhookServiceUri1 = "https://www.bing.com"; string webhookServiceUri2 = "https://www.bing.com"; 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() .DefineWebhook(webhookName1) .WithTriggerWhen(WebhookAction.Push, WebhookAction.Delete) .WithServiceUri(webhookServiceUri1) .WithTag("tag", "value") .WithCustomHeader("name", "value") .Attach() .DefineWebhook(webhookName2) .WithTriggerWhen(WebhookAction.Push) .WithServiceUri(webhookServiceUri2) .Enabled(false) .WithRepositoriesScope("") .Attach() .WithTag("tag1", "value1") .Create(); Utilities.Print(azureRegistry); //============================================================= // Ping the container registry webhook to validate it works as expected IWebhook webhook = azureRegistry.Webhooks.Get(webhookName1); webhook.Ping(); var webhookEvents = webhook.ListEvents(); Utilities.Log($"Found {webhookEvents.Count()} webhook events for: {webhook.Name} with container service: {azureRegistry.Name}/n"); foreach (var webhookEventInfo in webhookEvents) { Utilities.Log($"\t{webhookEventInfo.EventResponseMessage.Content}"); } //============================================================= // Create a Docker client that will be used to push/pull images to/from the Azure Container Registry var acrCredentials = azureRegistry.GetCredentials(); 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 + "/" + dockerImageRelPath + "/" + 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 }); //============================================================= // Gets the container registry webhook after pushing a container image and lists the event notifications webhook = azureRegistry.Webhooks.Get(webhookName1); webhookEvents = webhook.ListEvents(); Utilities.Log($"Found {webhookEvents.Count()} webhook events for: {webhook.Name} with container service: {azureRegistry.Name}/n"); foreach (var webhookEventInfo in webhookEvents) { Utilities.Log($"\t{webhookEventInfo.EventResponseMessage.Content}"); } } } 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"); } } }