dispatchers.go 18 KB

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