basics.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 are local to the Dockerfile, they will not persist
  75. when a container is run from the resulting image.
  76. 2.7 INSERT
  77. ----------
  78. ``INSERT <file url> <path>``
  79. The `INSERT` instruction will download the file from the given url to the given
  80. path within the image. It is similar to `RUN curl -o <path> <url>`, assuming
  81. curl was installed within the image.
  82. .. note::
  83. The path must include the file name.
  84. 3. Dockerfile Examples
  85. ======================
  86. .. code-block:: bash
  87. # Nginx
  88. #
  89. # VERSION 0.0.1
  90. FROM ubuntu
  91. MAINTAINER Guillaume J. Charmes "guillaume@dotcloud.com"
  92. # make sure the package repository is up to date
  93. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  94. RUN apt-get update
  95. RUN apt-get install -y inotify-tools nginx apache2 openssh-server
  96. INSERT https://raw.github.com/creack/docker-vps/master/nginx-wrapper.sh /usr/sbin/nginx-wrapper
  97. .. code-block:: bash
  98. # Firefox over VNC
  99. #
  100. # VERSION 0.3
  101. FROM ubuntu
  102. # make sure the package repository is up to date
  103. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  104. RUN apt-get update
  105. # Install vnc, xvfb in order to create a 'fake' display and firefox
  106. RUN apt-get install -y x11vnc xvfb firefox
  107. RUN mkdir /.vnc
  108. # Setup a password
  109. RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
  110. # Autostart firefox (might not be the best way, but it does the trick)
  111. RUN bash -c 'echo "firefox" >> /.bashrc'
  112. EXPOSE 5900
  113. CMD ["x11vnc", "-forever", "-usepw", "-create"]
  114. .. code-block:: bash
  115. # Multiple images example
  116. #
  117. # VERSION 0.1
  118. FROM ubuntu
  119. RUN echo foo > bar
  120. # Will output something like ===> 907ad6c2736f
  121. FROM ubuntu
  122. RUN echo moo > oink
  123. # Will output something like ===> 695d7793cbe4
  124. # You'll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
  125. # /oink.