public async Task <Guid> CreateSandbox(Guid userId) { User user = await this.CreateUserIfNotExisted(userId); Console.WriteLine(user.Sandboxes.Count); if (user.Sandboxes.Count > 0) { return(user.Sandboxes.First().SandboxId); } string configDir = $"{userId}/code-server"; string projDir = $"{userId}/workspace"; Directory.CreateDirectory(configDir); Directory.CreateDirectory(projDir); SandboxCreationConfig config = new SandboxCreationConfig() { Port = user.Port.ToString(), ConfigDir = configDir, ProjectDir = projDir, Password = "******", Image = "codercom/code-server:v2" }; string containerId = await this.docker.CreateContainer(config); Sandbox sandbox = new Sandbox() { SandboxId = Guid.NewGuid(), DockerContainerId = containerId, UserId = userId, DockerContainerPort = config.Port, SandboxCreationConfig = JsonConvert.SerializeObject(config) }; context.Sandboxes.Add(sandbox); await context.SaveChangesAsync(); return(sandbox.SandboxId); }
public async Task <string> CreateContainer(SandboxCreationConfig sandboxConfig) { CreateContainerResponse response = await client.Containers.CreateContainerAsync(new CreateContainerParameters() { Image = sandboxConfig.Image, Tty = true, // -it HostConfig = new HostConfig { PortBindings = new Dictionary <string, IList <PortBinding> > { { "8080/tcp", new List <PortBinding> { new PortBinding { HostPort = sandboxConfig.Port } } } } }, Volumes = new Dictionary <string, EmptyStruct>() { { $"{sandboxConfig.ConfigDir}:/home/coder/.local/share/code-server", default },