public async Task <string> RunContainer(Docker.DotNet.Models.AuthConfig authConfig, string dockerRepository, string imageTag, IList <string> command, IDictionary <string, string> environment, string hostWorkingDirectory) { List <string> env = environment.Select(kv => kv.Key + "=" + kv.Value).ToList(); env.Add("LC_ALL=en_EN.utf8"); env.Add("TERM=dumb"); var console = new DockerConsole(this, dockerRepository); SimulatorConsole.Instance.AddTab(console); SimulatorConsole.Instance.ChangeTab(console); var dockerImage = $"{dockerRepository}:{imageTag}"; var createConfig = new Docker.DotNet.Models.ImagesCreateParameters { FromImage = dockerRepository, Tag = imageTag }; Debug.Log($"Pulling image {dockerRepository}:{imageTag}"); await dockerClient.Images.CreateImageAsync(createConfig, authConfig, console); var createParams = new Docker.DotNet.Models.CreateContainerParameters() { Hostname = "", Domainname = "", Image = dockerImage, Cmd = command, User = "******", AttachStdout = true, AttachStderr = true, AttachStdin = true, Tty = true, OpenStdin = true, StdinOnce = true, HostConfig = new Docker.DotNet.Models.HostConfig { Binds = new[] { hostWorkingDirectory + ":/scenarios" }, NetworkMode = "host", RestartPolicy = new Docker.DotNet.Models.RestartPolicy { Name = Docker.DotNet.Models.RestartPolicyKind.No, MaximumRetryCount = 0 }, AutoRemove = false, Privileged = false, PublishAllPorts = false, ReadonlyRootfs = false, }, Env = env, WorkingDir = "/scenarios", }; Debug.Log("Creating container"); var response = await dockerClient.Containers.CreateContainerAsync(createParams); console.CompleteProgress(); Debug.Log("created container " + response.ID + " " + string.Join(", ", response.Warnings)); var container = await dockerClient.Containers.InspectContainerAsync(response.ID); Debug.Log("Attaching to container"); var containerAttachParameters = new Docker.DotNet.Models.ContainerAttachParameters { Stream = true, Stderr = true, Stdin = true, Stdout = true, }; var containerStream = await dockerClient.Containers.AttachContainerAsync(container.ID, true, containerAttachParameters); console.WriteLine($"Image: {dockerImage}"); console.WriteLine($"Created container: {container.ID} {container.Name}"); console.WriteLine($"working directory: {hostWorkingDirectory}"); console.WriteLine($"environment: {string.Join("; ", env)}"); console.WriteLine("---------------------------------"); console.ReadStream(containerStream); var startParams = new Docker.DotNet.Models.ContainerStartParameters(); containers.Add( container.ID, new ContainerData { imageName = dockerImage, console = console, }); Debug.Log("Start container"); var result = await dockerClient.Containers.StartContainerAsync(container.ID, startParams); if (!result) { throw new Exception("Docker launch failed"); } return(container.ID); }
/// <summary> /// Performs the playback of actions in this module. /// </summary> /// <remarks>You should not call this method directly, instead pass the module /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method /// that will in turn invoke this method.</remarks> void ITestModule.Run() { // docker run --name wp1 -p 5556:80 --link mysql:mysql // -e WORDPRESS_DB_PASSWORD=pwd -e WORDPRESS_DB_NAME=wp1 -d wordpress ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true; DockerClient client = null; if (certFile.Length > 0) { var credentials = new CertificateCredentials(new X509Certificate2(certFile)); client = new DockerClientConfiguration(new Uri(dockerURL), credentials).CreateClient(); } else { client = new DockerClientConfiguration(new Uri(dockerURL)).CreateClient(); } Random r = new Random(); int portNr = r.Next(1500, 8000); TestSuite.CurrentTestContainer.Parameters["URL"] = TestSuite.CurrentTestContainer.Parameters["URL"] + ":" + portNr.ToString(); //Report.Info(portNr.ToString()); //Report.Info(TestSuite.CurrentTestContainer.Parameters["URL"]); Docker.DotNet.Models.HostConfig hostConf = new Docker.DotNet.Models.HostConfig(); hostConf.Links = new List <string>(); hostConf.Links.Add("mysql"); var portBindings = new Dictionary <string, IList <Docker.DotNet.Models.PortBinding> > { { 80.ToString(), new List <Docker.DotNet.Models.PortBinding> { new Docker.DotNet.Models.PortBinding { HostPort = portNr.ToString(), HostIP = "0.0.0.0" } } } }; var exposedPorts = new Dictionary <string, object>() { { 80.ToString(), new { HostPort = portNr.ToString() } } }; hostConf.PortBindings = portBindings; Docker.DotNet.Models.CreateContainerParameters createContainerParams = new Docker.DotNet.Models.CreateContainerParameters(); createContainerParams.HostConfig = hostConf; createContainerParams.Image = "a8f5a76ca5cd"; createContainerParams.Env = new List <string>(); createContainerParams.Env.Add("WORDPRESS_DB_PASSWORD=pwd"); createContainerParams.Env.Add(String.Format("WORDPRESS_DB_NAME=wp{0}", portNr)); createContainerParams.ExposedPorts = exposedPorts; var createTask = client.Containers.CreateContainerAsync(createContainerParams); createTask.Wait(); var container = createTask.Result; TestSuite.CurrentTestContainer.Parameters["containerID"] = container.ID; var incpectTask = client.Containers.InspectContainerAsync(container.ID); incpectTask.Wait(); var inspect = incpectTask.Result; var startParams = new Docker.DotNet.Models.ContainerStartParameters(); var startTast = client.Containers.StartContainerAsync(container.ID, startParams); startTast.Wait(); }