run.rst 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. :title: Run Command
  2. :description: Run a command in a new container
  3. :keywords: run, container, docker, documentation
  4. ===========================================
  5. ``run`` -- Run a command in a new container
  6. ===========================================
  7. ::
  8. Usage: docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]
  9. Run a command in a new container
  10. -a=map[]: Attach to stdin, stdout or stderr.
  11. -c=0: CPU shares (relative weight)
  12. -cidfile="": Write the container ID to the file
  13. -d=false: Detached mode: Run container in the background, print new container id
  14. -e=[]: Set environment variables
  15. -h="": Container host name
  16. -i=false: Keep stdin open even if not attached
  17. -privileged=false: Give extended privileges to this container
  18. -m=0: Memory limit (in bytes)
  19. -n=true: Enable networking for this container
  20. -p=[]: Map a network port to the container
  21. -rm=false: Automatically remove the container when it exits (incompatible with -d)
  22. -t=false: Allocate a pseudo-tty
  23. -u="": Username or UID
  24. -dns=[]: Set custom dns servers for the container
  25. -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. If "host-dir" is missing, then docker creates a new volume.
  26. -volumes-from="": Mount all volumes from the given container.
  27. -entrypoint="": Overwrite the default entrypoint set by the image.
  28. -w="": Working directory inside the container
  29. -lxc-conf=[]: Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
  30. Examples
  31. --------
  32. .. code-block:: bash
  33. sudo docker run -cidfile /tmp/docker_test.cid ubuntu echo "test"
  34. This will create a container and print "test" to the console. The
  35. ``cidfile`` flag makes docker attempt to create a new file and write the
  36. container ID to it. If the file exists already, docker will return an
  37. error. Docker will close this file when docker run exits.
  38. .. code-block:: bash
  39. docker run mount -t tmpfs none /var/spool/squid
  40. This will *not* work, because by default, most potentially dangerous
  41. kernel capabilities are dropped; including ``cap_sys_admin`` (which is
  42. required to mount filesystems). However, the ``-privileged`` flag will
  43. allow it to run:
  44. .. code-block:: bash
  45. docker run -privileged mount -t tmpfs none /var/spool/squid
  46. The ``-privileged`` flag gives *all* capabilities to the container,
  47. and it also lifts all the limitations enforced by the ``device``
  48. cgroup controller. In other words, the container can then do almost
  49. everything that the host can do. This flag exists to allow special
  50. use-cases, like running Docker within Docker.
  51. .. code-block:: bash
  52. docker run -w /path/to/dir/ -i -t ubuntu pwd
  53. The ``-w`` lets the command being executed inside directory given,
  54. here /path/to/dir/. If the path does not exists it is created inside the
  55. container.
  56. .. code-block:: bash
  57. docker run -v `pwd`:`pwd` -w `pwd` -i -t ubuntu pwd
  58. The ``-v`` flag mounts the current working directory into the container.
  59. The ``-w`` lets the command being executed inside the current
  60. working directory, by changing into the directory to the value
  61. returned by ``pwd``. So this combination executes the command
  62. using the container, but inside the current working directory.