builder.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Package builder defines interfaces for any Docker builder to implement.
  2. //
  3. // Historically, only server-side Dockerfile interpreters existed.
  4. // This package allows for other implementations of Docker builders.
  5. package builder
  6. import (
  7. "io"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/backend"
  10. "github.com/docker/docker/api/types/container"
  11. containerpkg "github.com/docker/docker/container"
  12. "github.com/docker/docker/layer"
  13. "golang.org/x/net/context"
  14. )
  15. const (
  16. // DefaultDockerfileName is the Default filename with Docker commands, read by docker build
  17. DefaultDockerfileName string = "Dockerfile"
  18. )
  19. // Source defines a location that can be used as a source for the ADD/COPY
  20. // instructions in the builder.
  21. type Source interface {
  22. // Root returns root path for accessing source
  23. Root() string
  24. // Close allows to signal that the filesystem tree won't be used anymore.
  25. // For Context implementations using a temporary directory, it is recommended to
  26. // delete the temporary directory in Close().
  27. Close() error
  28. // Hash returns a checksum for a file
  29. Hash(path string) (string, error)
  30. }
  31. // Backend abstracts calls to a Docker Daemon.
  32. type Backend interface {
  33. ImageBackend
  34. ExecBackend
  35. // Commit creates a new Docker image from an existing Docker container.
  36. Commit(string, *backend.ContainerCommitConfig) (string, error)
  37. // ContainerCreateWorkdir creates the workdir
  38. ContainerCreateWorkdir(containerID string) error
  39. CreateImage(config []byte, parent string, platform string) (Image, error)
  40. ImageCacheBuilder
  41. }
  42. // ImageBackend are the interface methods required from an image component
  43. type ImageBackend interface {
  44. GetImageAndReleasableLayer(ctx context.Context, refOrID string, opts backend.GetImageAndLayerOptions) (Image, ReleaseableLayer, error)
  45. }
  46. // ExecBackend contains the interface methods required for executing containers
  47. type ExecBackend interface {
  48. // ContainerAttachRaw attaches to container.
  49. ContainerAttachRaw(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool, attached chan struct{}) error
  50. // ContainerCreate creates a new Docker container and returns potential warnings
  51. ContainerCreate(config types.ContainerCreateConfig) (container.ContainerCreateCreatedBody, error)
  52. // ContainerRm removes a container specified by `id`.
  53. ContainerRm(name string, config *types.ContainerRmConfig) error
  54. // ContainerKill stops the container execution abruptly.
  55. ContainerKill(containerID string, sig uint64) error
  56. // ContainerStart starts a new container
  57. ContainerStart(containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
  58. // ContainerWait stops processing until the given container is stopped.
  59. ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
  60. }
  61. // Result is the output produced by a Builder
  62. type Result struct {
  63. ImageID string
  64. FromImage Image
  65. }
  66. // ImageCacheBuilder represents a generator for stateful image cache.
  67. type ImageCacheBuilder interface {
  68. // MakeImageCache creates a stateful image cache.
  69. MakeImageCache(cacheFrom []string, platform string) ImageCache
  70. }
  71. // ImageCache abstracts an image cache.
  72. // (parent image, child runconfig) -> child image
  73. type ImageCache interface {
  74. // GetCache returns a reference to a cached image whose parent equals `parent`
  75. // and runconfig equals `cfg`. A cache miss is expected to return an empty ID and a nil error.
  76. GetCache(parentID string, cfg *container.Config) (imageID string, err error)
  77. }
  78. // Image represents a Docker image used by the builder.
  79. type Image interface {
  80. ImageID() string
  81. RunConfig() *container.Config
  82. MarshalJSON() ([]byte, error)
  83. }
  84. // ReleaseableLayer is an image layer that can be mounted and released
  85. type ReleaseableLayer interface {
  86. Release() error
  87. Mount() (string, error)
  88. Commit(platform string) (ReleaseableLayer, error)
  89. DiffID() layer.DiffID
  90. }