build_classic.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "os"
  20. "path/filepath"
  21. "runtime"
  22. "strings"
  23. "github.com/compose-spec/compose-go/types"
  24. buildx "github.com/docker/buildx/build"
  25. "github.com/docker/cli/cli/command/image/build"
  26. "github.com/docker/compose/v2/pkg/api"
  27. dockertypes "github.com/docker/docker/api/types"
  28. "github.com/docker/docker/cli"
  29. "github.com/docker/docker/pkg/archive"
  30. "github.com/docker/docker/pkg/idtools"
  31. "github.com/docker/docker/pkg/jsonmessage"
  32. "github.com/docker/docker/pkg/progress"
  33. "github.com/docker/docker/pkg/streamformatter"
  34. "github.com/docker/docker/pkg/urlutil"
  35. "github.com/hashicorp/go-multierror"
  36. "github.com/pkg/errors"
  37. )
  38. func (s *composeService) doBuildClassic(ctx context.Context, project *types.Project, opts map[string]buildx.Options) (map[string]string, error) {
  39. var nameDigests = make(map[string]string)
  40. var errs error
  41. err := project.WithServices(nil, func(service types.ServiceConfig) error {
  42. imageName := api.GetImageNameOrDefault(service, project.Name)
  43. o, ok := opts[imageName]
  44. if !ok {
  45. return nil
  46. }
  47. digest, err := s.doBuildClassicSimpleImage(ctx, o)
  48. if err != nil {
  49. errs = multierror.Append(errs, err).ErrorOrNil()
  50. }
  51. nameDigests[imageName] = digest
  52. return nil
  53. })
  54. if err != nil {
  55. return nil, err
  56. }
  57. return nameDigests, errs
  58. }
  59. //nolint: gocyclo
  60. func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options buildx.Options) (string, error) {
  61. var (
  62. buildCtx io.ReadCloser
  63. dockerfileCtx io.ReadCloser
  64. contextDir string
  65. tempDir string
  66. relDockerfile string
  67. err error
  68. )
  69. dockerfileName := options.Inputs.DockerfilePath
  70. specifiedContext := options.Inputs.ContextPath
  71. progBuff := s.stdout()
  72. buildBuff := s.stdout()
  73. if options.ImageIDFile != "" {
  74. // Avoid leaving a stale file if we eventually fail
  75. if err := os.Remove(options.ImageIDFile); err != nil && !os.IsNotExist(err) {
  76. return "", errors.Wrap(err, "removing image ID file")
  77. }
  78. }
  79. switch {
  80. case isLocalDir(specifiedContext):
  81. contextDir, relDockerfile, err = build.GetContextFromLocalDir(specifiedContext, dockerfileName)
  82. if err == nil && strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) {
  83. // Dockerfile is outside of build-context; read the Dockerfile and pass it as dockerfileCtx
  84. dockerfileCtx, err = os.Open(dockerfileName)
  85. if err != nil {
  86. return "", errors.Errorf("unable to open Dockerfile: %v", err)
  87. }
  88. defer dockerfileCtx.Close() //nolint:errcheck
  89. }
  90. case urlutil.IsGitURL(specifiedContext):
  91. tempDir, relDockerfile, err = build.GetContextFromGitURL(specifiedContext, dockerfileName)
  92. case urlutil.IsURL(specifiedContext):
  93. buildCtx, relDockerfile, err = build.GetContextFromURL(progBuff, specifiedContext, dockerfileName)
  94. default:
  95. return "", errors.Errorf("unable to prepare context: path %q not found", specifiedContext)
  96. }
  97. if err != nil {
  98. return "", errors.Errorf("unable to prepare context: %s", err)
  99. }
  100. if tempDir != "" {
  101. defer os.RemoveAll(tempDir) //nolint:errcheck
  102. contextDir = tempDir
  103. }
  104. // read from a directory into tar archive
  105. if buildCtx == nil {
  106. excludes, err := build.ReadDockerignore(contextDir)
  107. if err != nil {
  108. return "", err
  109. }
  110. if err := build.ValidateContextDirectory(contextDir, excludes); err != nil {
  111. return "", errors.Wrap(err, "checking context")
  112. }
  113. // And canonicalize dockerfile name to a platform-independent one
  114. relDockerfile = archive.CanonicalTarNameForPath(relDockerfile)
  115. excludes = build.TrimBuildFilesFromExcludes(excludes, relDockerfile, false)
  116. buildCtx, err = archive.TarWithOptions(contextDir, &archive.TarOptions{
  117. ExcludePatterns: excludes,
  118. ChownOpts: &idtools.Identity{},
  119. })
  120. if err != nil {
  121. return "", err
  122. }
  123. }
  124. // replace Dockerfile if it was added from stdin or a file outside the build-context, and there is archive context
  125. if dockerfileCtx != nil && buildCtx != nil {
  126. buildCtx, relDockerfile, err = build.AddDockerfileToBuildContext(dockerfileCtx, buildCtx)
  127. if err != nil {
  128. return "", err
  129. }
  130. }
  131. buildCtx, err = build.Compress(buildCtx)
  132. if err != nil {
  133. return "", err
  134. }
  135. progressOutput := streamformatter.NewProgressOutput(progBuff)
  136. body := progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
  137. configFile := s.configFile()
  138. creds, err := configFile.GetAllCredentials()
  139. if err != nil {
  140. return "", err
  141. }
  142. authConfigs := make(map[string]dockertypes.AuthConfig, len(creds))
  143. for k, auth := range creds {
  144. authConfigs[k] = dockertypes.AuthConfig(auth)
  145. }
  146. buildOptions := imageBuildOptions(options)
  147. buildOptions.Version = dockertypes.BuilderV1
  148. buildOptions.Dockerfile = relDockerfile
  149. buildOptions.AuthConfigs = authConfigs
  150. ctx, cancel := context.WithCancel(ctx)
  151. defer cancel()
  152. response, err := s.apiClient().ImageBuild(ctx, body, buildOptions)
  153. if err != nil {
  154. return "", err
  155. }
  156. defer response.Body.Close() //nolint:errcheck
  157. imageID := ""
  158. aux := func(msg jsonmessage.JSONMessage) {
  159. var result dockertypes.BuildResult
  160. if err := json.Unmarshal(*msg.Aux, &result); err != nil {
  161. fmt.Fprintf(s.stderr(), "Failed to parse aux message: %s", err)
  162. } else {
  163. imageID = result.ID
  164. }
  165. }
  166. err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, progBuff.FD(), true, aux)
  167. if err != nil {
  168. if jerr, ok := err.(*jsonmessage.JSONError); ok {
  169. // If no error code is set, default to 1
  170. if jerr.Code == 0 {
  171. jerr.Code = 1
  172. }
  173. return "", cli.StatusError{Status: jerr.Message, StatusCode: jerr.Code}
  174. }
  175. return "", err
  176. }
  177. // Windows: show error message about modified file permissions if the
  178. // daemon isn't running Windows.
  179. if response.OSType != "windows" && runtime.GOOS == "windows" {
  180. // if response.OSType != "windows" && runtime.GOOS == "windows" && !options.quiet {
  181. fmt.Fprintln(s.stdout(), "SECURITY WARNING: You are building a Docker "+
  182. "image from Windows against a non-Windows Docker host. All files and "+
  183. "directories added to build context will have '-rwxr-xr-x' permissions. "+
  184. "It is recommended to double check and reset permissions for sensitive "+
  185. "files and directories.")
  186. }
  187. if options.ImageIDFile != "" {
  188. if imageID == "" {
  189. return "", errors.Errorf("Server did not provide an image ID. Cannot write %s", options.ImageIDFile)
  190. }
  191. if err := os.WriteFile(options.ImageIDFile, []byte(imageID), 0o666); err != nil {
  192. return "", err
  193. }
  194. }
  195. return imageID, nil
  196. }
  197. func isLocalDir(c string) bool {
  198. _, err := os.Stat(c)
  199. return err == nil
  200. }
  201. func imageBuildOptions(options buildx.Options) dockertypes.ImageBuildOptions {
  202. return dockertypes.ImageBuildOptions{
  203. Tags: options.Tags,
  204. NoCache: options.NoCache,
  205. Remove: true,
  206. PullParent: options.Pull,
  207. BuildArgs: toMapStringStringPtr(options.BuildArgs),
  208. Labels: options.Labels,
  209. NetworkMode: options.NetworkMode,
  210. ExtraHosts: options.ExtraHosts,
  211. Target: options.Target,
  212. }
  213. }
  214. func toMapStringStringPtr(source map[string]string) map[string]*string {
  215. dest := make(map[string]*string)
  216. for k, v := range source {
  217. v := v
  218. dest[k] = &v
  219. }
  220. return dest
  221. }