cli.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. :title: Command Line Interface
  2. :description: Docker's CLI command description and usage
  3. :keywords: Docker, Docker documentation, CLI, command line
  4. .. _cli:
  5. Command Line Help
  6. -----------------
  7. To list available commands, either run ``docker`` with no parameters or execute
  8. ``docker help``::
  9. $ sudo docker
  10. Usage: docker [OPTIONS] COMMAND [arg...]
  11. -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to use
  12. A self-sufficient runtime for linux containers.
  13. ...
  14. .. _cli_attach:
  15. ``attach``
  16. ----------
  17. ::
  18. Usage: docker attach CONTAINER
  19. Attach to a running container.
  20. You can detach from the container again (and leave it running) with
  21. ``CTRL-c`` (for a quiet exit) or ``CTRL-\`` to get a stacktrace of
  22. the Docker client when it quits.
  23. To stop a container, use ``docker stop``
  24. To kill the container, use ``docker kill``
  25. .. _cli_attach_examples:
  26. Examples:
  27. ~~~~~~~~~
  28. .. code-block:: bash
  29. $ ID=$(sudo docker run -d ubuntu /usr/bin/top -b)
  30. $ sudo docker attach $ID
  31. top - 02:05:52 up 3:05, 0 users, load average: 0.01, 0.02, 0.05
  32. Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
  33. Cpu(s): 0.1%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
  34. Mem: 373572k total, 355560k used, 18012k free, 27872k buffers
  35. Swap: 786428k total, 0k used, 786428k free, 221740k cached
  36. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  37. 1 root 20 0 17200 1116 912 R 0 0.3 0:00.03 top
  38. top - 02:05:55 up 3:05, 0 users, load average: 0.01, 0.02, 0.05
  39. Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
  40. Cpu(s): 0.0%us, 0.2%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
  41. Mem: 373572k total, 355244k used, 18328k free, 27872k buffers
  42. Swap: 786428k total, 0k used, 786428k free, 221776k cached
  43. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  44. 1 root 20 0 17208 1144 932 R 0 0.3 0:00.03 top
  45. top - 02:05:58 up 3:06, 0 users, load average: 0.01, 0.02, 0.05
  46. Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
  47. Cpu(s): 0.2%us, 0.3%sy, 0.0%ni, 99.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
  48. Mem: 373572k total, 355780k used, 17792k free, 27880k buffers
  49. Swap: 786428k total, 0k used, 786428k free, 221776k cached
  50. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  51. 1 root 20 0 17208 1144 932 R 0 0.3 0:00.03 top
  52. ^C$
  53. $ sudo docker stop $ID
  54. .. _cli_build:
  55. ``build``
  56. ---------
  57. ::
  58. Usage: docker build [OPTIONS] PATH | URL | -
  59. Build a new container image from the source code at PATH
  60. -t="": Repository name (and optionally a tag) to be applied to the resulting image in case of success.
  61. -q=false: Suppress verbose build output.
  62. -no-cache: Do not use the cache when building the image.
  63. -rm: Remove intermediate containers after a successful build
  64. When a single Dockerfile is given as URL, then no context is set. When a git repository is set as URL, the repository is used as context
  65. .. _cli_build_examples:
  66. Examples
  67. ~~~~~~~~
  68. .. code-block:: bash
  69. sudo docker build .
  70. This will read the ``Dockerfile`` from the current directory. It will
  71. also send any other files and directories found in the current
  72. directory to the ``docker`` daemon.
  73. The contents of this directory would be used by ``ADD`` commands found
  74. within the ``Dockerfile``. This will send a lot of data to the
  75. ``docker`` daemon if the current directory contains a lot of data. If
  76. the absolute path is provided instead of ``.`` then only the files and
  77. directories required by the ADD commands from the ``Dockerfile`` will be
  78. added to the context and transferred to the ``docker`` daemon.
  79. .. code-block:: bash
  80. sudo docker build -t vieux/apache:2.0 .
  81. This will build like the previous example, but it will then tag the
  82. resulting image. The repository name will be ``vieux/apache`` and the
  83. tag will be ``2.0``
  84. .. code-block:: bash
  85. sudo docker build - < Dockerfile
  86. This will read a ``Dockerfile`` from *stdin* without context. Due to
  87. the lack of a context, no contents of any local directory will be sent
  88. to the ``docker`` daemon. ``ADD`` doesn't work when running in this
  89. mode because the absence of the context provides no source files to
  90. copy to the container.
  91. .. code-block:: bash
  92. sudo docker build github.com/creack/docker-firefox
  93. This will clone the Github repository and use it as context. The
  94. ``Dockerfile`` at the root of the repository is used as
  95. ``Dockerfile``. Note that you can specify an arbitrary git repository
  96. by using the ``git://`` schema.
  97. .. _cli_commit:
  98. ``commit``
  99. ----------
  100. ::
  101. Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY [TAG]]
  102. Create a new image from a container's changes
  103. -m="": Commit message
  104. -author="": Author (eg. "John Hannibal Smith <hannibal@a-team.com>"
  105. -run="": Configuration to be applied when the image is launched with `docker run`.
  106. (ex: '{"Cmd": ["cat", "/world"], "PortSpecs": ["22"]}')
  107. Full -run example (multiline is ok within a single quote ``'``)
  108. ::
  109. $ sudo docker commit -run='
  110. {
  111. "Entrypoint" : null,
  112. "Privileged" : false,
  113. "User" : "",
  114. "VolumesFrom" : "",
  115. "Cmd" : ["cat", "-e", "/etc/resolv.conf"],
  116. "Dns" : ["8.8.8.8", "8.8.4.4"],
  117. "MemorySwap" : 0,
  118. "AttachStdin" : false,
  119. "AttachStderr" : false,
  120. "CpuShares" : 0,
  121. "OpenStdin" : false,
  122. "Volumes" : null,
  123. "Hostname" : "122612f45831",
  124. "PortSpecs" : ["22", "80", "443"],
  125. "Image" : "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc",
  126. "Tty" : false,
  127. "Env" : [
  128. "HOME=/",
  129. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  130. ],
  131. "StdinOnce" : false,
  132. "Domainname" : "",
  133. "WorkingDir" : "/",
  134. "NetworkDisabled" : false,
  135. "Memory" : 0,
  136. "AttachStdout" : false
  137. }' $CONTAINER_ID
  138. .. _cli_cp:
  139. ``cp``
  140. ------
  141. ::
  142. Usage: docker cp CONTAINER:RESOURCE HOSTPATH
  143. Copy files/folders from the containers filesystem to the host
  144. path. Paths are relative to the root of the filesystem.
  145. .. _cli_diff:
  146. ``diff``
  147. --------
  148. ::
  149. Usage: docker diff CONTAINER [OPTIONS]
  150. Inspect changes on a container's filesystem
  151. .. _cli_events:
  152. ``events``
  153. ----------
  154. ::
  155. Usage: docker events
  156. Get real time events from the server
  157. .. _cli_events_example:
  158. Examples
  159. ~~~~~~~~
  160. You'll need two shells for this example.
  161. Shell 1: Listening for events
  162. .............................
  163. .. code-block:: bash
  164. $ sudo docker events
  165. Shell 2: Start and Stop a Container
  166. ...................................
  167. .. code-block:: bash
  168. $ sudo docker start 4386fb97867d
  169. $ sudo docker stop 4386fb97867d
  170. Shell 1: (Again .. now showing events)
  171. ......................................
  172. .. code-block:: bash
  173. [2013-09-03 15:49:26 +0200 CEST] 4386fb97867d: (from 12de384bfb10) start
  174. [2013-09-03 15:49:29 +0200 CEST] 4386fb97867d: (from 12de384bfb10) die
  175. [2013-09-03 15:49:29 +0200 CEST] 4386fb97867d: (from 12de384bfb10) stop
  176. .. _cli_export:
  177. ``export``
  178. ----------
  179. ::
  180. Usage: docker export CONTAINER
  181. Export the contents of a filesystem as a tar archive
  182. .. _cli_history:
  183. ``history``
  184. -----------
  185. ::
  186. Usage: docker history [OPTIONS] IMAGE
  187. Show the history of an image
  188. .. _cli_images:
  189. ``images``
  190. ----------
  191. ::
  192. Usage: docker images [OPTIONS] [NAME]
  193. List images
  194. -a=false: show all images
  195. -q=false: only show numeric IDs
  196. -viz=false: output in graphviz format
  197. Displaying images visually
  198. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  199. ::
  200. sudo docker images -viz | dot -Tpng -o docker.png
  201. .. image:: docker_images.gif
  202. :alt: Example inheritance graph of Docker images.
  203. .. _cli_import:
  204. ``import``
  205. ----------
  206. ::
  207. Usage: docker import URL|- [REPOSITORY [TAG]]
  208. Create a new filesystem image from the contents of a tarball
  209. At this time, the URL must start with ``http`` and point to a single
  210. file archive (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) containing a
  211. root filesystem. If you would like to import from a local directory or
  212. archive, you can use the ``-`` parameter to take the data from
  213. standard in.
  214. Examples
  215. ~~~~~~~~
  216. Import from a remote location
  217. .............................
  218. ``$ sudo docker import http://example.com/exampleimage.tgz exampleimagerepo``
  219. Import from a local file
  220. ........................
  221. Import to docker via pipe and standard in
  222. ``$ cat exampleimage.tgz | sudo docker import - exampleimagelocal``
  223. Import from a local directory
  224. .............................
  225. ``$ sudo tar -c . | docker import - exampleimagedir``
  226. Note the ``sudo`` in this example -- you must preserve the ownership
  227. of the files (especially root ownership) during the archiving with
  228. tar. If you are not root (or sudo) when you tar, then the ownerships
  229. might not get preserved.
  230. .. _cli_info:
  231. ``info``
  232. --------
  233. ::
  234. Usage: docker info
  235. Display system-wide information.
  236. .. _cli_insert:
  237. ``insert``
  238. ----------
  239. ::
  240. Usage: docker insert IMAGE URL PATH
  241. Insert a file from URL in the IMAGE at PATH
  242. Examples
  243. ~~~~~~~~
  244. Insert file from github
  245. .......................
  246. .. code-block:: bash
  247. $ sudo docker insert 8283e18b24bc https://raw.github.com/metalivedev/django/master/postinstall /tmp/postinstall.sh
  248. .. _cli_inspect:
  249. ``inspect``
  250. -----------
  251. ::
  252. Usage: docker inspect [OPTIONS] CONTAINER
  253. Return low-level information on a container
  254. .. _cli_kill:
  255. ``kill``
  256. --------
  257. ::
  258. Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
  259. Kill a running container
  260. .. _cli_login:
  261. ``login``
  262. ---------
  263. ::
  264. Usage: docker login [OPTIONS] [SERVER]
  265. Register or Login to the docker registry server
  266. -e="": email
  267. -p="": password
  268. -u="": username
  269. If you want to login to a private registry you can
  270. specify this by adding the server name.
  271. example:
  272. docker login localhost:8080
  273. .. _cli_logs:
  274. ``logs``
  275. --------
  276. ::
  277. Usage: docker logs [OPTIONS] CONTAINER
  278. Fetch the logs of a container
  279. .. _cli_port:
  280. ``port``
  281. --------
  282. ::
  283. Usage: docker port [OPTIONS] CONTAINER PRIVATE_PORT
  284. Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
  285. .. _cli_ps:
  286. ``ps``
  287. ------
  288. ::
  289. Usage: docker ps [OPTIONS]
  290. List containers
  291. -a=false: Show all containers. Only running containers are shown by default.
  292. -notrunc=false: Don't truncate output
  293. -q=false: Only display numeric IDs
  294. .. _cli_pull:
  295. ``pull``
  296. --------
  297. ::
  298. Usage: docker pull NAME
  299. Pull an image or a repository from the registry
  300. .. _cli_push:
  301. ``push``
  302. --------
  303. ::
  304. Usage: docker push NAME
  305. Push an image or a repository to the registry
  306. .. _cli_restart:
  307. ``restart``
  308. -----------
  309. ::
  310. Usage: docker restart [OPTIONS] NAME
  311. Restart a running container
  312. .. _cli_rm:
  313. ``rm``
  314. ------
  315. ::
  316. Usage: docker rm [OPTIONS] CONTAINER
  317. Remove one or more containers
  318. .. _cli_rmi:
  319. ``rmi``
  320. -------
  321. ::
  322. Usage: docker rmi IMAGE [IMAGE...]
  323. Remove one or more images
  324. .. _cli_run:
  325. ``run``
  326. -------
  327. ::
  328. Usage: docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]
  329. Run a command in a new container
  330. -a=map[]: Attach to stdin, stdout or stderr.
  331. -c=0: CPU shares (relative weight)
  332. -cidfile="": Write the container ID to the file
  333. -d=false: Detached mode: Run container in the background, print new container id
  334. -e=[]: Set environment variables
  335. -h="": Container host name
  336. -i=false: Keep stdin open even if not attached
  337. -privileged=false: Give extended privileges to this container
  338. -m=0: Memory limit (in bytes)
  339. -n=true: Enable networking for this container
  340. -p=[]: Map a network port to the container
  341. -rm=false: Automatically remove the container when it exits (incompatible with -d)
  342. -t=false: Allocate a pseudo-tty
  343. -u="": Username or UID
  344. -dns=[]: Set custom dns servers for the container
  345. -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. If "container-dir" is missing, then docker creates a new volume.
  346. -volumes-from="": Mount all volumes from the given container.
  347. -entrypoint="": Overwrite the default entrypoint set by the image.
  348. -w="": Working directory inside the container
  349. -lxc-conf=[]: Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
  350. Examples
  351. ~~~~~~~~
  352. .. code-block:: bash
  353. sudo docker run -cidfile /tmp/docker_test.cid ubuntu echo "test"
  354. This will create a container and print "test" to the console. The
  355. ``cidfile`` flag makes docker attempt to create a new file and write the
  356. container ID to it. If the file exists already, docker will return an
  357. error. Docker will close this file when docker run exits.
  358. .. code-block:: bash
  359. docker run mount -t tmpfs none /var/spool/squid
  360. This will *not* work, because by default, most potentially dangerous
  361. kernel capabilities are dropped; including ``cap_sys_admin`` (which is
  362. required to mount filesystems). However, the ``-privileged`` flag will
  363. allow it to run:
  364. .. code-block:: bash
  365. docker run -privileged mount -t tmpfs none /var/spool/squid
  366. The ``-privileged`` flag gives *all* capabilities to the container,
  367. and it also lifts all the limitations enforced by the ``device``
  368. cgroup controller. In other words, the container can then do almost
  369. everything that the host can do. This flag exists to allow special
  370. use-cases, like running Docker within Docker.
  371. .. code-block:: bash
  372. docker run -w /path/to/dir/ -i -t ubuntu pwd
  373. The ``-w`` lets the command being executed inside directory given,
  374. here /path/to/dir/. If the path does not exists it is created inside the
  375. container.
  376. .. code-block:: bash
  377. docker run -v `pwd`:`pwd` -w `pwd` -i -t ubuntu pwd
  378. The ``-v`` flag mounts the current working directory into the container.
  379. The ``-w`` lets the command being executed inside the current
  380. working directory, by changing into the directory to the value
  381. returned by ``pwd``. So this combination executes the command
  382. using the container, but inside the current working directory.
  383. .. _cli_search:
  384. ``search``
  385. ----------
  386. ::
  387. Usage: docker search TERM
  388. Searches for the TERM parameter on the Docker index and prints out
  389. a list of repositories that match.
  390. .. _cli_start:
  391. ``start``
  392. ---------
  393. ::
  394. Usage: docker start [OPTIONS] NAME
  395. Start a stopped container
  396. .. _cli_stop:
  397. ``stop``
  398. --------
  399. ::
  400. Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
  401. Stop a running container
  402. -t=10: Number of seconds to wait for the container to stop before killing it.
  403. .. _cli_tag:
  404. ``tag``
  405. -------
  406. ::
  407. Usage: docker tag [OPTIONS] IMAGE REPOSITORY [TAG]
  408. Tag an image into a repository
  409. -f=false: Force
  410. .. _cli_top:
  411. ``top``
  412. -------
  413. ::
  414. Usage: docker top CONTAINER
  415. Lookup the running processes of a container
  416. .. _cli_version:
  417. ``version``
  418. -----------
  419. Show the version of the docker client, daemon, and latest released version.
  420. .. _cli_wait:
  421. ``wait``
  422. --------
  423. ::
  424. Usage: docker wait [OPTIONS] NAME
  425. Block until a container stops, then print its exit code.