run.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. :title: Docker Run Reference
  2. :description: Configure containers at runtime
  3. :keywords: docker, run, configure, runtime
  4. .. _run_docker:
  5. ====================
  6. Docker Run Reference
  7. ====================
  8. **Docker runs processes in isolated containers**. When an operator
  9. executes ``docker run``, she starts a process with its own file
  10. system, its own networking, and its own isolated process tree. The
  11. :ref:`image_def` which starts the process may define defaults related
  12. to the binary to run, the networking to expose, and more, but ``docker
  13. run`` gives final control to the operator who starts the container
  14. from the image. That's the main reason :ref:`cli_run` has more options
  15. than any other ``docker`` command.
  16. Every one of the :ref:`example_list` shows running containers, and so
  17. here we try to give more in-depth guidance.
  18. .. contents:: Table of Contents
  19. :depth: 2
  20. .. _run_running:
  21. General Form
  22. ============
  23. As you've seen in the :ref:`example_list`, the basic `run` command
  24. takes this form::
  25. docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]
  26. To learn how to interpret the types of ``[OPTIONS]``, see
  27. :ref:`cli_options`.
  28. The list of ``[OPTIONS]`` breaks down into two groups:
  29. 1. Settings exclusive to operators, including:
  30. * Detached or Foreground running,
  31. * Container Identification,
  32. * Network settings, and
  33. * Runtime Constraints on CPU and Memory
  34. * Privileges and LXC Configuration
  35. 2. Setting shared between operators and developers, where operators
  36. can override defaults developers set in images at build time.
  37. Together, the ``docker run [OPTIONS]`` give complete control over
  38. runtime behavior to the operator, allowing them to override all
  39. defaults set by the developer during ``docker build`` and nearly all
  40. the defaults set by the Docker runtime itself.
  41. Operator Exclusive Options
  42. ==========================
  43. Only the operator (the person executing ``docker run``) can set the
  44. following options.
  45. .. contents::
  46. :local:
  47. Detached vs Foreground
  48. ----------------------
  49. When starting a Docker container, you must first decide if you want to
  50. run the container in the background in a "detached" mode or in the
  51. default foreground mode::
  52. -d=false: Detached mode: Run container in the background, print new container id
  53. Detached (-d)
  54. .............
  55. In detached mode (``-d=true`` or just ``-d``), all I/O should be done
  56. through network connections or shared volumes because the container is
  57. no longer listening to the commandline where you executed ``docker
  58. run``. You can reattach to a detached container with ``docker``
  59. :ref:`cli_attach`. If you choose to run a container in the detached
  60. mode, then you cannot use the ``--rm`` option.
  61. Foreground
  62. ..........
  63. In foreground mode (the default when ``-d`` is not specified),
  64. ``docker run`` can start the process in the container and attach the
  65. console to the process's standard input, output, and standard
  66. error. It can even pretend to be a TTY (this is what most commandline
  67. executables expect) and pass along signals. All of that is
  68. configurable::
  69. -a=[] : Attach to ``stdin``, ``stdout`` and/or ``stderr``
  70. -t=false : Allocate a pseudo-tty
  71. --sig-proxy=true: Proxify all received signal to the process (even in non-tty mode)
  72. -i=false : Keep STDIN open even if not attached
  73. If you do not specify ``-a`` then Docker will `attach everything
  74. (stdin,stdout,stderr)
  75. <https://github.com/dotcloud/docker/blob/75a7f4d90cde0295bcfb7213004abce8d4779b75/commands.go#L1797>`_. You
  76. can specify to which of the three standard streams (``stdin``, ``stdout``,
  77. ``stderr``) you'd like to connect instead, as in::
  78. docker run -a stdin -a stdout -i -t ubuntu /bin/bash
  79. For interactive processes (like a shell) you will typically want a tty
  80. as well as persistent standard input (``stdin``), so you'll use ``-i
  81. -t`` together in most interactive cases.
  82. Container Identification
  83. ------------------------
  84. Name (--name)
  85. .............
  86. The operator can identify a container in three ways:
  87. * UUID long identifier ("f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778")
  88. * UUID short identifier ("f78375b1c487")
  89. * Name ("evil_ptolemy")
  90. The UUID identifiers come from the Docker daemon, and if you do not
  91. assign a name to the container with ``--name`` then the daemon will
  92. also generate a random string name too. The name can become a handy
  93. way to add meaning to a container since you can use this name when
  94. defining :ref:`links <working_with_links_names>` (or any other place
  95. you need to identify a container). This works for both background and
  96. foreground Docker containers.
  97. PID Equivalent
  98. ..............
  99. And finally, to help with automation, you can have Docker write the
  100. container ID out to a file of your choosing. This is similar to how
  101. some programs might write out their process ID to a file (you've seen
  102. them as PID files)::
  103. --cidfile="": Write the container ID to the file
  104. Network Settings
  105. ----------------
  106. ::
  107. -n=true : Enable networking for this container
  108. --dns=[] : Set custom dns servers for the container
  109. By default, all containers have networking enabled and they can make
  110. any outgoing connections. The operator can completely disable
  111. networking with ``docker run -n`` which disables all incoming and outgoing
  112. networking. In cases like this, you would perform I/O through files or
  113. STDIN/STDOUT only.
  114. Your container will use the same DNS servers as the host by default,
  115. but you can override this with ``--dns``.
  116. Clean Up (--rm)
  117. ---------------
  118. By default a container's file system persists even after the container
  119. exits. This makes debugging a lot easier (since you can inspect the
  120. final state) and you retain all your data by default. But if you are
  121. running short-term **foreground** processes, these container file
  122. systems can really pile up. If instead you'd like Docker to
  123. **automatically clean up the container and remove the file system when
  124. the container exits**, you can add the ``--rm`` flag::
  125. --rm=false: Automatically remove the container when it exits (incompatible with -d)
  126. Runtime Constraints on CPU and Memory
  127. -------------------------------------
  128. The operator can also adjust the performance parameters of the container::
  129. -m="": Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
  130. -c=0 : CPU shares (relative weight)
  131. The operator can constrain the memory available to a container easily
  132. with ``docker run -m``. If the host supports swap memory, then the
  133. ``-m`` memory setting can be larger than physical RAM.
  134. Similarly the operator can increase the priority of this container
  135. with the ``-c`` option. By default, all containers run at the same
  136. priority and get the same proportion of CPU cycles, but you can tell
  137. the kernel to give more shares of CPU time to one or more containers
  138. when you start them via Docker.
  139. Runtime Privilege and LXC Configuration
  140. ---------------------------------------
  141. ::
  142. --privileged=false: Give extended privileges to this container
  143. --lxc-conf=[]: Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
  144. By default, Docker containers are "unprivileged" and cannot, for
  145. example, run a Docker daemon inside a Docker container. This is
  146. because by default a container is not allowed to access any devices,
  147. but a "privileged" container is given access to all devices (see
  148. lxc-template.go_ and documentation on `cgroups devices
  149. <https://www.kernel.org/doc/Documentation/cgroups/devices.txt>`_).
  150. When the operator executes ``docker run --privileged``, Docker will
  151. enable to access to all devices on the host as well as set some
  152. configuration in AppArmor to allow the container nearly all the same
  153. access to the host as processes running outside containers on the
  154. host. Additional information about running with ``--privileged`` is
  155. available on the `Docker Blog
  156. <http://blog.docker.io/2013/09/docker-can-now-run-within-docker/>`_.
  157. An operator can also specify LXC options using one or more
  158. ``--lxc-conf`` parameters. These can be new parameters or override
  159. existing parameters from the lxc-template.go_. Note that in the
  160. future, a given host's Docker daemon may not use LXC, so this is an
  161. implementation-specific configuration meant for operators already
  162. familiar with using LXC directly.
  163. .. _lxc-template.go: https://github.com/dotcloud/docker/blob/master/execdriver/lxc/lxc_template.go
  164. Overriding ``Dockerfile`` Image Defaults
  165. ========================================
  166. When a developer builds an image from a :ref:`Dockerfile
  167. <dockerbuilder>` or when she commits it, the developer can set a
  168. number of default parameters that take effect when the image starts up
  169. as a container.
  170. Four of the ``Dockerfile`` commands cannot be overridden at runtime:
  171. ``FROM, MAINTAINER, RUN``, and ``ADD``. Everything else has a
  172. corresponding override in ``docker run``. We'll go through what the
  173. developer might have set in each ``Dockerfile`` instruction and how the
  174. operator can override that setting.
  175. .. contents::
  176. :local:
  177. CMD (Default Command or Options)
  178. --------------------------------
  179. Recall the optional ``COMMAND`` in the Docker commandline::
  180. docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]
  181. This command is optional because the person who created the ``IMAGE``
  182. may have already provided a default ``COMMAND`` using the ``Dockerfile``
  183. ``CMD``. As the operator (the person running a container from the
  184. image), you can override that ``CMD`` just by specifying a new
  185. ``COMMAND``.
  186. If the image also specifies an ``ENTRYPOINT`` then the ``CMD`` or
  187. ``COMMAND`` get appended as arguments to the ``ENTRYPOINT``.
  188. ENTRYPOINT (Default Command to Execute at Runtime
  189. -------------------------------------------------
  190. ::
  191. --entrypoint="": Overwrite the default entrypoint set by the image
  192. The ENTRYPOINT of an image is similar to a ``COMMAND`` because it
  193. specifies what executable to run when the container starts, but it is
  194. (purposely) more difficult to override. The ``ENTRYPOINT`` gives a
  195. container its default nature or behavior, so that when you set an
  196. ``ENTRYPOINT`` you can run the container *as if it were that binary*,
  197. complete with default options, and you can pass in more options via
  198. the ``COMMAND``. But, sometimes an operator may want to run something else
  199. inside the container, so you can override the default ``ENTRYPOINT`` at
  200. runtime by using a string to specify the new ``ENTRYPOINT``. Here is an
  201. example of how to run a shell in a container that has been set up to
  202. automatically run something else (like ``/usr/bin/redis-server``)::
  203. docker run -i -t --entrypoint /bin/bash example/redis
  204. or two examples of how to pass more parameters to that ENTRYPOINT::
  205. docker run -i -t --entrypoint /bin/bash example/redis -c ls -l
  206. docker run -i -t --entrypoint /usr/bin/redis-cli example/redis --help
  207. EXPOSE (Incoming Ports)
  208. -----------------------
  209. The ``Dockerfile`` doesn't give much control over networking, only
  210. providing the ``EXPOSE`` instruction to give a hint to the operator
  211. about what incoming ports might provide services. The following
  212. options work with or override the ``Dockerfile``'s exposed defaults::
  213. --expose=[]: Expose a port from the container
  214. without publishing it to your host
  215. -P=false : Publish all exposed ports to the host interfaces
  216. -p=[] : Publish a container's port to the host (format:
  217. ip:hostPort:containerPort | ip::containerPort |
  218. hostPort:containerPort)
  219. (use 'docker port' to see the actual mapping)
  220. --link="" : Add link to another container (name:alias)
  221. As mentioned previously, ``EXPOSE`` (and ``--expose``) make a port
  222. available **in** a container for incoming connections. The port number
  223. on the inside of the container (where the service listens) does not
  224. need to be the same number as the port exposed on the outside of the
  225. container (where clients connect), so inside the container you might
  226. have an HTTP service listening on port 80 (and so you ``EXPOSE 80`` in
  227. the ``Dockerfile``), but outside the container the port might be 42800.
  228. To help a new client container reach the server container's internal
  229. port operator ``--expose``'d by the operator or ``EXPOSE``'d by the
  230. developer, the operator has three choices: start the server container
  231. with ``-P`` or ``-p,`` or start the client container with ``--link``.
  232. If the operator uses ``-P`` or ``-p`` then Docker will make the
  233. exposed port accessible on the host and the ports will be available to
  234. any client that can reach the host. To find the map between the host
  235. ports and the exposed ports, use ``docker port``)
  236. If the operator uses ``--link`` when starting the new client container,
  237. then the client container can access the exposed port via a private
  238. networking interface. Docker will set some environment variables in
  239. the client container to help indicate which interface and port to use.
  240. ENV (Environment Variables)
  241. ---------------------------
  242. The operator can **set any environment variable** in the container by
  243. using one or more ``-e`` flags, even overriding those already defined by the
  244. developer with a Dockefile ``ENV``::
  245. $ docker run -e "deep=purple" --rm ubuntu /bin/bash -c export
  246. declare -x HOME="/"
  247. declare -x HOSTNAME="85bc26a0e200"
  248. declare -x OLDPWD
  249. declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  250. declare -x PWD="/"
  251. declare -x SHLVL="1"
  252. declare -x container="lxc"
  253. declare -x deep="purple"
  254. Similarly the operator can set the **hostname** with ``-h``.
  255. ``--link name:alias`` also sets environment variables, using the
  256. *alias* string to define environment variables within the container
  257. that give the IP and PORT information for connecting to the service
  258. container. Let's imagine we have a container running Redis::
  259. # Start the service container, named redis-name
  260. $ docker run -d --name redis-name dockerfiles/redis
  261. 4241164edf6f5aca5b0e9e4c9eccd899b0b8080c64c0cd26efe02166c73208f3
  262. # The redis-name container exposed port 6379
  263. $ docker ps
  264. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  265. 4241164edf6f dockerfiles/redis:latest /redis-stable/src/re 5 seconds ago Up 4 seconds 6379/tcp redis-name
  266. # Note that there are no public ports exposed since we didn't use -p or -P
  267. $ docker port 4241164edf6f 6379
  268. 2014/01/25 00:55:38 Error: No public port '6379' published for 4241164edf6f
  269. Yet we can get information about the Redis container's exposed ports
  270. with ``--link``. Choose an alias that will form a valid environment
  271. variable!
  272. ::
  273. $ docker run --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c export
  274. declare -x HOME="/"
  275. declare -x HOSTNAME="acda7f7b1cdc"
  276. declare -x OLDPWD
  277. declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  278. declare -x PWD="/"
  279. declare -x REDIS_ALIAS_NAME="/distracted_wright/redis"
  280. declare -x REDIS_ALIAS_PORT="tcp://172.17.0.32:6379"
  281. declare -x REDIS_ALIAS_PORT_6379_TCP="tcp://172.17.0.32:6379"
  282. declare -x REDIS_ALIAS_PORT_6379_TCP_ADDR="172.17.0.32"
  283. declare -x REDIS_ALIAS_PORT_6379_TCP_PORT="6379"
  284. declare -x REDIS_ALIAS_PORT_6379_TCP_PROTO="tcp"
  285. declare -x SHLVL="1"
  286. declare -x container="lxc"
  287. And we can use that information to connect from another container as a client::
  288. $ docker run -i -t --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c '/redis-stable/src/redis-cli -h $REDIS_ALIAS_PORT_6379_TCP_ADDR -p $REDIS_ALIAS_PORT_6379_TCP_PORT'
  289. 172.17.0.32:6379>
  290. VOLUME (Shared Filesystems)
  291. ---------------------------
  292. ::
  293. -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro].
  294. If "container-dir" is missing, then docker creates a new volume.
  295. --volumes-from="": Mount all volumes from the given container(s)
  296. The volumes commands are complex enough to have their own
  297. documentation in section :ref:`volume_def`. A developer can define one
  298. or more ``VOLUME``\s associated with an image, but only the operator can
  299. give access from one container to another (or from a container to a
  300. volume mounted on the host).
  301. USER
  302. ----
  303. The default user within a container is ``root`` (id = 0), but if the
  304. developer created additional users, those are accessible too. The
  305. developer can set a default user to run the first process with the
  306. ``Dockerfile USER`` command, but the operator can override it ::
  307. -u="": Username or UID
  308. WORKDIR
  309. -------
  310. The default working directory for running binaries within a container is the root directory (``/``), but the developer can set a different default with the ``Dockerfile WORKDIR`` command. The operator can override this with::
  311. -w="": Working directory inside the container