container.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package container
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/container"
  9. networktypes "github.com/docker/docker/api/types/network"
  10. "github.com/docker/docker/client"
  11. )
  12. // CreateVMemLabel creates a container with the supplied parameters
  13. func CreateVMemLabel(client client.APIClient, v []string, memoryMB int, labels map[string]string, img string, cmd []string) (string, error) {
  14. ctx := context.Background()
  15. config := container.Config{
  16. Cmd: cmd,
  17. Image: img,
  18. Labels: labels,
  19. }
  20. hostConfig := container.HostConfig{
  21. Binds: v,
  22. Resources: container.Resources{
  23. Memory: int64(memoryMB * 1024 * 1024),
  24. },
  25. }
  26. networkingConfig := networktypes.NetworkingConfig{}
  27. name := ""
  28. response, err := client.ContainerCreate(ctx, &config, &hostConfig, &networkingConfig, name)
  29. if err != nil {
  30. return "", err
  31. }
  32. return response.ID, nil
  33. }
  34. // Run runs the command provided in the container image named
  35. func Run(client client.APIClient, img string, cmd []string) (string, error) {
  36. return RunV(client, []string{}, img, cmd)
  37. }
  38. // RunV runs the command provided in the container image named with
  39. // the equivalent of -v bind mounts
  40. func RunV(client client.APIClient, v []string, img string, cmd []string) (string, error) {
  41. return RunVMem(client, v, 0, img, cmd)
  42. }
  43. // RunVMem runs the command provided in the container image named with
  44. // the equivalent of -v bind mounts and a specified memory limit
  45. func RunVMem(client client.APIClient, v []string, memoryMB int, img string, cmd []string) (string, error) {
  46. return RunVMemLabel(client, v, memoryMB, map[string]string{}, img, cmd)
  47. }
  48. // RunVLabel runs the command provided in the container image named
  49. // with the equivalent of -v bind mounts and the specified labels
  50. func RunVLabel(client client.APIClient, v []string, labels map[string]string, img string, cmd []string) (string, error) {
  51. return RunVMemLabel(client, v, 0, labels, img, cmd)
  52. }
  53. // RunVMemLabel runs the command provided in the container image named
  54. // with the equivalent of -v bind mounts, a specified memory limit,
  55. // and the specified labels
  56. func RunVMemLabel(client client.APIClient, v []string, memoryMB int, labels map[string]string, img string, cmd []string) (string, error) {
  57. containerID, err := CreateVMemLabel(client, v, memoryMB, labels, img, cmd)
  58. if err != nil {
  59. return "", err
  60. }
  61. ctx := context.Background()
  62. if err := client.ContainerStart(ctx, containerID, types.ContainerStartOptions{}); err != nil {
  63. return "", err
  64. }
  65. return containerID, nil
  66. }
  67. // Wait waits until the named container has exited
  68. func Wait(client client.APIClient, container string) (int64, error) {
  69. resultC, errC := client.ContainerWait(context.Background(), container, "")
  70. select {
  71. case result := <-resultC:
  72. return result.StatusCode, nil
  73. case err := <-errC:
  74. return -1, err
  75. }
  76. }
  77. // Stop stops the named container
  78. func Stop(client client.APIClient, container string) error {
  79. timeout := time.Duration(10) * time.Second
  80. ctx := context.Background()
  81. return client.ContainerStop(ctx, container, &timeout)
  82. }
  83. // Start starts the named container
  84. func Start(client client.APIClient, container string) error {
  85. ctx := context.Background()
  86. return client.ContainerStart(ctx, container, types.ContainerStartOptions{})
  87. }
  88. // Kill kills the named container with SIGKILL
  89. func Kill(client client.APIClient, container string) error {
  90. ctx := context.Background()
  91. return client.ContainerKill(ctx, container, "KILL")
  92. }
  93. // Export exports a container's file system as a tarball
  94. func Export(client client.APIClient, path, name string) error {
  95. ctx := context.Background()
  96. responseReader, err := client.ContainerExport(ctx, name)
  97. if err != nil {
  98. return err
  99. }
  100. defer responseReader.Close()
  101. file, err := os.Create(path)
  102. if err != nil {
  103. return err
  104. }
  105. defer file.Close()
  106. _, err = io.Copy(file, responseReader)
  107. return err
  108. }