cli.rst 18 KB

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