static async Task TestClient() { var dockerClient = new DockerClient.DockerClient(); var volumes = await dockerClient.Volumes.List(); foreach (var volume in volumes.Volumes) { Console.WriteLine(volume.Name); } }
public static async Task Main(string[] args) { DockerClient.DockerClient client = new DockerClient.DockerClient( new Uri("http://localhost:2376")); // docker run --name {id} --label {image} {arguments} var loggerConfiguration = new LoggerConfiguration() #if DEBUG .MinimumLevel.Verbose() #else .MinimumLevel.Information() #endif .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Console().WriteTo .RollingFile("dotnet-spider-portal.log"); Log.Logger = loggerConfiguration.CreateLogger(); await CreateWebHostBuilder(args).Build().RunAsync(); }
public static async Task RunAsync(PortalOptions options, PortalDbContext dbContext, int jobId) { var spider = await dbContext.Spiders.FirstOrDefaultAsync(x => x.Id == jobId); if (spider == null) { throw new Exception($"任务 {jobId} 不存在"); } var docker = new DockerClient.DockerClient(new Uri(options.Docker)); bool exists = await docker.ExistsAsync(new { label = new[] { $"dotnetspider.spider.id={spider.Id}" } }); if (exists) { throw new Exception($"任务 {spider.Id} 正在运行"); } var env = new List <string>((spider.Environment ?? "").Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)) { $"id={spider.Id}", $"type={spider.Type}", $"name={spider.Name}" }; var result = await docker.PullAsync(spider.Image); if (!result.Success) { throw new Exception($"接取镜像 {spider.Image} 失败: {result.Message}"); } result = await docker.CreateAsync(spider.Image, env.ToArray(), new Dictionary <string, object> { { "id", spider.Id.ToString() }, { "type", spider.Type }, { "name", spider.Name } }, new[] { options.DockerVolumes }); if (!result.Success) { throw new Exception($"创建任务 {jobId} 实例失败: {result.Message}"); } var spiderContainer = new SpiderContainer { ContainerId = result.Id, SpiderId = spider.Id, Status = "Creating" }; result = await docker.StartAsync(spiderContainer.ContainerId); if (result.Success) { spiderContainer.Status = "OK"; } dbContext.SpiderContainers.Add(spiderContainer); await dbContext.SaveChangesAsync(); }