Created
June 8, 2017 09:04
-
-
Save kunalkushwaha/5c2aae9eea2429e7c70f44c3cb21cc90 to your computer and use it in GitHub Desktop.
Create container in background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "io/ioutil" | |
| "os" | |
| "time" | |
| "github.com/containerd/containerd" | |
| "github.com/containerd/containerd/namespaces" | |
| ) | |
| func main() { | |
| var ( | |
| ctx = context.Background() | |
| background = namespaces.WithNamespace(ctx, "background") | |
| address = "/run/containerd/containerd.sock" | |
| ) | |
| fmt.Println("Test code for running containers in background") | |
| client, err := containerd.New(address) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| //Pull Image | |
| image, err := client.Pull(background, "docker.io/library/alpine:latest", containerd.WithPullUnpack) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| //Define specs and Create Conatiner | |
| spec, err := containerd.GenerateSpec(containerd.WithImageConfig(background, image), containerd.WithProcessArgs("cat")) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| container, err := client.NewContainer(background, "test", containerd.WithSpec(spec), containerd.WithImage(image), containerd.WithNewRootFS("test", image)) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| defer container.Delete(background) | |
| //Create Task in background | |
| r, _, err := os.Pipe() | |
| if err != err { | |
| fmt.Println("Error in creating pipe ", err) | |
| return | |
| } | |
| _, ow, err := os.Pipe() | |
| if err != nil { | |
| fmt.Println("Error in creating pipe ", err) | |
| return | |
| } | |
| task, err := container.NewTask(background, containerd.NewIO(r, ow, ioutil.Discard)) | |
| if err != nil { | |
| fmt.Println("Task creation error: ", err) | |
| return | |
| } | |
| defer task.Delete(background) | |
| //FIXME: wait in goroutine for Task Creation. | |
| time.Sleep(15 * time.Second) | |
| err = task.Start(background) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| fmt.Println(task.Pid()) | |
| return | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Task creation error: rpc error: code = Unknown desc = exit status 1