builder.go 3.9 KB

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