/// <summary>
        /// imageName must be on server already otherwise expect a 404
        /// </summary>
        /// <param name="imageName">name of the image, ex: redis</param>
        /// <returns></returns>
        public async Task<string> DockerRun(string imageName)
        {
            //            docker create
            // if miss - 404 on API
            //    docker pull imagename
            //docker create
            //docker start


            var runParams = new DotNet.Models.CreateContainerParameters();
            
            Config y = new Config();
            runParams.Config = y;

            runParams.Config.Tty = true;
            runParams.Config.AttachStderr = true;
            runParams.Config.AttachStdout = true;
            
            
            runParams.Config.Image = imageName;
            

            var result =  await client.Containers.CreateContainerAsync(runParams);
            var hostconfig = new HostConfig();

            await client.Containers.StartContainerAsync(result.Id, hostconfig);
            return result.Id; 

        }
Esempio n. 2
0
        public async Task OneTimeSetup()
        {
            //docker run -p 2181:2181 -p 9092:9092 --env ADVERTISED_HOST=127.0.0.1 --env ADVERTISED_PORT=9092 --name kafka-rebus spotify/kafka
            TestContext.Out.WriteLine("Starting One time setup");
            var client = new DockerClientConfiguration(LocalDockerUri())
                         .CreateClient();
            var listCont = await client.Containers.ListContainersAsync(new ContainersListParameters());

            TestContext.Out.WriteLine("I found {0} running containers on the local machine", listCont.Count);
            // the docker images are started, need to restart them
            foreach (var x in listCont.Where(x => x.Names.Any(y => y.Contains("kafka-rebus"))))
            {
                var i = await client.Containers.StopContainerAsync(x.ID, new ContainerStopParameters());

                await client.Containers.RemoveContainerAsync(x.ID, new ContainerRemoveParameters());
            }

            var parameters = new Docker.DotNet.Models.Config
            {
                Image        = "spotify/kafka",
                ArgsEscaped  = true,
                AttachStderr = true,
                AttachStdin  = true,
                AttachStdout = true,
                Env          = new List <string> {
                    "ADVERTISED_PORT=9092", "ADVERTISED_HOST=127.0.0.1"
                }
            };

            var ports2181 = new List <PortBinding> {
                new PortBinding()
                {
                    HostPort = "2181", HostIP = ""
                }
            };
            var ports9092 = new List <PortBinding> {
                new PortBinding()
                {
                    HostPort = "9092", HostIP = ""
                }
            };

            var resp = await client.Containers.CreateContainerAsync(new CreateContainerParameters(parameters)
            {
                HostConfig = new HostConfig {
                    PortBindings = new Dictionary <string, IList <PortBinding> >()
                    {
                        { "2181/tcp", ports2181 }, { "9092/tcp", ports9092 }
                    }
                },
                Name = "kafka-rebus"
            });

            await client.Containers.StartContainerAsync(resp.ID, new ContainerStartParameters());

            // it takes a few seconds for the agents to come online and register with the servers
            Thread.Sleep(5000);
        }
Esempio n. 3
0
        public ServicesFixture()
        {
            //docker run -p 2181:2181 -p 9092:9092 --env ADVERTISED_HOST=127.0.0.1 --env ADVERTISED_PORT=9092 --name kafka-rebus spotify/kafka
            _output.Add("Starting One time setup");
            try
            {
                var client            = new DockerClientConfiguration(LocalDockerUri()).CreateClient();
                var name              = "kafka-rebus";
                var existingContainer = client.Containers.ListContainersAsync(new ContainersListParameters
                {
                    All     = true,
                    Filters = new Dictionary <string, IDictionary <string, bool> > {
                        { "name", new Dictionary <string, bool> {
                              { name, true }
                          } }
                    }
                }).Result.FirstOrDefault();

                if (existingContainer != null)
                {
                    _output.Add($"Found container on the local machine with name \"{name}\", state: {existingContainer.State}, Id: {existingContainer.ID}");
                    if (existingContainer.State == "running")
                    {
                        // the docker images are started, need to restart them
                        bool completed = client.Containers.StopContainerAsync(existingContainer.ID, new ContainerStopParameters()).Result;
                        _output.Add($"Stopped container with Id: {existingContainer.ID}.");
                    }
                    client.Containers.RemoveContainerAsync(existingContainer.ID, new ContainerRemoveParameters()).Wait();
                    _output.Add($"Removed Container with Id: {existingContainer.ID}.");
                }
                else
                {
                    _output.Add($"Containers with Name \"{name}\" not found.");
                }

                // pull image again
                client.Images.CreateImageAsync(
                    new ImagesCreateParameters
                {
                    FromImage = "spotify/kafka",
                    Tag       = "latest"
                },
                    new AuthConfig(),
                    new Progress <JSONMessage>(msg =>
                                               _output.Add($"{msg.Status}|{msg.ProgressMessage}|{msg.ErrorMessage}")
                                               )).Wait();

                var parameters = new Docker.DotNet.Models.Config
                {
                    Image        = "spotify/kafka:latest",
                    ArgsEscaped  = true,
                    AttachStderr = true,
                    AttachStdin  = true,
                    AttachStdout = true,
                    Env          = new List <string> {
                        "ADVERTISED_PORT=9092", "ADVERTISED_HOST=127.0.0.1"
                    }
                };

                var ports2181 = new List <PortBinding> {
                    new PortBinding {
                        HostPort = "2181", HostIP = ""
                    }
                };
                var ports9092 = new List <PortBinding> {
                    new PortBinding {
                        HostPort = "9092", HostIP = ""
                    }
                };

                var resp = client.Containers.CreateContainerAsync(new CreateContainerParameters(parameters)
                {
                    HostConfig = new HostConfig {
                        PortBindings = new Dictionary <string, IList <PortBinding> > {
                            { "2181/tcp", ports2181 }, { "9092/tcp", ports9092 }
                        }
                    },
                    Name = name
                }).Result;
                _output.Add($"Created container with Id: {resp.ID}.");

                bool result = client.Containers.StartContainerAsync(resp.ID, new ContainerStartParameters()).Result;
                _output.Add($"Started container with Id: {resp.ID}.");
            }
            catch (Exception e)
            {
                if (_output.Count > 0)
                {
                    _output[0] = _output[0] + $" was aborted by an exception {e.GetType().Name}.\nThe logs before the exception:\n";
                }
                throw new ApplicationException(string.Join("\n", _output), e);
            }

            Thread.Sleep(3000);
        }