builder.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ==============
  2. Docker Builder
  3. ==============
  4. .. contents:: Table of Contents
  5. Docker Builder specifes a simple DSL which allows you to automate the steps you
  6. would normally manually take to create an image. Docker Build will run your
  7. steps and commit them along the way, giving you a final image.
  8. 1. Usage
  9. ========
  10. To use Docker Builder, assemble the steps into a text file (commonly referred to
  11. as a Dockerfile) and supply this to `docker build` on STDIN, like so:
  12. ``docker build < Dockerfile``
  13. Docker will run your steps one-by-one, committing the result if necessary,
  14. before finally outputting the ID of your new image.
  15. 2. Format
  16. =========
  17. The Dockerfile format is quite simple:
  18. ``instruction arguments``
  19. The Instruction is not case-sensitive, however convention is for them to be
  20. UPPERCASE in order to distinguish them from arguments more easily.
  21. Dockerfiles are evaluated in order, therefore the first instruction must be
  22. `FROM` in order to specify the base image from which you are building.
  23. Docker will ignore lines in Dockerfiles prefixed with "`#`", so you may add
  24. comment lines. A comment marker in the rest of the line will be treated as an
  25. argument.
  26. 2. Instructions
  27. ===============
  28. Docker builder comes with a set of instructions, described below.
  29. 2.1 FROM
  30. --------
  31. ``FROM <image>``
  32. The `FROM` instruction sets the base image for subsequent instructions. As such,
  33. a valid Dockerfile must have it as its first instruction.
  34. `FROM` can be included multiple times within a single Dockerfile in order to
  35. create multiple images. Simply make a note of the last image id output by the
  36. commit before each new `FROM` command.
  37. 2.2 MAINTAINER
  38. --------------
  39. ``MAINTAINER <name>``
  40. The `MAINTAINER` instruction allows you to set the Author field of the generated
  41. images.
  42. 2.3 RUN
  43. -------
  44. ``RUN <command>``
  45. The `RUN` instruction will execute any commands on the current image and commit
  46. the results. The resulting committed image will be used for the next step in the
  47. Dockerfile.
  48. Layering `RUN` instructions and generating commits conforms to the
  49. core concepts of Docker where commits are cheap and containers can be created
  50. from any point in an image's history, much like source control.
  51. 2.4 CMD
  52. -------
  53. ``CMD <command>``
  54. The `CMD` instruction sets the command to be executed when running the image.
  55. This is functionally equivalent to running
  56. `docker commit -run '{"Cmd": <command>}'` outside the builder.
  57. .. note::
  58. Don't confuse `RUN` with `CMD`. `RUN` actually runs a command and commits
  59. the result; `CMD` does not execute anything at build time, but specifies the
  60. intended command for the image.
  61. 2.5 EXPOSE
  62. ----------
  63. ``EXPOSE <port> [<port>...]``
  64. The `EXPOSE` instruction sets ports to be publicly exposed when running the
  65. image. This is functionally equivalent to running
  66. `docker commit -run '{"PortSpecs": ["<port>", "<port2>"]}'` outside the builder.
  67. 2.6 ENV
  68. -------
  69. ``ENV <key> <value>``
  70. The `ENV` instruction sets the environment variable `<key>` to the value
  71. `<value>`. This value will be passed to all future ``RUN`` instructions. This is
  72. functionally equivalent to prefixing the command with `<key>=<value>`
  73. .. note::
  74. The environment variables will persist when a container is run from the resulting image.
  75. 2.7 INSERT
  76. ----------
  77. ``INSERT <file url> <path>``
  78. The `INSERT` instruction will download the file from the given url to the given
  79. path within the image. It is similar to `RUN curl -o <path> <url>`, assuming
  80. curl was installed within the image.
  81. .. note::
  82. The path must include the file name.
  83. .. note::
  84. This instruction has temporarily disabled
  85. 3. Dockerfile Examples
  86. ======================
  87. .. code-block:: bash
  88. # Nginx
  89. #
  90. # VERSION 0.0.1
  91. FROM ubuntu
  92. MAINTAINER Guillaume J. Charmes "guillaume@dotcloud.com"
  93. # make sure the package repository is up to date
  94. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  95. RUN apt-get update
  96. RUN apt-get install -y inotify-tools nginx apache2 openssh-server
  97. INSERT https://raw.github.com/creack/docker-vps/master/nginx-wrapper.sh /usr/sbin/nginx-wrapper
  98. .. code-block:: bash
  99. # Firefox over VNC
  100. #
  101. # VERSION 0.3
  102. FROM ubuntu
  103. # make sure the package repository is up to date
  104. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  105. RUN apt-get update
  106. # Install vnc, xvfb in order to create a 'fake' display and firefox
  107. RUN apt-get install -y x11vnc xvfb firefox
  108. RUN mkdir /.vnc
  109. # Setup a password
  110. RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
  111. # Autostart firefox (might not be the best way, but it does the trick)
  112. RUN bash -c 'echo "firefox" >> /.bashrc'
  113. EXPOSE 5900
  114. CMD ["x11vnc", "-forever", "-usepw", "-create"]
  115. .. code-block:: bash
  116. # Multiple images example
  117. #
  118. # VERSION 0.1
  119. FROM ubuntu
  120. RUN echo foo > bar
  121. # Will output something like ===> 907ad6c2736f
  122. FROM ubuntu
  123. RUN echo moo > oink
  124. # Will output something like ===> 695d7793cbe4
  125. # You'll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
  126. # /oink.