docker 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. #!/bin/bash
  2. #
  3. # bash completion file for core docker commands
  4. #
  5. # This script provides completion of:
  6. # - commands and their options
  7. # - container ids and names
  8. # - image repos and tags
  9. # - filepaths
  10. #
  11. # To enable the completions either:
  12. # - place this file in /etc/bash_completion.d
  13. # or
  14. # - copy this file to e.g. ~/.docker-completion.sh and add the line
  15. # below to your .bashrc after bash completion features are loaded
  16. # . ~/.docker-completion.sh
  17. #
  18. # Note:
  19. # Currently, the completions will not work if the docker daemon is not
  20. # bound to the default communication port/socket
  21. # If the docker daemon is using a unix socket for communication your user
  22. # must have access to the socket for the completions to function correctly
  23. #
  24. # Note for developers:
  25. # Please arrange options sorted alphabetically by long name with the short
  26. # options immediately following their corresponding long form.
  27. # This order should be applied to lists, alternatives and code blocks.
  28. __docker_q() {
  29. docker 2>/dev/null "$@"
  30. }
  31. __docker_containers_all() {
  32. local IFS=$'\n'
  33. local containers=( $(__docker_q ps -aq --no-trunc) )
  34. if [ "$1" ]; then
  35. containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") )
  36. fi
  37. local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
  38. names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
  39. unset IFS
  40. COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") )
  41. }
  42. __docker_containers_running() {
  43. __docker_containers_all '.State.Running'
  44. }
  45. __docker_containers_stopped() {
  46. __docker_containers_all 'not .State.Running'
  47. }
  48. __docker_containers_pauseable() {
  49. __docker_containers_all 'and .State.Running (not .State.Paused)'
  50. }
  51. __docker_containers_unpauseable() {
  52. __docker_containers_all '.State.Paused'
  53. }
  54. __docker_image_repos() {
  55. local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
  56. COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
  57. }
  58. __docker_image_repos_and_tags() {
  59. local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
  60. COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
  61. __ltrim_colon_completions "$cur"
  62. }
  63. __docker_image_repos_and_tags_and_ids() {
  64. local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')"
  65. COMPREPLY=( $(compgen -W "$images" -- "$cur") )
  66. __ltrim_colon_completions "$cur"
  67. }
  68. __docker_containers_and_images() {
  69. __docker_containers_all
  70. local containers=( "${COMPREPLY[@]}" )
  71. __docker_image_repos_and_tags_and_ids
  72. COMPREPLY+=( "${containers[@]}" )
  73. }
  74. __docker_pos_first_nonflag() {
  75. local argument_flags=$1
  76. local counter=$cpos
  77. while [ $counter -le $cword ]; do
  78. if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
  79. (( counter++ ))
  80. else
  81. case "${words[$counter]}" in
  82. -*)
  83. ;;
  84. *)
  85. break
  86. ;;
  87. esac
  88. fi
  89. (( counter++ ))
  90. done
  91. echo $counter
  92. }
  93. # Transforms a multiline list of strings into a single line string
  94. # with the words separated by "|".
  95. # This is used to prepare arguments to __docker_pos_first_nonflag().
  96. __docker_to_alternatives() {
  97. local parts=( $1 )
  98. local IFS='|'
  99. echo "${parts[*]}"
  100. }
  101. # Transforms a multiline list of options into an extglob pattern
  102. # suitable for use in case statements.
  103. __docker_to_extglob() {
  104. local extglob=$( __docker_to_alternatives "$1" )
  105. echo "@($extglob)"
  106. }
  107. __docker_resolve_hostname() {
  108. command -v host >/dev/null 2>&1 || return
  109. COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
  110. }
  111. __docker_capabilities() {
  112. # The list of capabilities is defined in types.go, ALL was added manually.
  113. COMPREPLY=( $( compgen -W "
  114. ALL
  115. AUDIT_CONTROL
  116. AUDIT_WRITE
  117. AUDIT_READ
  118. BLOCK_SUSPEND
  119. CHOWN
  120. DAC_OVERRIDE
  121. DAC_READ_SEARCH
  122. FOWNER
  123. FSETID
  124. IPC_LOCK
  125. IPC_OWNER
  126. KILL
  127. LEASE
  128. LINUX_IMMUTABLE
  129. MAC_ADMIN
  130. MAC_OVERRIDE
  131. MKNOD
  132. NET_ADMIN
  133. NET_BIND_SERVICE
  134. NET_BROADCAST
  135. NET_RAW
  136. SETFCAP
  137. SETGID
  138. SETPCAP
  139. SETUID
  140. SYS_ADMIN
  141. SYS_BOOT
  142. SYS_CHROOT
  143. SYSLOG
  144. SYS_MODULE
  145. SYS_NICE
  146. SYS_PACCT
  147. SYS_PTRACE
  148. SYS_RAWIO
  149. SYS_RESOURCE
  150. SYS_TIME
  151. SYS_TTY_CONFIG
  152. WAKE_ALARM
  153. " -- "$cur" ) )
  154. }
  155. # a selection of the available signals that is most likely of interest in the
  156. # context of docker containers.
  157. __docker_signals() {
  158. local signals=(
  159. SIGCONT
  160. SIGHUP
  161. SIGINT
  162. SIGKILL
  163. SIGQUIT
  164. SIGSTOP
  165. SIGTERM
  166. SIGUSR1
  167. SIGUSR2
  168. )
  169. COMPREPLY=( $( compgen -W "${signals[*]} ${signals[*]#SIG}" -- "$( echo $cur | tr '[:lower:]' '[:upper:]')" ) )
  170. }
  171. _docker_docker() {
  172. local boolean_options="
  173. --daemon -d
  174. --debug -D
  175. --help -h
  176. --icc
  177. --ip-forward
  178. --ip-masq
  179. --iptables
  180. --ipv6
  181. --selinux-enabled
  182. --tls
  183. --tlsverify
  184. --version -v
  185. "
  186. case "$prev" in
  187. --graph|-g)
  188. _filedir -d
  189. return
  190. ;;
  191. --log-level|-l)
  192. COMPREPLY=( $( compgen -W "debug info warn error fatal" -- "$cur" ) )
  193. return
  194. ;;
  195. --pidfile|-p|--tlscacert|--tlscert|--tlskey)
  196. _filedir
  197. return
  198. ;;
  199. --storage-driver|-s)
  200. COMPREPLY=( $( compgen -W "aufs devicemapper btrfs overlay" -- "$(echo $cur | tr '[:upper:]' '[:lower:]')" ) )
  201. return
  202. ;;
  203. $main_options_with_args_glob )
  204. return
  205. ;;
  206. esac
  207. case "$cur" in
  208. -*)
  209. COMPREPLY=( $( compgen -W "$boolean_options $main_options_with_args" -- "$cur" ) )
  210. ;;
  211. *)
  212. COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
  213. ;;
  214. esac
  215. }
  216. _docker_attach() {
  217. case "$cur" in
  218. -*)
  219. COMPREPLY=( $( compgen -W "--help --no-stdin --sig-proxy" -- "$cur" ) )
  220. ;;
  221. *)
  222. local counter="$(__docker_pos_first_nonflag)"
  223. if [ $cword -eq $counter ]; then
  224. __docker_containers_running
  225. fi
  226. ;;
  227. esac
  228. }
  229. _docker_build() {
  230. case "$prev" in
  231. --tag|-t)
  232. __docker_image_repos_and_tags
  233. return
  234. ;;
  235. --file|-f)
  236. _filedir
  237. return
  238. ;;
  239. esac
  240. case "$cur" in
  241. -*)
  242. COMPREPLY=( $( compgen -W "--file -f --force-rm --help --no-cache --pull --quiet -q --rm --tag -t" -- "$cur" ) )
  243. ;;
  244. *)
  245. local counter="$(__docker_pos_first_nonflag '--tag|-t')"
  246. if [ $cword -eq $counter ]; then
  247. _filedir -d
  248. fi
  249. ;;
  250. esac
  251. }
  252. _docker_commit() {
  253. case "$prev" in
  254. --author|-a|--change|-c|--message|-m)
  255. return
  256. ;;
  257. esac
  258. case "$cur" in
  259. -*)
  260. COMPREPLY=( $( compgen -W "--author -a --change -c --help --message -m --pause -p" -- "$cur" ) )
  261. ;;
  262. *)
  263. local counter=$(__docker_pos_first_nonflag '--author|-a|--change|-c|--message|-m')
  264. if [ $cword -eq $counter ]; then
  265. __docker_containers_all
  266. return
  267. fi
  268. (( counter++ ))
  269. if [ $cword -eq $counter ]; then
  270. __docker_image_repos_and_tags
  271. return
  272. fi
  273. ;;
  274. esac
  275. }
  276. _docker_cp() {
  277. case "$cur" in
  278. -*)
  279. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  280. ;;
  281. *)
  282. local counter=$(__docker_pos_first_nonflag)
  283. if [ $cword -eq $counter ]; then
  284. case "$cur" in
  285. *:)
  286. return
  287. ;;
  288. *)
  289. __docker_containers_all
  290. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  291. compopt -o nospace
  292. return
  293. ;;
  294. esac
  295. fi
  296. (( counter++ ))
  297. if [ $cword -eq $counter ]; then
  298. _filedir
  299. return
  300. fi
  301. ;;
  302. esac
  303. }
  304. _docker_create() {
  305. _docker_run
  306. }
  307. _docker_diff() {
  308. case "$cur" in
  309. -*)
  310. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  311. ;;
  312. *)
  313. local counter=$(__docker_pos_first_nonflag)
  314. if [ $cword -eq $counter ]; then
  315. __docker_containers_all
  316. fi
  317. ;;
  318. esac
  319. }
  320. _docker_events() {
  321. case "$prev" in
  322. --filter|-f)
  323. COMPREPLY=( $( compgen -S = -W "container event image" -- "$cur" ) )
  324. compopt -o nospace
  325. return
  326. ;;
  327. --since|--until)
  328. return
  329. ;;
  330. esac
  331. # "=" gets parsed to a word and assigned to either $cur or $prev depending on whether
  332. # it is the last character or not. So we search for "xxx=" in the the last two words.
  333. case "${words[$cword-2]}$prev=" in
  334. *container=*)
  335. cur="${cur#=}"
  336. __docker_containers_all
  337. return
  338. ;;
  339. *event=*)
  340. COMPREPLY=( $( compgen -W "create destroy die export kill pause restart start stop unpause" -- "${cur#=}" ) )
  341. return
  342. ;;
  343. *image=*)
  344. cur="${cur#=}"
  345. __docker_image_repos_and_tags_and_ids
  346. return
  347. ;;
  348. esac
  349. case "$cur" in
  350. -*)
  351. COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) )
  352. ;;
  353. esac
  354. }
  355. _docker_exec() {
  356. case "$cur" in
  357. -*)
  358. COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty" -- "$cur" ) )
  359. ;;
  360. *)
  361. __docker_containers_running
  362. ;;
  363. esac
  364. }
  365. _docker_export() {
  366. case "$cur" in
  367. -*)
  368. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  369. ;;
  370. *)
  371. local counter=$(__docker_pos_first_nonflag)
  372. if [ $cword -eq $counter ]; then
  373. __docker_containers_all
  374. fi
  375. ;;
  376. esac
  377. }
  378. _docker_help() {
  379. local counter=$(__docker_pos_first_nonflag)
  380. if [ $cword -eq $counter ]; then
  381. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  382. fi
  383. }
  384. _docker_history() {
  385. case "$cur" in
  386. -*)
  387. COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) )
  388. ;;
  389. *)
  390. local counter=$(__docker_pos_first_nonflag)
  391. if [ $cword -eq $counter ]; then
  392. __docker_image_repos_and_tags_and_ids
  393. fi
  394. ;;
  395. esac
  396. }
  397. _docker_images() {
  398. case "$prev" in
  399. --filter|-f)
  400. COMPREPLY=( $( compgen -W "dangling=true" -- "$cur" ) )
  401. return
  402. ;;
  403. esac
  404. case "${words[$cword-2]}$prev=" in
  405. *dangling=*)
  406. COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
  407. return
  408. ;;
  409. esac
  410. case "$cur" in
  411. -*)
  412. COMPREPLY=( $( compgen -W "--all -a --filter -f --help --no-trunc --quiet -q" -- "$cur" ) )
  413. ;;
  414. *)
  415. local counter=$(__docker_pos_first_nonflag)
  416. if [ $cword -eq $counter ]; then
  417. __docker_image_repos
  418. fi
  419. ;;
  420. esac
  421. }
  422. _docker_import() {
  423. case "$cur" in
  424. -*)
  425. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  426. ;;
  427. *)
  428. local counter=$(__docker_pos_first_nonflag)
  429. if [ $cword -eq $counter ]; then
  430. return
  431. fi
  432. (( counter++ ))
  433. if [ $cword -eq $counter ]; then
  434. __docker_image_repos_and_tags
  435. return
  436. fi
  437. ;;
  438. esac
  439. }
  440. _docker_info() {
  441. case "$cur" in
  442. -*)
  443. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  444. ;;
  445. esac
  446. }
  447. _docker_inspect() {
  448. case "$prev" in
  449. --format|-f)
  450. return
  451. ;;
  452. esac
  453. case "$cur" in
  454. -*)
  455. COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
  456. ;;
  457. *)
  458. __docker_containers_and_images
  459. ;;
  460. esac
  461. }
  462. _docker_kill() {
  463. case "$prev" in
  464. --signal|-s)
  465. __docker_signals
  466. return
  467. ;;
  468. esac
  469. case "$cur" in
  470. -*)
  471. COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) )
  472. ;;
  473. *)
  474. __docker_containers_running
  475. ;;
  476. esac
  477. }
  478. _docker_load() {
  479. case "$prev" in
  480. --input|-i)
  481. _filedir
  482. return
  483. ;;
  484. esac
  485. case "$cur" in
  486. -*)
  487. COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) )
  488. ;;
  489. esac
  490. }
  491. _docker_login() {
  492. case "$prev" in
  493. --email|-e|--password|-p|--username|-u)
  494. return
  495. ;;
  496. esac
  497. case "$cur" in
  498. -*)
  499. COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) )
  500. ;;
  501. esac
  502. }
  503. _docker_logout() {
  504. case "$cur" in
  505. -*)
  506. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  507. ;;
  508. esac
  509. }
  510. _docker_logs() {
  511. case "$prev" in
  512. --tail)
  513. return
  514. ;;
  515. esac
  516. case "$cur" in
  517. -*)
  518. COMPREPLY=( $( compgen -W "--follow -f --help --tail --timestamps -t" -- "$cur" ) )
  519. ;;
  520. *)
  521. local counter=$(__docker_pos_first_nonflag '--tail')
  522. if [ $cword -eq $counter ]; then
  523. __docker_containers_all
  524. fi
  525. ;;
  526. esac
  527. }
  528. _docker_pause() {
  529. case "$cur" in
  530. -*)
  531. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  532. ;;
  533. *)
  534. local counter=$(__docker_pos_first_nonflag)
  535. if [ $cword -eq $counter ]; then
  536. __docker_containers_pauseable
  537. fi
  538. ;;
  539. esac
  540. }
  541. _docker_port() {
  542. case "$cur" in
  543. -*)
  544. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  545. ;;
  546. *)
  547. local counter=$(__docker_pos_first_nonflag)
  548. if [ $cword -eq $counter ]; then
  549. __docker_containers_all
  550. fi
  551. ;;
  552. esac
  553. }
  554. _docker_ps() {
  555. case "$prev" in
  556. --before|--since)
  557. __docker_containers_all
  558. ;;
  559. --filter|-f)
  560. COMPREPLY=( $( compgen -S = -W "exited status" -- "$cur" ) )
  561. compopt -o nospace
  562. return
  563. ;;
  564. -n)
  565. return
  566. ;;
  567. esac
  568. case "${words[$cword-2]}$prev=" in
  569. *status=*)
  570. COMPREPLY=( $( compgen -W "exited paused restarting running" -- "${cur#=}" ) )
  571. return
  572. ;;
  573. esac
  574. case "$cur" in
  575. -*)
  576. COMPREPLY=( $( compgen -W "--all -a --before --filter -f --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) )
  577. ;;
  578. esac
  579. }
  580. _docker_pull() {
  581. case "$cur" in
  582. -*)
  583. COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) )
  584. ;;
  585. *)
  586. local counter=$(__docker_pos_first_nonflag)
  587. if [ $cword -eq $counter ]; then
  588. __docker_image_repos_and_tags
  589. fi
  590. ;;
  591. esac
  592. }
  593. _docker_push() {
  594. case "$cur" in
  595. -*)
  596. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  597. ;;
  598. *)
  599. local counter=$(__docker_pos_first_nonflag)
  600. if [ $cword -eq $counter ]; then
  601. __docker_image_repos_and_tags
  602. fi
  603. ;;
  604. esac
  605. }
  606. _docker_rename() {
  607. case "$cur" in
  608. -*)
  609. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  610. ;;
  611. *)
  612. local counter=$(__docker_pos_first_nonflag)
  613. if [ $cword -eq $counter ]; then
  614. __docker_containers_all
  615. fi
  616. ;;
  617. esac
  618. }
  619. _docker_restart() {
  620. case "$prev" in
  621. --time|-t)
  622. return
  623. ;;
  624. esac
  625. case "$cur" in
  626. -*)
  627. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  628. ;;
  629. *)
  630. __docker_containers_all
  631. ;;
  632. esac
  633. }
  634. _docker_rm() {
  635. case "$cur" in
  636. -*)
  637. COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
  638. ;;
  639. *)
  640. for arg in "${COMP_WORDS[@]}"; do
  641. case "$arg" in
  642. --force|-f)
  643. __docker_containers_all
  644. return
  645. ;;
  646. esac
  647. done
  648. __docker_containers_stopped
  649. ;;
  650. esac
  651. }
  652. _docker_rmi() {
  653. case "$cur" in
  654. -*)
  655. COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
  656. ;;
  657. *)
  658. __docker_image_repos_and_tags_and_ids
  659. ;;
  660. esac
  661. }
  662. _docker_run() {
  663. local options_with_args="
  664. --add-host
  665. --attach -a
  666. --cap-add
  667. --cap-drop
  668. --cidfile
  669. --cpuset
  670. --cpu-shares -c
  671. --device
  672. --dns
  673. --dns-search
  674. --entrypoint
  675. --env -e
  676. --env-file
  677. --expose
  678. --hostname -h
  679. --ipc
  680. --link
  681. --lxc-conf
  682. --mac-address
  683. --memory -m
  684. --memory-swap
  685. --name
  686. --net
  687. --pid
  688. --publish -p
  689. --restart
  690. --security-opt
  691. --user -u
  692. --ulimit
  693. --volumes-from
  694. --volume -v
  695. --workdir -w
  696. "
  697. local all_options="$options_with_args
  698. --help
  699. --interactive -i
  700. --privileged
  701. --publish-all -P
  702. --read-only
  703. --tty -t
  704. "
  705. [ "$command" = "run" ] && all_options="$all_options
  706. --detach -d
  707. --rm
  708. --sig-proxy
  709. "
  710. local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
  711. case "$prev" in
  712. --add-host)
  713. case "$cur" in
  714. *:)
  715. __docker_resolve_hostname
  716. return
  717. ;;
  718. esac
  719. ;;
  720. --attach|-a)
  721. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  722. return
  723. ;;
  724. --cap-add|--cap-drop)
  725. __docker_capabilities
  726. return
  727. ;;
  728. --cidfile|--env-file)
  729. _filedir
  730. return
  731. ;;
  732. --device|--volume|-v)
  733. case "$cur" in
  734. *:*)
  735. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  736. ;;
  737. '')
  738. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  739. compopt -o nospace
  740. ;;
  741. /*)
  742. _filedir
  743. compopt -o nospace
  744. ;;
  745. esac
  746. return
  747. ;;
  748. --env|-e)
  749. COMPREPLY=( $( compgen -e -- "$cur" ) )
  750. compopt -o nospace
  751. return
  752. ;;
  753. --ipc)
  754. case "$cur" in
  755. *:*)
  756. cur="${cur#*:}"
  757. __docker_containers_running
  758. ;;
  759. *)
  760. COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  761. if [ "$COMPREPLY" = "container:" ]; then
  762. compopt -o nospace
  763. fi
  764. ;;
  765. esac
  766. return
  767. ;;
  768. --link)
  769. case "$cur" in
  770. *:*)
  771. ;;
  772. *)
  773. __docker_containers_running
  774. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  775. compopt -o nospace
  776. ;;
  777. esac
  778. return
  779. ;;
  780. --net)
  781. case "$cur" in
  782. container:*)
  783. local cur=${cur#*:}
  784. __docker_containers_all
  785. ;;
  786. *)
  787. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  788. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  789. compopt -o nospace
  790. fi
  791. ;;
  792. esac
  793. return
  794. ;;
  795. --restart)
  796. case "$cur" in
  797. on-failure:*)
  798. ;;
  799. *)
  800. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  801. ;;
  802. esac
  803. return
  804. ;;
  805. --security-opt)
  806. case "$cur" in
  807. label:*:*)
  808. ;;
  809. label:*)
  810. local cur=${cur##*:}
  811. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  812. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  813. compopt -o nospace
  814. fi
  815. ;;
  816. *)
  817. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  818. compopt -o nospace
  819. ;;
  820. esac
  821. return
  822. ;;
  823. --volumes-from)
  824. __docker_containers_all
  825. return
  826. ;;
  827. $options_with_args_glob )
  828. return
  829. ;;
  830. esac
  831. case "$cur" in
  832. -*)
  833. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  834. ;;
  835. *)
  836. local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  837. if [ $cword -eq $counter ]; then
  838. __docker_image_repos_and_tags_and_ids
  839. fi
  840. ;;
  841. esac
  842. }
  843. _docker_save() {
  844. case "$prev" in
  845. --output|-o)
  846. _filedir
  847. return
  848. ;;
  849. esac
  850. case "$cur" in
  851. -*)
  852. COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  853. ;;
  854. *)
  855. __docker_image_repos_and_tags_and_ids
  856. ;;
  857. esac
  858. }
  859. _docker_search() {
  860. case "$prev" in
  861. --stars|-s)
  862. return
  863. ;;
  864. esac
  865. case "$cur" in
  866. -*)
  867. COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  868. ;;
  869. esac
  870. }
  871. _docker_start() {
  872. case "$cur" in
  873. -*)
  874. COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
  875. ;;
  876. *)
  877. __docker_containers_stopped
  878. ;;
  879. esac
  880. }
  881. _docker_stats() {
  882. case "$cur" in
  883. -*)
  884. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  885. ;;
  886. *)
  887. __docker_containers_running
  888. ;;
  889. esac
  890. }
  891. _docker_stop() {
  892. case "$prev" in
  893. --time|-t)
  894. return
  895. ;;
  896. esac
  897. case "$cur" in
  898. -*)
  899. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  900. ;;
  901. *)
  902. __docker_containers_running
  903. ;;
  904. esac
  905. }
  906. _docker_tag() {
  907. case "$cur" in
  908. -*)
  909. COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
  910. ;;
  911. *)
  912. local counter=$(__docker_pos_first_nonflag)
  913. if [ $cword -eq $counter ]; then
  914. __docker_image_repos_and_tags
  915. return
  916. fi
  917. (( counter++ ))
  918. if [ $cword -eq $counter ]; then
  919. __docker_image_repos_and_tags
  920. return
  921. fi
  922. ;;
  923. esac
  924. }
  925. _docker_unpause() {
  926. case "$cur" in
  927. -*)
  928. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  929. ;;
  930. *)
  931. local counter=$(__docker_pos_first_nonflag)
  932. if [ $cword -eq $counter ]; then
  933. __docker_containers_unpauseable
  934. fi
  935. ;;
  936. esac
  937. }
  938. _docker_top() {
  939. case "$cur" in
  940. -*)
  941. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  942. ;;
  943. *)
  944. local counter=$(__docker_pos_first_nonflag)
  945. if [ $cword -eq $counter ]; then
  946. __docker_containers_running
  947. fi
  948. ;;
  949. esac
  950. }
  951. _docker_version() {
  952. case "$cur" in
  953. -*)
  954. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  955. ;;
  956. esac
  957. }
  958. _docker_wait() {
  959. case "$cur" in
  960. -*)
  961. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  962. ;;
  963. *)
  964. __docker_containers_all
  965. ;;
  966. esac
  967. }
  968. _docker() {
  969. local previous_extglob_setting=$(shopt -p extglob)
  970. shopt -s extglob
  971. local commands=(
  972. attach
  973. build
  974. commit
  975. cp
  976. create
  977. diff
  978. events
  979. exec
  980. export
  981. history
  982. images
  983. import
  984. info
  985. inspect
  986. kill
  987. load
  988. login
  989. logout
  990. logs
  991. pause
  992. port
  993. ps
  994. pull
  995. push
  996. rename
  997. restart
  998. rm
  999. rmi
  1000. run
  1001. save
  1002. search
  1003. start
  1004. stats
  1005. stop
  1006. tag
  1007. top
  1008. unpause
  1009. version
  1010. wait
  1011. )
  1012. local main_options_with_args="
  1013. --api-cors-header
  1014. --bip
  1015. --bridge -b
  1016. --default-ulimit
  1017. --dns
  1018. --dns-search
  1019. --exec-driver -e
  1020. --fixed-cidr
  1021. --fixed-cidr-v6
  1022. --graph -g
  1023. --group -G
  1024. --host -H
  1025. --insecure-registry
  1026. --ip
  1027. --label
  1028. --log-level -l
  1029. --mtu
  1030. --pidfile -p
  1031. --registry-mirror
  1032. --storage-driver -s
  1033. --storage-opt
  1034. --tlscacert
  1035. --tlscert
  1036. --tlskey
  1037. "
  1038. local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1039. COMPREPLY=()
  1040. local cur prev words cword
  1041. _get_comp_words_by_ref -n : cur prev words cword
  1042. local command='docker' cpos=0
  1043. local counter=1
  1044. while [ $counter -lt $cword ]; do
  1045. case "${words[$counter]}" in
  1046. $main_options_with_args_glob )
  1047. (( counter++ ))
  1048. ;;
  1049. -*)
  1050. ;;
  1051. *)
  1052. command="${words[$counter]}"
  1053. cpos=$counter
  1054. (( cpos++ ))
  1055. break
  1056. ;;
  1057. esac
  1058. (( counter++ ))
  1059. done
  1060. local completions_func=_docker_${command}
  1061. declare -F $completions_func >/dev/null && $completions_func
  1062. eval "$previous_extglob_setting"
  1063. return 0
  1064. }
  1065. complete -F _docker docker