dispatchers.go 16 KB

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