dispatchers.go 17 KB

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