dispatchers.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. // This file contains the dispatchers for each command. Note that
  3. // `nullDispatch` is not actually a command, but support for commands we parse
  4. // but do nothing with.
  5. //
  6. // See evaluator.go for a higher level discussion of the whole evaluator
  7. // package.
  8. import (
  9. "bytes"
  10. "fmt"
  11. "runtime"
  12. "sort"
  13. "strings"
  14. "github.com/containerd/containerd/platforms"
  15. "github.com/docker/docker/api"
  16. "github.com/docker/docker/api/types/strslice"
  17. "github.com/docker/docker/builder"
  18. "github.com/docker/docker/errdefs"
  19. "github.com/docker/docker/image"
  20. "github.com/docker/docker/pkg/jsonmessage"
  21. "github.com/docker/docker/pkg/system"
  22. "github.com/docker/go-connections/nat"
  23. "github.com/moby/buildkit/frontend/dockerfile/instructions"
  24. "github.com/moby/buildkit/frontend/dockerfile/parser"
  25. "github.com/moby/buildkit/frontend/dockerfile/shell"
  26. "github.com/moby/sys/signal"
  27. specs "github.com/opencontainers/image-spec/specs-go/v1"
  28. "github.com/pkg/errors"
  29. )
  30. // ENV foo bar
  31. //
  32. // Sets the environment variable foo to bar, also makes interpolation
  33. // in the dockerfile available from the next statement on via ${foo}.
  34. func dispatchEnv(d dispatchRequest, c *instructions.EnvCommand) error {
  35. runConfig := d.state.runConfig
  36. commitMessage := bytes.NewBufferString("ENV")
  37. for _, e := range c.Env {
  38. name := e.Key
  39. newVar := e.String()
  40. commitMessage.WriteString(" " + newVar)
  41. gotOne := false
  42. for i, envVar := range runConfig.Env {
  43. envParts := strings.SplitN(envVar, "=", 2)
  44. compareFrom := envParts[0]
  45. if shell.EqualEnvKeys(compareFrom, name) {
  46. runConfig.Env[i] = newVar
  47. gotOne = true
  48. break
  49. }
  50. }
  51. if !gotOne {
  52. runConfig.Env = append(runConfig.Env, newVar)
  53. }
  54. }
  55. return d.builder.commit(d.state, commitMessage.String())
  56. }
  57. // MAINTAINER some text <maybe@an.email.address>
  58. //
  59. // Sets the maintainer metadata.
  60. func dispatchMaintainer(d dispatchRequest, c *instructions.MaintainerCommand) error {
  61. d.state.maintainer = c.Maintainer
  62. return d.builder.commit(d.state, "MAINTAINER "+c.Maintainer)
  63. }
  64. // LABEL some json data describing the image
  65. //
  66. // Sets the Label variable foo to bar,
  67. func dispatchLabel(d dispatchRequest, c *instructions.LabelCommand) error {
  68. if d.state.runConfig.Labels == nil {
  69. d.state.runConfig.Labels = make(map[string]string)
  70. }
  71. commitStr := "LABEL"
  72. for _, v := range c.Labels {
  73. d.state.runConfig.Labels[v.Key] = v.Value
  74. commitStr += " " + v.String()
  75. }
  76. return d.builder.commit(d.state, commitStr)
  77. }
  78. // ADD foo /path
  79. //
  80. // Add the file 'foo' to '/path'. Tarball and Remote URL (http, https) handling
  81. // exist here. If you do not wish to have this automatic handling, use COPY.
  82. func dispatchAdd(d dispatchRequest, c *instructions.AddCommand) error {
  83. if c.Chmod != "" {
  84. return errors.New("the --chmod option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with BuildKit enabled")
  85. }
  86. downloader := newRemoteSourceDownloader(d.builder.Output, d.builder.Stdout)
  87. copier := copierFromDispatchRequest(d, downloader, nil)
  88. defer copier.Cleanup()
  89. copyInstruction, err := copier.createCopyInstruction(c.SourcesAndDest, "ADD")
  90. if err != nil {
  91. return err
  92. }
  93. copyInstruction.chownStr = c.Chown
  94. copyInstruction.allowLocalDecompression = true
  95. return d.builder.performCopy(d, copyInstruction)
  96. }
  97. // COPY foo /path
  98. //
  99. // Same as 'ADD' but without the tar and remote url handling.
  100. func dispatchCopy(d dispatchRequest, c *instructions.CopyCommand) error {
  101. if c.Chmod != "" {
  102. return errors.New("the --chmod option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with BuildKit enabled")
  103. }
  104. var im *imageMount
  105. var err error
  106. if c.From != "" {
  107. im, err = d.getImageMount(c.From)
  108. if err != nil {
  109. return errors.Wrapf(err, "invalid from flag value %s", c.From)
  110. }
  111. }
  112. copier := copierFromDispatchRequest(d, errOnSourceDownload, im)
  113. defer copier.Cleanup()
  114. copyInstruction, err := copier.createCopyInstruction(c.SourcesAndDest, "COPY")
  115. if err != nil {
  116. return err
  117. }
  118. copyInstruction.chownStr = c.Chown
  119. if c.From != "" && copyInstruction.chownStr == "" {
  120. copyInstruction.preserveOwnership = true
  121. }
  122. return d.builder.performCopy(d, copyInstruction)
  123. }
  124. func (d *dispatchRequest) getImageMount(imageRefOrID string) (*imageMount, error) {
  125. if imageRefOrID == "" {
  126. // TODO: this could return the source in the default case as well?
  127. return nil, nil
  128. }
  129. var localOnly bool
  130. stage, err := d.stages.get(imageRefOrID)
  131. if err != nil {
  132. return nil, err
  133. }
  134. if stage != nil {
  135. imageRefOrID = stage.Image
  136. localOnly = true
  137. }
  138. return d.builder.imageSources.Get(imageRefOrID, localOnly, d.builder.platform)
  139. }
  140. // FROM [--platform=platform] imagename[:tag | @digest] [AS build-stage-name]
  141. func initializeStage(d dispatchRequest, cmd *instructions.Stage) error {
  142. d.builder.imageProber.Reset()
  143. var platform *specs.Platform
  144. if v := cmd.Platform; v != "" {
  145. v, err := d.getExpandedString(d.shlex, v)
  146. if err != nil {
  147. return errors.Wrapf(err, "failed to process arguments for platform %s", v)
  148. }
  149. p, err := platforms.Parse(v)
  150. if err != nil {
  151. return errors.Wrapf(err, "failed to parse platform %s", v)
  152. }
  153. platform = &p
  154. }
  155. image, err := d.getFromImage(d.shlex, cmd.BaseName, platform)
  156. if err != nil {
  157. return err
  158. }
  159. state := d.state
  160. if err := state.beginStage(cmd.Name, image); err != nil {
  161. return err
  162. }
  163. if len(state.runConfig.OnBuild) > 0 {
  164. triggers := state.runConfig.OnBuild
  165. state.runConfig.OnBuild = nil
  166. return dispatchTriggeredOnBuild(d, triggers)
  167. }
  168. return nil
  169. }
  170. func dispatchTriggeredOnBuild(d dispatchRequest, triggers []string) error {
  171. fmt.Fprintf(d.builder.Stdout, "# Executing %d build trigger", len(triggers))
  172. if len(triggers) > 1 {
  173. fmt.Fprint(d.builder.Stdout, "s")
  174. }
  175. fmt.Fprintln(d.builder.Stdout)
  176. for _, trigger := range triggers {
  177. d.state.updateRunConfig()
  178. ast, err := parser.Parse(strings.NewReader(trigger))
  179. if err != nil {
  180. return err
  181. }
  182. if len(ast.AST.Children) != 1 {
  183. return errors.New("onbuild trigger should be a single expression")
  184. }
  185. cmd, err := instructions.ParseCommand(ast.AST.Children[0])
  186. if err != nil {
  187. var uiErr *instructions.UnknownInstructionError
  188. if errors.As(err, &uiErr) {
  189. buildsFailed.WithValues(metricsUnknownInstructionError).Inc()
  190. }
  191. return err
  192. }
  193. err = dispatch(d, cmd)
  194. if err != nil {
  195. return err
  196. }
  197. }
  198. return nil
  199. }
  200. func (d *dispatchRequest) getExpandedString(shlex *shell.Lex, str string) (string, error) {
  201. substitutionArgs := []string{}
  202. for key, value := range d.state.buildArgs.GetAllMeta() {
  203. substitutionArgs = append(substitutionArgs, key+"="+value)
  204. }
  205. name, err := shlex.ProcessWord(str, substitutionArgs)
  206. if err != nil {
  207. return "", err
  208. }
  209. return name, nil
  210. }
  211. func (d *dispatchRequest) getImageOrStage(name string, platform *specs.Platform) (builder.Image, error) {
  212. var localOnly bool
  213. if im, ok := d.stages.getByName(name); ok {
  214. name = im.Image
  215. localOnly = true
  216. }
  217. if platform == nil {
  218. platform = d.builder.platform
  219. }
  220. // Windows cannot support a container with no base image.
  221. if name == api.NoBaseImageSpecifier {
  222. // Windows supports scratch. What is not supported is running containers from it.
  223. if runtime.GOOS == "windows" {
  224. return nil, errors.New("Windows does not support FROM scratch")
  225. }
  226. // TODO: scratch should not have an os. It should be nil image.
  227. imageImage := &image.Image{}
  228. if platform != nil {
  229. imageImage.OS = platform.OS
  230. } else {
  231. imageImage.OS = runtime.GOOS
  232. }
  233. return builder.Image(imageImage), nil
  234. }
  235. imageMount, err := d.builder.imageSources.Get(name, localOnly, platform)
  236. if err != nil {
  237. return nil, err
  238. }
  239. return imageMount.Image(), nil
  240. }
  241. func (d *dispatchRequest) getFromImage(shlex *shell.Lex, basename string, platform *specs.Platform) (builder.Image, error) {
  242. name, err := d.getExpandedString(shlex, basename)
  243. if err != nil {
  244. return nil, err
  245. }
  246. // Empty string is interpreted to FROM scratch by images.GetImageAndReleasableLayer,
  247. // so validate expanded result is not empty.
  248. if name == "" {
  249. return nil, errors.Errorf("base name (%s) should not be blank", basename)
  250. }
  251. return d.getImageOrStage(name, platform)
  252. }
  253. func dispatchOnbuild(d dispatchRequest, c *instructions.OnbuildCommand) error {
  254. d.state.runConfig.OnBuild = append(d.state.runConfig.OnBuild, c.Expression)
  255. return d.builder.commit(d.state, "ONBUILD "+c.Expression)
  256. }
  257. // WORKDIR /tmp
  258. //
  259. // Set the working directory for future RUN/CMD/etc statements.
  260. func dispatchWorkdir(d dispatchRequest, c *instructions.WorkdirCommand) error {
  261. runConfig := d.state.runConfig
  262. var err error
  263. runConfig.WorkingDir, err = normalizeWorkdir(d.state.operatingSystem, runConfig.WorkingDir, c.Path)
  264. if err != nil {
  265. return err
  266. }
  267. // For performance reasons, we explicitly do a create/mkdir now
  268. // This avoids having an unnecessary expensive mount/unmount calls
  269. // (on Windows in particular) during each container create.
  270. // Prior to 1.13, the mkdir was deferred and not executed at this step.
  271. if d.builder.disableCommit {
  272. // Don't call back into the daemon if we're going through docker commit --change "WORKDIR /foo".
  273. // We've already updated the runConfig and that's enough.
  274. return nil
  275. }
  276. comment := "WORKDIR " + runConfig.WorkingDir
  277. runConfigWithCommentCmd := copyRunConfig(runConfig, withCmdCommentString(comment, d.state.operatingSystem))
  278. containerID, err := d.builder.probeAndCreate(d.state, runConfigWithCommentCmd)
  279. if err != nil || containerID == "" {
  280. return err
  281. }
  282. if err := d.builder.docker.ContainerCreateWorkdir(containerID); err != nil {
  283. return err
  284. }
  285. return d.builder.commitContainer(d.state, containerID, runConfigWithCommentCmd)
  286. }
  287. // RUN some command yo
  288. //
  289. // run a command and commit the image. Args are automatically prepended with
  290. // the current SHELL which defaults to 'sh -c' under linux or 'cmd /S /C' under
  291. // Windows, in the event there is only one argument The difference in processing:
  292. //
  293. // RUN echo hi # sh -c echo hi (Linux and LCOW)
  294. // RUN echo hi # cmd /S /C echo hi (Windows)
  295. // RUN [ "echo", "hi" ] # echo hi
  296. func dispatchRun(d dispatchRequest, c *instructions.RunCommand) error {
  297. if !system.IsOSSupported(d.state.operatingSystem) {
  298. return system.ErrNotSupportedOperatingSystem
  299. }
  300. if len(c.FlagsUsed) > 0 {
  301. // classic builder RUN currently does not support any flags, so fail on the first one
  302. return errors.Errorf("the --%s option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with BuildKit enabled", c.FlagsUsed[0])
  303. }
  304. stateRunConfig := d.state.runConfig
  305. cmdFromArgs, argsEscaped := resolveCmdLine(c.ShellDependantCmdLine, stateRunConfig, d.state.operatingSystem, c.Name(), c.String())
  306. buildArgs := d.state.buildArgs.FilterAllowed(stateRunConfig.Env)
  307. saveCmd := cmdFromArgs
  308. if len(buildArgs) > 0 {
  309. saveCmd = prependEnvOnCmd(d.state.buildArgs, buildArgs, cmdFromArgs)
  310. }
  311. runConfigForCacheProbe := copyRunConfig(stateRunConfig,
  312. withCmd(saveCmd),
  313. withArgsEscaped(argsEscaped),
  314. withEntrypointOverride(saveCmd, nil))
  315. if hit, err := d.builder.probeCache(d.state, runConfigForCacheProbe); err != nil || hit {
  316. return err
  317. }
  318. runConfig := copyRunConfig(stateRunConfig,
  319. withCmd(cmdFromArgs),
  320. withArgsEscaped(argsEscaped),
  321. withEnv(append(stateRunConfig.Env, buildArgs...)),
  322. withEntrypointOverride(saveCmd, strslice.StrSlice{""}),
  323. withoutHealthcheck())
  324. cID, err := d.builder.create(runConfig)
  325. if err != nil {
  326. return err
  327. }
  328. if err := d.builder.containerManager.Run(d.builder.clientCtx, cID, d.builder.Stdout, d.builder.Stderr); err != nil {
  329. if err, ok := err.(*statusCodeError); ok {
  330. // TODO: change error type, because jsonmessage.JSONError assumes HTTP
  331. msg := fmt.Sprintf(
  332. "The command '%s' returned a non-zero code: %d",
  333. strings.Join(runConfig.Cmd, " "), err.StatusCode())
  334. if err.Error() != "" {
  335. msg = fmt.Sprintf("%s: %s", msg, err.Error())
  336. }
  337. return &jsonmessage.JSONError{
  338. Message: msg,
  339. Code: err.StatusCode(),
  340. }
  341. }
  342. return err
  343. }
  344. // Don't persist the argsEscaped value in the committed image. Use the original
  345. // from previous build steps (only CMD and ENTRYPOINT persist this).
  346. if d.state.operatingSystem == "windows" {
  347. runConfigForCacheProbe.ArgsEscaped = stateRunConfig.ArgsEscaped
  348. }
  349. return d.builder.commitContainer(d.state, cID, runConfigForCacheProbe)
  350. }
  351. // Derive the command to use for probeCache() and to commit in this container.
  352. // Note that we only do this if there are any build-time env vars. Also, we
  353. // use the special argument "|#" at the start of the args array. This will
  354. // avoid conflicts with any RUN command since commands can not
  355. // start with | (vertical bar). The "#" (number of build envs) is there to
  356. // help ensure proper cache matches. We don't want a RUN command
  357. // that starts with "foo=abc" to be considered part of a build-time env var.
  358. //
  359. // remove any unreferenced built-in args from the environment variables.
  360. // These args are transparent so resulting image should be the same regardless
  361. // of the value.
  362. func prependEnvOnCmd(buildArgs *BuildArgs, buildArgVars []string, cmd strslice.StrSlice) strslice.StrSlice {
  363. var tmpBuildEnv []string
  364. for _, env := range buildArgVars {
  365. key := strings.SplitN(env, "=", 2)[0]
  366. if buildArgs.IsReferencedOrNotBuiltin(key) {
  367. tmpBuildEnv = append(tmpBuildEnv, env)
  368. }
  369. }
  370. sort.Strings(tmpBuildEnv)
  371. tmpEnv := append([]string{fmt.Sprintf("|%d", len(tmpBuildEnv))}, tmpBuildEnv...)
  372. return strslice.StrSlice(append(tmpEnv, cmd...))
  373. }
  374. // CMD foo
  375. //
  376. // Set the default command to run in the container (which may be empty).
  377. // Argument handling is the same as RUN.
  378. func dispatchCmd(d dispatchRequest, c *instructions.CmdCommand) error {
  379. runConfig := d.state.runConfig
  380. cmd, argsEscaped := resolveCmdLine(c.ShellDependantCmdLine, runConfig, d.state.operatingSystem, c.Name(), c.String())
  381. // We warn here as Windows shell processing operates differently to Linux.
  382. // Linux: /bin/sh -c "echo hello" world --> hello
  383. // Windows: cmd /s /c "echo hello" world --> hello world
  384. if d.state.operatingSystem == "windows" &&
  385. len(runConfig.Entrypoint) > 0 &&
  386. d.state.runConfig.ArgsEscaped != argsEscaped {
  387. fmt.Fprintf(d.builder.Stderr, " ---> [Warning] Shell-form ENTRYPOINT and exec-form CMD may have unexpected results\n")
  388. }
  389. runConfig.Cmd = cmd
  390. runConfig.ArgsEscaped = argsEscaped
  391. if err := d.builder.commit(d.state, fmt.Sprintf("CMD %q", cmd)); err != nil {
  392. return err
  393. }
  394. if len(c.ShellDependantCmdLine.CmdLine) != 0 {
  395. d.state.cmdSet = true
  396. }
  397. return nil
  398. }
  399. // HEALTHCHECK foo
  400. //
  401. // Set the default healthcheck command to run in the container (which may be empty).
  402. // Argument handling is the same as RUN.
  403. func dispatchHealthcheck(d dispatchRequest, c *instructions.HealthCheckCommand) error {
  404. runConfig := d.state.runConfig
  405. if runConfig.Healthcheck != nil {
  406. oldCmd := runConfig.Healthcheck.Test
  407. if len(oldCmd) > 0 && oldCmd[0] != "NONE" {
  408. fmt.Fprintf(d.builder.Stdout, "Note: overriding previous HEALTHCHECK: %v\n", oldCmd)
  409. }
  410. }
  411. runConfig.Healthcheck = c.Health
  412. return d.builder.commit(d.state, fmt.Sprintf("HEALTHCHECK %q", runConfig.Healthcheck))
  413. }
  414. // ENTRYPOINT /usr/sbin/nginx
  415. //
  416. // Set the entrypoint to /usr/sbin/nginx. Will accept the CMD as the arguments
  417. // to /usr/sbin/nginx. Uses the default shell if not in JSON format.
  418. //
  419. // Handles command processing similar to CMD and RUN, only req.runConfig.Entrypoint
  420. // is initialized at newBuilder time instead of through argument parsing.
  421. func dispatchEntrypoint(d dispatchRequest, c *instructions.EntrypointCommand) error {
  422. runConfig := d.state.runConfig
  423. cmd, argsEscaped := resolveCmdLine(c.ShellDependantCmdLine, runConfig, d.state.operatingSystem, c.Name(), c.String())
  424. // This warning is a little more complex than in dispatchCmd(), as the Windows base images (similar
  425. // universally to almost every Linux image out there) have a single .Cmd field populated so that
  426. // `docker run --rm image` starts the default shell which would typically be sh on Linux,
  427. // or cmd on Windows. The catch to this is that if a dockerfile had `CMD ["c:\\windows\\system32\\cmd.exe"]`,
  428. // we wouldn't be able to tell the difference. However, that would be highly unlikely, and besides, this
  429. // is only trying to give a helpful warning of possibly unexpected results.
  430. if d.state.operatingSystem == "windows" &&
  431. d.state.runConfig.ArgsEscaped != argsEscaped &&
  432. ((len(runConfig.Cmd) == 1 && strings.ToLower(runConfig.Cmd[0]) != `c:\windows\system32\cmd.exe` && len(runConfig.Shell) == 0) || (len(runConfig.Cmd) > 1)) {
  433. fmt.Fprintf(d.builder.Stderr, " ---> [Warning] Shell-form CMD and exec-form ENTRYPOINT may have unexpected results\n")
  434. }
  435. runConfig.Entrypoint = cmd
  436. runConfig.ArgsEscaped = argsEscaped
  437. if !d.state.cmdSet {
  438. runConfig.Cmd = nil
  439. }
  440. return d.builder.commit(d.state, fmt.Sprintf("ENTRYPOINT %q", runConfig.Entrypoint))
  441. }
  442. // EXPOSE 6667/tcp 7000/tcp
  443. //
  444. // Expose ports for links and port mappings. This all ends up in
  445. // req.runConfig.ExposedPorts for runconfig.
  446. func dispatchExpose(d dispatchRequest, c *instructions.ExposeCommand, envs []string) error {
  447. // custom multi word expansion
  448. // expose $FOO with FOO="80 443" is expanded as EXPOSE [80,443]. This is the only command supporting word to words expansion
  449. // so the word processing has been de-generalized
  450. ports := []string{}
  451. for _, p := range c.Ports {
  452. ps, err := d.shlex.ProcessWords(p, envs)
  453. if err != nil {
  454. return err
  455. }
  456. ports = append(ports, ps...)
  457. }
  458. c.Ports = ports
  459. ps, _, err := nat.ParsePortSpecs(ports)
  460. if err != nil {
  461. return err
  462. }
  463. if d.state.runConfig.ExposedPorts == nil {
  464. d.state.runConfig.ExposedPorts = make(nat.PortSet)
  465. }
  466. for p := range ps {
  467. d.state.runConfig.ExposedPorts[p] = struct{}{}
  468. }
  469. return d.builder.commit(d.state, "EXPOSE "+strings.Join(c.Ports, " "))
  470. }
  471. // USER foo
  472. //
  473. // Set the user to 'foo' for future commands and when running the
  474. // ENTRYPOINT/CMD at container run time.
  475. func dispatchUser(d dispatchRequest, c *instructions.UserCommand) error {
  476. d.state.runConfig.User = c.User
  477. return d.builder.commit(d.state, fmt.Sprintf("USER %v", c.User))
  478. }
  479. // VOLUME /foo
  480. //
  481. // Expose the volume /foo for use. Will also accept the JSON array form.
  482. func dispatchVolume(d dispatchRequest, c *instructions.VolumeCommand) error {
  483. if d.state.runConfig.Volumes == nil {
  484. d.state.runConfig.Volumes = map[string]struct{}{}
  485. }
  486. for _, v := range c.Volumes {
  487. if v == "" {
  488. return errors.New("VOLUME specified can not be an empty string")
  489. }
  490. d.state.runConfig.Volumes[v] = struct{}{}
  491. }
  492. return d.builder.commit(d.state, fmt.Sprintf("VOLUME %v", c.Volumes))
  493. }
  494. // STOPSIGNAL signal
  495. //
  496. // Set the signal that will be used to kill the container.
  497. func dispatchStopSignal(d dispatchRequest, c *instructions.StopSignalCommand) error {
  498. _, err := signal.ParseSignal(c.Signal)
  499. if err != nil {
  500. return errdefs.InvalidParameter(err)
  501. }
  502. d.state.runConfig.StopSignal = c.Signal
  503. return d.builder.commit(d.state, fmt.Sprintf("STOPSIGNAL %v", c.Signal))
  504. }
  505. // ARG name[=value]
  506. //
  507. // Adds the variable foo to the trusted list of variables that can be passed
  508. // to builder using the --build-arg flag for expansion/substitution or passing to 'run'.
  509. // Dockerfile author may optionally set a default value of this variable.
  510. func dispatchArg(d dispatchRequest, c *instructions.ArgCommand) error {
  511. var commitStr strings.Builder
  512. commitStr.WriteString("ARG ")
  513. for i, arg := range c.Args {
  514. if i > 0 {
  515. commitStr.WriteString(" ")
  516. }
  517. commitStr.WriteString(arg.Key)
  518. if arg.Value != nil {
  519. commitStr.WriteString("=")
  520. commitStr.WriteString(*arg.Value)
  521. }
  522. d.state.buildArgs.AddArg(arg.Key, arg.Value)
  523. }
  524. return d.builder.commit(d.state, commitStr.String())
  525. }
  526. // SHELL powershell -command
  527. //
  528. // Set the non-default shell to use.
  529. func dispatchShell(d dispatchRequest, c *instructions.ShellCommand) error {
  530. d.state.runConfig.Shell = c.Shell
  531. return d.builder.commit(d.state, fmt.Sprintf("SHELL %v", d.state.runConfig.Shell))
  532. }