run.rst 16 KB

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