builder.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 // import "github.com/docker/docker/builder"
  6. import (
  7. "context"
  8. "io"
  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/image"
  13. "github.com/docker/docker/layer"
  14. "github.com/opencontainers/go-digest"
  15. )
  16. const (
  17. // DefaultDockerfileName is the Default filename with Docker commands, read by docker build
  18. DefaultDockerfileName = "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() string
  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. // CommitBuildStep creates a new Docker image from the config generated by
  37. // a build step.
  38. CommitBuildStep(context.Context, backend.CommitConfig) (image.ID, error)
  39. // ContainerCreateWorkdir creates the workdir
  40. ContainerCreateWorkdir(containerID string) error
  41. CreateImage(ctx context.Context, config []byte, parent string, contentStoreDigest digest.Digest) (Image, error)
  42. ImageCacheBuilder
  43. }
  44. // ImageBackend are the interface methods required from an image component
  45. type ImageBackend interface {
  46. GetImageAndReleasableLayer(ctx context.Context, refOrID string, opts backend.GetImageAndLayerOptions) (Image, ROLayer, error)
  47. }
  48. // ExecBackend contains the interface methods required for executing containers
  49. type ExecBackend interface {
  50. // ContainerAttachRaw attaches to container.
  51. ContainerAttachRaw(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool, attached chan struct{}) error
  52. // ContainerCreateIgnoreImagesArgsEscaped creates a new Docker container and returns potential warnings
  53. ContainerCreateIgnoreImagesArgsEscaped(ctx context.Context, config backend.ContainerCreateConfig) (container.CreateResponse, error)
  54. // ContainerRm removes a container specified by `id`.
  55. ContainerRm(name string, config *backend.ContainerRmConfig) error
  56. // ContainerKill stops the container execution abruptly.
  57. ContainerKill(containerID string, sig string) error
  58. // ContainerStart starts a new container
  59. ContainerStart(ctx context.Context, containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
  60. // ContainerWait stops processing until the given container is stopped.
  61. ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
  62. }
  63. // Result is the output produced by a Builder
  64. type Result struct {
  65. ImageID string
  66. FromImage Image
  67. }
  68. // ImageCacheBuilder represents a generator for stateful image cache.
  69. type ImageCacheBuilder interface {
  70. // MakeImageCache creates a stateful image cache.
  71. MakeImageCache(ctx context.Context, cacheFrom []string) (ImageCache, error)
  72. }
  73. // ImageCache abstracts an image cache.
  74. // (parent image, child runconfig) -> child image
  75. type ImageCache interface {
  76. // GetCache returns a reference to a cached image whose parent equals `parent`
  77. // and runconfig equals `cfg`. A cache miss is expected to return an empty ID and a nil error.
  78. GetCache(parentID string, cfg *container.Config) (imageID string, err error)
  79. }
  80. // Image represents a Docker image used by the builder.
  81. type Image interface {
  82. ImageID() string
  83. RunConfig() *container.Config
  84. MarshalJSON() ([]byte, error)
  85. OperatingSystem() string
  86. }
  87. // ROLayer is a reference to image rootfs layer
  88. type ROLayer interface {
  89. Release() error
  90. NewRWLayer() (RWLayer, error)
  91. DiffID() layer.DiffID
  92. ContentStoreDigest() digest.Digest
  93. }
  94. // RWLayer is active layer that can be read/modified
  95. type RWLayer interface {
  96. Release() error
  97. Root() string
  98. Commit() (ROLayer, error)
  99. }