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