docker 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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 ${host:+-H "$host"} 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_container_names() {
  55. local containers=( $(__docker_q ps -aq --no-trunc) )
  56. local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
  57. names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
  58. COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") )
  59. }
  60. __docker_container_ids() {
  61. local containers=( $(__docker_q ps -aq) )
  62. COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") )
  63. }
  64. __docker_image_repos() {
  65. local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
  66. COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
  67. }
  68. __docker_image_repos_and_tags() {
  69. local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
  70. COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
  71. __ltrim_colon_completions "$cur"
  72. }
  73. __docker_image_repos_and_tags_and_ids() {
  74. local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')"
  75. COMPREPLY=( $(compgen -W "$images" -- "$cur") )
  76. __ltrim_colon_completions "$cur"
  77. }
  78. __docker_containers_and_images() {
  79. __docker_containers_all
  80. local containers=( "${COMPREPLY[@]}" )
  81. __docker_image_repos_and_tags_and_ids
  82. COMPREPLY+=( "${containers[@]}" )
  83. }
  84. # Finds the position of the first word that is neither option nor an option's argument.
  85. # If there are options that require arguments, you should pass a glob describing those
  86. # options, e.g. "--option1|-o|--option2"
  87. # Use this function to restrict completions to exact positions after the argument list.
  88. __docker_pos_first_nonflag() {
  89. local argument_flags=$1
  90. local counter=$((command_pos + 1))
  91. while [ $counter -le $cword ]; do
  92. if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
  93. (( counter++ ))
  94. # eat "=" in case of --option=arg syntax
  95. [ "${words[$counter]}" = "=" ] && (( counter++ ))
  96. else
  97. case "${words[$counter]}" in
  98. -*)
  99. ;;
  100. *)
  101. break
  102. ;;
  103. esac
  104. fi
  105. # Bash splits words at "=", retaining "=" as a word, examples:
  106. # "--debug=false" => 3 words, "--log-opt syslog-facility=daemon" => 4 words
  107. while [ "${words[$counter + 1]}" = "=" ] ; do
  108. counter=$(( counter + 2))
  109. done
  110. (( counter++ ))
  111. done
  112. echo $counter
  113. }
  114. # Returns the value of the first option matching option_glob.
  115. # Valid values for option_glob are option names like '--log-level' and
  116. # globs like '--log-level|-l'
  117. # Only positions between the command and the current word are considered.
  118. __docker_value_of_option() {
  119. local option_glob=$1
  120. local counter=$((command_pos + 1))
  121. while [ $counter -lt $cword ]; do
  122. case ${words[$counter]} in
  123. $option_glob )
  124. echo ${words[$counter + 1]}
  125. break
  126. ;;
  127. esac
  128. (( counter++ ))
  129. done
  130. }
  131. # Transforms a multiline list of strings into a single line string
  132. # with the words separated by "|".
  133. # This is used to prepare arguments to __docker_pos_first_nonflag().
  134. __docker_to_alternatives() {
  135. local parts=( $1 )
  136. local IFS='|'
  137. echo "${parts[*]}"
  138. }
  139. # Transforms a multiline list of options into an extglob pattern
  140. # suitable for use in case statements.
  141. __docker_to_extglob() {
  142. local extglob=$( __docker_to_alternatives "$1" )
  143. echo "@($extglob)"
  144. }
  145. __docker_resolve_hostname() {
  146. command -v host >/dev/null 2>&1 || return
  147. COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
  148. }
  149. __docker_capabilities() {
  150. # The list of capabilities is defined in types.go, ALL was added manually.
  151. COMPREPLY=( $( compgen -W "
  152. ALL
  153. AUDIT_CONTROL
  154. AUDIT_WRITE
  155. AUDIT_READ
  156. BLOCK_SUSPEND
  157. CHOWN
  158. DAC_OVERRIDE
  159. DAC_READ_SEARCH
  160. FOWNER
  161. FSETID
  162. IPC_LOCK
  163. IPC_OWNER
  164. KILL
  165. LEASE
  166. LINUX_IMMUTABLE
  167. MAC_ADMIN
  168. MAC_OVERRIDE
  169. MKNOD
  170. NET_ADMIN
  171. NET_BIND_SERVICE
  172. NET_BROADCAST
  173. NET_RAW
  174. SETFCAP
  175. SETGID
  176. SETPCAP
  177. SETUID
  178. SYS_ADMIN
  179. SYS_BOOT
  180. SYS_CHROOT
  181. SYSLOG
  182. SYS_MODULE
  183. SYS_NICE
  184. SYS_PACCT
  185. SYS_PTRACE
  186. SYS_RAWIO
  187. SYS_RESOURCE
  188. SYS_TIME
  189. SYS_TTY_CONFIG
  190. WAKE_ALARM
  191. " -- "$cur" ) )
  192. }
  193. __docker_log_drivers() {
  194. COMPREPLY=( $( compgen -W "
  195. fluentd
  196. gelf
  197. journald
  198. json-file
  199. none
  200. syslog
  201. " -- "$cur" ) )
  202. }
  203. __docker_log_driver_options() {
  204. # see docs/reference/logging/index.md
  205. case $(__docker_value_of_option --log-driver) in
  206. fluentd)
  207. COMPREPLY=( $( compgen -W "fluentd-address fluentd-tag" -S = -- "$cur" ) )
  208. ;;
  209. gelf)
  210. COMPREPLY=( $( compgen -W "gelf-address gelf-tag" -S = -- "$cur" ) )
  211. ;;
  212. syslog)
  213. COMPREPLY=( $( compgen -W "syslog-address syslog-facility syslog-tag" -S = -- "$cur" ) )
  214. ;;
  215. *)
  216. return
  217. ;;
  218. esac
  219. compopt -o nospace
  220. }
  221. __docker_complete_log_driver_options() {
  222. # "=" gets parsed to a word and assigned to either $cur or $prev depending on whether
  223. # it is the last character or not. So we search for "xxx=" in the the last two words.
  224. case "${words[$cword-2]}$prev=" in
  225. *gelf-address=*)
  226. COMPREPLY=( $( compgen -W "udp" -S "://" -- "${cur#=}" ) )
  227. compopt -o nospace
  228. return
  229. ;;
  230. *syslog-address=*)
  231. COMPREPLY=( $( compgen -W "tcp udp unix" -S "://" -- "${cur#=}" ) )
  232. compopt -o nospace
  233. return
  234. ;;
  235. *syslog-facility=*)
  236. COMPREPLY=( $( compgen -W "
  237. auth
  238. authpriv
  239. cron
  240. daemon
  241. ftp
  242. kern
  243. local0
  244. local1
  245. local2
  246. local3
  247. local4
  248. local5
  249. local6
  250. local7
  251. lpr
  252. mail
  253. news
  254. syslog
  255. user
  256. uucp
  257. " -- "${cur#=}" ) )
  258. return
  259. ;;
  260. esac
  261. return 1
  262. }
  263. # a selection of the available signals that is most likely of interest in the
  264. # context of docker containers.
  265. __docker_signals() {
  266. local signals=(
  267. SIGCONT
  268. SIGHUP
  269. SIGINT
  270. SIGKILL
  271. SIGQUIT
  272. SIGSTOP
  273. SIGTERM
  274. SIGUSR1
  275. SIGUSR2
  276. )
  277. COMPREPLY=( $( compgen -W "${signals[*]} ${signals[*]#SIG}" -- "$( echo $cur | tr '[:lower:]' '[:upper:]')" ) )
  278. }
  279. _docker_docker() {
  280. local boolean_options="
  281. --daemon -d
  282. --debug -D
  283. --help -h
  284. --icc
  285. --ip-forward
  286. --ip-masq
  287. --iptables
  288. --ipv6
  289. --selinux-enabled
  290. --tls
  291. --tlsverify
  292. --userland-proxy=false
  293. --version -v
  294. "
  295. case "$prev" in
  296. --exec-root|--graph|-g)
  297. _filedir -d
  298. return
  299. ;;
  300. --log-driver)
  301. __docker_log_drivers
  302. return
  303. ;;
  304. --log-level|-l)
  305. COMPREPLY=( $( compgen -W "debug info warn error fatal" -- "$cur" ) )
  306. return
  307. ;;
  308. --log-opt)
  309. __docker_log_driver_options
  310. return
  311. ;;
  312. --pidfile|-p|--tlscacert|--tlscert|--tlskey)
  313. _filedir
  314. return
  315. ;;
  316. --storage-driver|-s)
  317. COMPREPLY=( $( compgen -W "aufs devicemapper btrfs overlay" -- "$(echo $cur | tr '[:upper:]' '[:lower:]')" ) )
  318. return
  319. ;;
  320. $main_options_with_args_glob )
  321. return
  322. ;;
  323. esac
  324. __docker_complete_log_driver_options && return
  325. case "$cur" in
  326. -*)
  327. COMPREPLY=( $( compgen -W "$boolean_options $main_options_with_args" -- "$cur" ) )
  328. ;;
  329. *)
  330. COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
  331. ;;
  332. esac
  333. }
  334. _docker_attach() {
  335. case "$cur" in
  336. -*)
  337. COMPREPLY=( $( compgen -W "--help --no-stdin --sig-proxy" -- "$cur" ) )
  338. ;;
  339. *)
  340. local counter="$(__docker_pos_first_nonflag)"
  341. if [ $cword -eq $counter ]; then
  342. __docker_containers_running
  343. fi
  344. ;;
  345. esac
  346. }
  347. _docker_build() {
  348. case "$prev" in
  349. --cgroup-parent|--cpuset-cpus|--cpuset-mems|--cpu-shares|-c|--cpu-period|--cpu-quota|--memory|-m|--memory-swap)
  350. return
  351. ;;
  352. --file|-f)
  353. _filedir
  354. return
  355. ;;
  356. --tag|-t)
  357. __docker_image_repos_and_tags
  358. return
  359. ;;
  360. esac
  361. case "$cur" in
  362. -*)
  363. COMPREPLY=( $( compgen -W "--cgroup-parent --cpuset-cpus --cpuset-mems --cpu-shares -c --cpu-period --cpu-quota --file -f --force-rm --help --memory -m --memory-swap --no-cache --pull --quiet -q --rm --tag -t" -- "$cur" ) )
  364. ;;
  365. *)
  366. local counter="$(__docker_pos_first_nonflag '--cgroup-parent|--cpuset-cpus|--cpuset-mems|--cpu-shares|-c|--cpu-period|--cpu-quota|--file|-f|--memory|-m|--memory-swap|--tag|-t')"
  367. if [ $cword -eq $counter ]; then
  368. _filedir -d
  369. fi
  370. ;;
  371. esac
  372. }
  373. _docker_commit() {
  374. case "$prev" in
  375. --author|-a|--change|-c|--message|-m)
  376. return
  377. ;;
  378. esac
  379. case "$cur" in
  380. -*)
  381. COMPREPLY=( $( compgen -W "--author -a --change -c --help --message -m --pause -p" -- "$cur" ) )
  382. ;;
  383. *)
  384. local counter=$(__docker_pos_first_nonflag '--author|-a|--change|-c|--message|-m')
  385. if [ $cword -eq $counter ]; then
  386. __docker_containers_all
  387. return
  388. fi
  389. (( counter++ ))
  390. if [ $cword -eq $counter ]; then
  391. __docker_image_repos_and_tags
  392. return
  393. fi
  394. ;;
  395. esac
  396. }
  397. _docker_cp() {
  398. case "$cur" in
  399. -*)
  400. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  401. ;;
  402. *)
  403. local counter=$(__docker_pos_first_nonflag)
  404. if [ $cword -eq $counter ]; then
  405. case "$cur" in
  406. *:)
  407. return
  408. ;;
  409. *)
  410. __docker_containers_all
  411. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  412. compopt -o nospace
  413. return
  414. ;;
  415. esac
  416. fi
  417. (( counter++ ))
  418. if [ $cword -eq $counter ]; then
  419. _filedir -d
  420. return
  421. fi
  422. ;;
  423. esac
  424. }
  425. _docker_create() {
  426. _docker_run
  427. }
  428. _docker_diff() {
  429. case "$cur" in
  430. -*)
  431. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  432. ;;
  433. *)
  434. local counter=$(__docker_pos_first_nonflag)
  435. if [ $cword -eq $counter ]; then
  436. __docker_containers_all
  437. fi
  438. ;;
  439. esac
  440. }
  441. _docker_events() {
  442. case "$prev" in
  443. --filter|-f)
  444. COMPREPLY=( $( compgen -S = -W "container event image" -- "$cur" ) )
  445. compopt -o nospace
  446. return
  447. ;;
  448. --since|--until)
  449. return
  450. ;;
  451. esac
  452. case "${words[$cword-2]}$prev=" in
  453. *container=*)
  454. cur="${cur#=}"
  455. __docker_containers_all
  456. return
  457. ;;
  458. *event=*)
  459. COMPREPLY=( $( compgen -W "
  460. attach
  461. commit
  462. copy
  463. create
  464. delete
  465. destroy
  466. die
  467. exec_create
  468. exec_start
  469. export
  470. import
  471. kill
  472. oom
  473. pause
  474. pull
  475. push
  476. rename
  477. resize
  478. restart
  479. start
  480. stop
  481. tag
  482. top
  483. unpause
  484. untag
  485. " -- "${cur#=}" ) )
  486. return
  487. ;;
  488. *image=*)
  489. cur="${cur#=}"
  490. __docker_image_repos_and_tags_and_ids
  491. return
  492. ;;
  493. esac
  494. case "$cur" in
  495. -*)
  496. COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) )
  497. ;;
  498. esac
  499. }
  500. _docker_exec() {
  501. case "$prev" in
  502. --user|-u)
  503. return
  504. ;;
  505. esac
  506. case "$cur" in
  507. -*)
  508. COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty -u --user" -- "$cur" ) )
  509. ;;
  510. *)
  511. __docker_containers_running
  512. ;;
  513. esac
  514. }
  515. _docker_export() {
  516. case "$cur" in
  517. -*)
  518. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  519. ;;
  520. *)
  521. local counter=$(__docker_pos_first_nonflag)
  522. if [ $cword -eq $counter ]; then
  523. __docker_containers_all
  524. fi
  525. ;;
  526. esac
  527. }
  528. _docker_help() {
  529. local counter=$(__docker_pos_first_nonflag)
  530. if [ $cword -eq $counter ]; then
  531. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  532. fi
  533. }
  534. _docker_history() {
  535. case "$cur" in
  536. -*)
  537. COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) )
  538. ;;
  539. *)
  540. local counter=$(__docker_pos_first_nonflag)
  541. if [ $cword -eq $counter ]; then
  542. __docker_image_repos_and_tags_and_ids
  543. fi
  544. ;;
  545. esac
  546. }
  547. _docker_images() {
  548. case "$prev" in
  549. --filter|-f)
  550. COMPREPLY=( $( compgen -W "dangling=true label=" -- "$cur" ) )
  551. if [ "$COMPREPLY" = "label=" ]; then
  552. compopt -o nospace
  553. fi
  554. return
  555. ;;
  556. esac
  557. case "${words[$cword-2]}$prev=" in
  558. *dangling=*)
  559. COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
  560. return
  561. ;;
  562. *label=*)
  563. return
  564. ;;
  565. esac
  566. case "$cur" in
  567. -*)
  568. COMPREPLY=( $( compgen -W "--all -a --digests --filter -f --help --no-trunc --quiet -q" -- "$cur" ) )
  569. ;;
  570. =)
  571. return
  572. ;;
  573. *)
  574. __docker_image_repos
  575. ;;
  576. esac
  577. }
  578. _docker_import() {
  579. case "$cur" in
  580. -*)
  581. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  582. ;;
  583. *)
  584. local counter=$(__docker_pos_first_nonflag)
  585. if [ $cword -eq $counter ]; then
  586. return
  587. fi
  588. (( counter++ ))
  589. if [ $cword -eq $counter ]; then
  590. __docker_image_repos_and_tags
  591. return
  592. fi
  593. ;;
  594. esac
  595. }
  596. _docker_info() {
  597. case "$cur" in
  598. -*)
  599. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  600. ;;
  601. esac
  602. }
  603. _docker_inspect() {
  604. case "$prev" in
  605. --format|-f)
  606. return
  607. ;;
  608. --type)
  609. COMPREPLY=( $( compgen -W "image container" -- "$cur" ) )
  610. return
  611. ;;
  612. esac
  613. case "$cur" in
  614. -*)
  615. COMPREPLY=( $( compgen -W "--format -f --type --help" -- "$cur" ) )
  616. ;;
  617. *)
  618. __docker_containers_and_images
  619. ;;
  620. esac
  621. }
  622. _docker_kill() {
  623. case "$prev" in
  624. --signal|-s)
  625. __docker_signals
  626. return
  627. ;;
  628. esac
  629. case "$cur" in
  630. -*)
  631. COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) )
  632. ;;
  633. *)
  634. __docker_containers_running
  635. ;;
  636. esac
  637. }
  638. _docker_load() {
  639. case "$prev" in
  640. --input|-i)
  641. _filedir
  642. return
  643. ;;
  644. esac
  645. case "$cur" in
  646. -*)
  647. COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) )
  648. ;;
  649. esac
  650. }
  651. _docker_login() {
  652. case "$prev" in
  653. --email|-e|--password|-p|--username|-u)
  654. return
  655. ;;
  656. esac
  657. case "$cur" in
  658. -*)
  659. COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) )
  660. ;;
  661. esac
  662. }
  663. _docker_logout() {
  664. case "$cur" in
  665. -*)
  666. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  667. ;;
  668. esac
  669. }
  670. _docker_logs() {
  671. case "$prev" in
  672. --since|--tail)
  673. return
  674. ;;
  675. esac
  676. case "$cur" in
  677. -*)
  678. COMPREPLY=( $( compgen -W "--follow -f --help --since --tail --timestamps -t" -- "$cur" ) )
  679. ;;
  680. *)
  681. local counter=$(__docker_pos_first_nonflag '--tail')
  682. if [ $cword -eq $counter ]; then
  683. __docker_containers_all
  684. fi
  685. ;;
  686. esac
  687. }
  688. _docker_pause() {
  689. case "$cur" in
  690. -*)
  691. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  692. ;;
  693. *)
  694. local counter=$(__docker_pos_first_nonflag)
  695. if [ $cword -eq $counter ]; then
  696. __docker_containers_pauseable
  697. fi
  698. ;;
  699. esac
  700. }
  701. _docker_port() {
  702. case "$cur" in
  703. -*)
  704. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  705. ;;
  706. *)
  707. local counter=$(__docker_pos_first_nonflag)
  708. if [ $cword -eq $counter ]; then
  709. __docker_containers_all
  710. fi
  711. ;;
  712. esac
  713. }
  714. _docker_ps() {
  715. case "$prev" in
  716. --before|--since)
  717. __docker_containers_all
  718. ;;
  719. --filter|-f)
  720. COMPREPLY=( $( compgen -S = -W "exited id label name status" -- "$cur" ) )
  721. compopt -o nospace
  722. return
  723. ;;
  724. -n)
  725. return
  726. ;;
  727. esac
  728. case "${words[$cword-2]}$prev=" in
  729. *id=*)
  730. cur="${cur#=}"
  731. __docker_container_ids
  732. return
  733. ;;
  734. *name=*)
  735. cur="${cur#=}"
  736. __docker_container_names
  737. return
  738. ;;
  739. *status=*)
  740. COMPREPLY=( $( compgen -W "exited paused restarting running" -- "${cur#=}" ) )
  741. return
  742. ;;
  743. esac
  744. case "$cur" in
  745. -*)
  746. COMPREPLY=( $( compgen -W "--all -a --before --filter -f --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) )
  747. ;;
  748. esac
  749. }
  750. _docker_pull() {
  751. case "$cur" in
  752. -*)
  753. COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) )
  754. ;;
  755. *)
  756. local counter=$(__docker_pos_first_nonflag)
  757. if [ $cword -eq $counter ]; then
  758. for arg in "${COMP_WORDS[@]}"; do
  759. case "$arg" in
  760. --all-tags|-a)
  761. __docker_image_repos
  762. return
  763. ;;
  764. esac
  765. done
  766. __docker_image_repos_and_tags
  767. fi
  768. ;;
  769. esac
  770. }
  771. _docker_push() {
  772. case "$cur" in
  773. -*)
  774. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  775. ;;
  776. *)
  777. local counter=$(__docker_pos_first_nonflag)
  778. if [ $cword -eq $counter ]; then
  779. __docker_image_repos_and_tags
  780. fi
  781. ;;
  782. esac
  783. }
  784. _docker_rename() {
  785. case "$cur" in
  786. -*)
  787. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  788. ;;
  789. *)
  790. local counter=$(__docker_pos_first_nonflag)
  791. if [ $cword -eq $counter ]; then
  792. __docker_containers_all
  793. fi
  794. ;;
  795. esac
  796. }
  797. _docker_restart() {
  798. case "$prev" in
  799. --time|-t)
  800. return
  801. ;;
  802. esac
  803. case "$cur" in
  804. -*)
  805. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  806. ;;
  807. *)
  808. __docker_containers_all
  809. ;;
  810. esac
  811. }
  812. _docker_rm() {
  813. case "$cur" in
  814. -*)
  815. COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
  816. ;;
  817. *)
  818. for arg in "${COMP_WORDS[@]}"; do
  819. case "$arg" in
  820. --force|-f)
  821. __docker_containers_all
  822. return
  823. ;;
  824. esac
  825. done
  826. __docker_containers_stopped
  827. ;;
  828. esac
  829. }
  830. _docker_rmi() {
  831. case "$cur" in
  832. -*)
  833. COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
  834. ;;
  835. *)
  836. __docker_image_repos_and_tags_and_ids
  837. ;;
  838. esac
  839. }
  840. _docker_run() {
  841. local options_with_args="
  842. --add-host
  843. --blkio-weight
  844. --attach -a
  845. --cap-add
  846. --cap-drop
  847. --cgroup-parent
  848. --cidfile
  849. --cpuset
  850. --cpu-period
  851. --cpu-quota
  852. --cpu-shares -c
  853. --device
  854. --dns
  855. --dns-search
  856. --entrypoint
  857. --env -e
  858. --env-file
  859. --expose
  860. --group-add
  861. --hostname -h
  862. --ipc
  863. --label -l
  864. --label-file
  865. --link
  866. --log-driver
  867. --log-opt
  868. --lxc-conf
  869. --mac-address
  870. --memory -m
  871. --memory-swap
  872. --name
  873. --net
  874. --pid
  875. --publish -p
  876. --restart
  877. --security-opt
  878. --user -u
  879. --ulimit
  880. --uts
  881. --volumes-from
  882. --volume -v
  883. --workdir -w
  884. "
  885. local all_options="$options_with_args
  886. --help
  887. --interactive -i
  888. --privileged
  889. --publish-all -P
  890. --read-only
  891. --tty -t
  892. "
  893. [ "$command" = "run" ] && all_options="$all_options
  894. --detach -d
  895. --rm
  896. --sig-proxy
  897. "
  898. local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
  899. case "$prev" in
  900. --add-host)
  901. case "$cur" in
  902. *:)
  903. __docker_resolve_hostname
  904. return
  905. ;;
  906. esac
  907. ;;
  908. --attach|-a)
  909. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  910. return
  911. ;;
  912. --cap-add|--cap-drop)
  913. __docker_capabilities
  914. return
  915. ;;
  916. --cidfile|--env-file|--label-file)
  917. _filedir
  918. return
  919. ;;
  920. --device|--volume|-v)
  921. case "$cur" in
  922. *:*)
  923. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  924. ;;
  925. '')
  926. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  927. compopt -o nospace
  928. ;;
  929. /*)
  930. _filedir
  931. compopt -o nospace
  932. ;;
  933. esac
  934. return
  935. ;;
  936. --env|-e)
  937. COMPREPLY=( $( compgen -e -- "$cur" ) )
  938. compopt -o nospace
  939. return
  940. ;;
  941. --ipc)
  942. case "$cur" in
  943. *:*)
  944. cur="${cur#*:}"
  945. __docker_containers_running
  946. ;;
  947. *)
  948. COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  949. if [ "$COMPREPLY" = "container:" ]; then
  950. compopt -o nospace
  951. fi
  952. ;;
  953. esac
  954. return
  955. ;;
  956. --link)
  957. case "$cur" in
  958. *:*)
  959. ;;
  960. *)
  961. __docker_containers_running
  962. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  963. compopt -o nospace
  964. ;;
  965. esac
  966. return
  967. ;;
  968. --log-driver)
  969. __docker_log_drivers
  970. return
  971. ;;
  972. --log-opt)
  973. __docker_log_driver_options
  974. return
  975. ;;
  976. --net)
  977. case "$cur" in
  978. container:*)
  979. local cur=${cur#*:}
  980. __docker_containers_all
  981. ;;
  982. *)
  983. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  984. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  985. compopt -o nospace
  986. fi
  987. ;;
  988. esac
  989. return
  990. ;;
  991. --restart)
  992. case "$cur" in
  993. on-failure:*)
  994. ;;
  995. *)
  996. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  997. ;;
  998. esac
  999. return
  1000. ;;
  1001. --security-opt)
  1002. case "$cur" in
  1003. label:*:*)
  1004. ;;
  1005. label:*)
  1006. local cur=${cur##*:}
  1007. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  1008. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  1009. compopt -o nospace
  1010. fi
  1011. ;;
  1012. *)
  1013. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  1014. compopt -o nospace
  1015. ;;
  1016. esac
  1017. return
  1018. ;;
  1019. --volumes-from)
  1020. __docker_containers_all
  1021. return
  1022. ;;
  1023. $options_with_args_glob )
  1024. return
  1025. ;;
  1026. esac
  1027. __docker_complete_log_driver_options && return
  1028. case "$cur" in
  1029. -*)
  1030. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  1031. ;;
  1032. *)
  1033. local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  1034. if [ $cword -eq $counter ]; then
  1035. __docker_image_repos_and_tags_and_ids
  1036. fi
  1037. ;;
  1038. esac
  1039. }
  1040. _docker_save() {
  1041. case "$prev" in
  1042. --output|-o)
  1043. _filedir
  1044. return
  1045. ;;
  1046. esac
  1047. case "$cur" in
  1048. -*)
  1049. COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  1050. ;;
  1051. *)
  1052. __docker_image_repos_and_tags_and_ids
  1053. ;;
  1054. esac
  1055. }
  1056. _docker_search() {
  1057. case "$prev" in
  1058. --stars|-s)
  1059. return
  1060. ;;
  1061. esac
  1062. case "$cur" in
  1063. -*)
  1064. COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  1065. ;;
  1066. esac
  1067. }
  1068. _docker_start() {
  1069. case "$cur" in
  1070. -*)
  1071. COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
  1072. ;;
  1073. *)
  1074. __docker_containers_stopped
  1075. ;;
  1076. esac
  1077. }
  1078. _docker_stats() {
  1079. case "$cur" in
  1080. -*)
  1081. COMPREPLY=( $( compgen -W "--no-stream --help" -- "$cur" ) )
  1082. ;;
  1083. *)
  1084. __docker_containers_running
  1085. ;;
  1086. esac
  1087. }
  1088. _docker_stop() {
  1089. case "$prev" in
  1090. --time|-t)
  1091. return
  1092. ;;
  1093. esac
  1094. case "$cur" in
  1095. -*)
  1096. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  1097. ;;
  1098. *)
  1099. __docker_containers_running
  1100. ;;
  1101. esac
  1102. }
  1103. _docker_tag() {
  1104. case "$cur" in
  1105. -*)
  1106. COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
  1107. ;;
  1108. *)
  1109. local counter=$(__docker_pos_first_nonflag)
  1110. if [ $cword -eq $counter ]; then
  1111. __docker_image_repos_and_tags
  1112. return
  1113. fi
  1114. (( counter++ ))
  1115. if [ $cword -eq $counter ]; then
  1116. __docker_image_repos_and_tags
  1117. return
  1118. fi
  1119. ;;
  1120. esac
  1121. }
  1122. _docker_unpause() {
  1123. case "$cur" in
  1124. -*)
  1125. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1126. ;;
  1127. *)
  1128. local counter=$(__docker_pos_first_nonflag)
  1129. if [ $cword -eq $counter ]; then
  1130. __docker_containers_unpauseable
  1131. fi
  1132. ;;
  1133. esac
  1134. }
  1135. _docker_top() {
  1136. case "$cur" in
  1137. -*)
  1138. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1139. ;;
  1140. *)
  1141. local counter=$(__docker_pos_first_nonflag)
  1142. if [ $cword -eq $counter ]; then
  1143. __docker_containers_running
  1144. fi
  1145. ;;
  1146. esac
  1147. }
  1148. _docker_version() {
  1149. case "$cur" in
  1150. -*)
  1151. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1152. ;;
  1153. esac
  1154. }
  1155. _docker_wait() {
  1156. case "$cur" in
  1157. -*)
  1158. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1159. ;;
  1160. *)
  1161. __docker_containers_all
  1162. ;;
  1163. esac
  1164. }
  1165. _docker() {
  1166. local previous_extglob_setting=$(shopt -p extglob)
  1167. shopt -s extglob
  1168. local commands=(
  1169. attach
  1170. build
  1171. commit
  1172. cp
  1173. create
  1174. diff
  1175. events
  1176. exec
  1177. export
  1178. history
  1179. images
  1180. import
  1181. info
  1182. inspect
  1183. kill
  1184. load
  1185. login
  1186. logout
  1187. logs
  1188. pause
  1189. port
  1190. ps
  1191. pull
  1192. push
  1193. rename
  1194. restart
  1195. rm
  1196. rmi
  1197. run
  1198. save
  1199. search
  1200. start
  1201. stats
  1202. stop
  1203. tag
  1204. top
  1205. unpause
  1206. version
  1207. wait
  1208. )
  1209. local main_options_with_args="
  1210. --api-cors-header
  1211. --bip
  1212. --bridge -b
  1213. --default-gateway
  1214. --default-gateway-v6
  1215. --default-ulimit
  1216. --dns
  1217. --dns-search
  1218. --exec-driver -e
  1219. --exec-opt
  1220. --exec-root
  1221. --fixed-cidr
  1222. --fixed-cidr-v6
  1223. --graph -g
  1224. --group -G
  1225. --host -H
  1226. --insecure-registry
  1227. --ip
  1228. --label
  1229. --log-driver
  1230. --log-level -l
  1231. --log-opt
  1232. --mtu
  1233. --pidfile -p
  1234. --registry-mirror
  1235. --storage-driver -s
  1236. --storage-opt
  1237. --tlscacert
  1238. --tlscert
  1239. --tlskey
  1240. "
  1241. local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1242. local host
  1243. COMPREPLY=()
  1244. local cur prev words cword
  1245. _get_comp_words_by_ref -n : cur prev words cword
  1246. local command='docker' command_pos=0
  1247. local counter=1
  1248. while [ $counter -lt $cword ]; do
  1249. case "${words[$counter]}" in
  1250. # save host so that completion can use custom daemon
  1251. --host|-H)
  1252. (( counter++ ))
  1253. host="${words[$counter]}"
  1254. ;;
  1255. $main_options_with_args_glob )
  1256. (( counter++ ))
  1257. ;;
  1258. -*)
  1259. ;;
  1260. =)
  1261. (( counter++ ))
  1262. ;;
  1263. *)
  1264. command="${words[$counter]}"
  1265. command_pos=$counter
  1266. break
  1267. ;;
  1268. esac
  1269. (( counter++ ))
  1270. done
  1271. local completions_func=_docker_${command}
  1272. declare -F $completions_func >/dev/null && $completions_func
  1273. eval "$previous_extglob_setting"
  1274. return 0
  1275. }
  1276. complete -F _docker docker