docker 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. __docker_q() {
  24. docker 2>/dev/null "$@"
  25. }
  26. __docker_containers_all() {
  27. local IFS=$'\n'
  28. local containers=( $(__docker_q ps -aq --no-trunc) )
  29. if [ "$1" ]; then
  30. containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") )
  31. fi
  32. local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
  33. names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
  34. unset IFS
  35. COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") )
  36. }
  37. __docker_containers_running() {
  38. __docker_containers_all '.State.Running'
  39. }
  40. __docker_containers_stopped() {
  41. __docker_containers_all 'not .State.Running'
  42. }
  43. __docker_containers_pauseable() {
  44. __docker_containers_all 'and .State.Running (not .State.Paused)'
  45. }
  46. __docker_containers_unpauseable() {
  47. __docker_containers_all '.State.Paused'
  48. }
  49. __docker_image_repos() {
  50. local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
  51. COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
  52. }
  53. __docker_image_repos_and_tags() {
  54. local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
  55. COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
  56. __ltrim_colon_completions "$cur"
  57. }
  58. __docker_image_repos_and_tags_and_ids() {
  59. local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')"
  60. COMPREPLY=( $(compgen -W "$images" -- "$cur") )
  61. __ltrim_colon_completions "$cur"
  62. }
  63. __docker_containers_and_images() {
  64. __docker_containers_all
  65. local containers=( "${COMPREPLY[@]}" )
  66. __docker_image_repos_and_tags_and_ids
  67. COMPREPLY+=( "${containers[@]}" )
  68. }
  69. __docker_pos_first_nonflag() {
  70. local argument_flags=$1
  71. local counter=$cpos
  72. while [ $counter -le $cword ]; do
  73. if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
  74. (( counter++ ))
  75. else
  76. case "${words[$counter]}" in
  77. -*)
  78. ;;
  79. *)
  80. break
  81. ;;
  82. esac
  83. fi
  84. (( counter++ ))
  85. done
  86. echo $counter
  87. }
  88. __docker_resolve_hostname() {
  89. command -v host >/dev/null 2>&1 || return
  90. COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
  91. }
  92. __docker_capabilities() {
  93. # The list of capabilities is defined in types.go, ALL was added manually.
  94. COMPREPLY=( $( compgen -W "
  95. ALL
  96. AUDIT_CONTROL
  97. AUDIT_WRITE
  98. BLOCK_SUSPEND
  99. CHOWN
  100. DAC_OVERRIDE
  101. DAC_READ_SEARCH
  102. FOWNER
  103. FSETID
  104. IPC_LOCK
  105. IPC_OWNER
  106. KILL
  107. LEASE
  108. LINUX_IMMUTABLE
  109. MAC_ADMIN
  110. MAC_OVERRIDE
  111. MKNOD
  112. NET_ADMIN
  113. NET_BIND_SERVICE
  114. NET_BROADCAST
  115. NET_RAW
  116. SETFCAP
  117. SETGID
  118. SETPCAP
  119. SETUID
  120. SYS_ADMIN
  121. SYS_BOOT
  122. SYS_CHROOT
  123. SYSLOG
  124. SYS_MODULE
  125. SYS_NICE
  126. SYS_PACCT
  127. SYS_PTRACE
  128. SYS_RAWIO
  129. SYS_RESOURCE
  130. SYS_TIME
  131. SYS_TTY_CONFIG
  132. WAKE_ALARM
  133. " -- "$cur" ) )
  134. }
  135. _docker_docker() {
  136. case "$prev" in
  137. -H)
  138. return
  139. ;;
  140. esac
  141. case "$cur" in
  142. -*)
  143. COMPREPLY=( $( compgen -W "-H" -- "$cur" ) )
  144. ;;
  145. *)
  146. COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
  147. ;;
  148. esac
  149. }
  150. _docker_attach() {
  151. case "$cur" in
  152. -*)
  153. COMPREPLY=( $( compgen -W "--no-stdin --sig-proxy" -- "$cur" ) )
  154. ;;
  155. *)
  156. local counter="$(__docker_pos_first_nonflag)"
  157. if [ $cword -eq $counter ]; then
  158. __docker_containers_running
  159. fi
  160. ;;
  161. esac
  162. }
  163. _docker_build() {
  164. case "$prev" in
  165. -t|--tag)
  166. __docker_image_repos_and_tags
  167. return
  168. ;;
  169. esac
  170. case "$cur" in
  171. -*)
  172. COMPREPLY=( $( compgen -W "-t --tag -q --quiet --no-cache --rm --force-rm" -- "$cur" ) )
  173. ;;
  174. *)
  175. local counter="$(__docker_pos_first_nonflag '-t|--tag')"
  176. if [ $cword -eq $counter ]; then
  177. _filedir -d
  178. fi
  179. ;;
  180. esac
  181. }
  182. _docker_commit() {
  183. case "$prev" in
  184. -m|--message|-a|--author|--run)
  185. return
  186. ;;
  187. esac
  188. case "$cur" in
  189. -*)
  190. COMPREPLY=( $( compgen -W "-m --message -a --author --run" -- "$cur" ) )
  191. ;;
  192. *)
  193. local counter=$(__docker_pos_first_nonflag '-m|--message|-a|--author|--run')
  194. if [ $cword -eq $counter ]; then
  195. __docker_containers_all
  196. return
  197. fi
  198. (( counter++ ))
  199. if [ $cword -eq $counter ]; then
  200. __docker_image_repos_and_tags
  201. return
  202. fi
  203. ;;
  204. esac
  205. }
  206. _docker_cp() {
  207. local counter=$(__docker_pos_first_nonflag)
  208. if [ $cword -eq $counter ]; then
  209. case "$cur" in
  210. *:)
  211. return
  212. ;;
  213. *)
  214. __docker_containers_all
  215. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  216. compopt -o nospace
  217. return
  218. ;;
  219. esac
  220. fi
  221. (( counter++ ))
  222. if [ $cword -eq $counter ]; then
  223. _filedir
  224. return
  225. fi
  226. }
  227. _docker_create() {
  228. _docker_run
  229. }
  230. _docker_diff() {
  231. local counter=$(__docker_pos_first_nonflag)
  232. if [ $cword -eq $counter ]; then
  233. __docker_containers_all
  234. fi
  235. }
  236. _docker_events() {
  237. case "$prev" in
  238. --since)
  239. return
  240. ;;
  241. esac
  242. case "$cur" in
  243. -*)
  244. COMPREPLY=( $( compgen -W "--since" -- "$cur" ) )
  245. ;;
  246. esac
  247. }
  248. _docker_exec() {
  249. case "$cur" in
  250. -*)
  251. COMPREPLY=( $( compgen -W "-d --detach -i --interactive -t --tty" -- "$cur" ) )
  252. ;;
  253. *)
  254. __docker_containers_running
  255. ;;
  256. esac
  257. }
  258. _docker_export() {
  259. local counter=$(__docker_pos_first_nonflag)
  260. if [ $cword -eq $counter ]; then
  261. __docker_containers_all
  262. fi
  263. }
  264. _docker_help() {
  265. local counter=$(__docker_pos_first_nonflag)
  266. if [ $cword -eq $counter ]; then
  267. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  268. fi
  269. }
  270. _docker_history() {
  271. case "$cur" in
  272. -*)
  273. COMPREPLY=( $( compgen -W "-q --quiet --no-trunc" -- "$cur" ) )
  274. ;;
  275. *)
  276. local counter=$(__docker_pos_first_nonflag)
  277. if [ $cword -eq $counter ]; then
  278. __docker_image_repos_and_tags_and_ids
  279. fi
  280. ;;
  281. esac
  282. }
  283. _docker_images() {
  284. case "$cur" in
  285. -*)
  286. COMPREPLY=( $( compgen -W "-q --quiet -a --all --no-trunc -v --viz -t --tree" -- "$cur" ) )
  287. ;;
  288. *)
  289. local counter=$(__docker_pos_first_nonflag)
  290. if [ $cword -eq $counter ]; then
  291. __docker_image_repos
  292. fi
  293. ;;
  294. esac
  295. }
  296. _docker_import() {
  297. local counter=$(__docker_pos_first_nonflag)
  298. if [ $cword -eq $counter ]; then
  299. return
  300. fi
  301. (( counter++ ))
  302. if [ $cword -eq $counter ]; then
  303. __docker_image_repos_and_tags
  304. return
  305. fi
  306. }
  307. _docker_info() {
  308. return
  309. }
  310. _docker_inspect() {
  311. case "$prev" in
  312. -f|--format)
  313. return
  314. ;;
  315. esac
  316. case "$cur" in
  317. -*)
  318. COMPREPLY=( $( compgen -W "-f --format" -- "$cur" ) )
  319. ;;
  320. *)
  321. __docker_containers_and_images
  322. ;;
  323. esac
  324. }
  325. _docker_kill() {
  326. __docker_containers_running
  327. }
  328. _docker_load() {
  329. return
  330. }
  331. _docker_login() {
  332. case "$prev" in
  333. -u|--username|-p|--password|-e|--email)
  334. return
  335. ;;
  336. esac
  337. case "$cur" in
  338. -*)
  339. COMPREPLY=( $( compgen -W "-u --username -p --password -e --email" -- "$cur" ) )
  340. ;;
  341. esac
  342. }
  343. _docker_logs() {
  344. case "$cur" in
  345. -*)
  346. COMPREPLY=( $( compgen -W "-f --follow" -- "$cur" ) )
  347. ;;
  348. *)
  349. local counter=$(__docker_pos_first_nonflag)
  350. if [ $cword -eq $counter ]; then
  351. __docker_containers_all
  352. fi
  353. ;;
  354. esac
  355. }
  356. _docker_pause() {
  357. local counter=$(__docker_pos_first_nonflag)
  358. if [ $cword -eq $counter ]; then
  359. __docker_containers_pauseable
  360. fi
  361. }
  362. _docker_port() {
  363. local counter=$(__docker_pos_first_nonflag)
  364. if [ $cword -eq $counter ]; then
  365. __docker_containers_all
  366. fi
  367. }
  368. _docker_ps() {
  369. case "$prev" in
  370. --since|--before)
  371. __docker_containers_all
  372. ;;
  373. -n)
  374. return
  375. ;;
  376. esac
  377. case "$cur" in
  378. -*)
  379. COMPREPLY=( $( compgen -W "-q --quiet -s --size -a --all --no-trunc -l --latest --since --before -n" -- "$cur" ) )
  380. ;;
  381. esac
  382. }
  383. _docker_pull() {
  384. case "$prev" in
  385. -t|--tag)
  386. return
  387. ;;
  388. esac
  389. case "$cur" in
  390. -*)
  391. COMPREPLY=( $( compgen -W "-t --tag" -- "$cur" ) )
  392. ;;
  393. *)
  394. local counter=$(__docker_pos_first_nonflag '-t|--tag')
  395. if [ $cword -eq $counter ]; then
  396. __docker_image_repos_and_tags
  397. fi
  398. ;;
  399. esac
  400. }
  401. _docker_push() {
  402. local counter=$(__docker_pos_first_nonflag)
  403. if [ $cword -eq $counter ]; then
  404. __docker_image_repos_and_tags
  405. fi
  406. }
  407. _docker_restart() {
  408. case "$prev" in
  409. -t|--time)
  410. return
  411. ;;
  412. esac
  413. case "$cur" in
  414. -*)
  415. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  416. ;;
  417. *)
  418. __docker_containers_all
  419. ;;
  420. esac
  421. }
  422. _docker_rm() {
  423. case "$cur" in
  424. -*)
  425. COMPREPLY=( $( compgen -W "-f --force -l --link -v --volumes" -- "$cur" ) )
  426. return
  427. ;;
  428. *)
  429. for arg in "${COMP_WORDS[@]}"; do
  430. case "$arg" in
  431. -f|--force)
  432. __docker_containers_all
  433. return
  434. ;;
  435. esac
  436. done
  437. __docker_containers_stopped
  438. return
  439. ;;
  440. esac
  441. }
  442. _docker_rmi() {
  443. __docker_image_repos_and_tags_and_ids
  444. }
  445. _docker_run() {
  446. local options_with_args="
  447. -a --attach
  448. --add-host
  449. --cap-add
  450. --cap-drop
  451. -c --cpu-shares
  452. --cidfile
  453. --cpuset
  454. --device
  455. --dns
  456. --dns-search
  457. -e --env
  458. --entrypoint
  459. --env-file
  460. --expose
  461. -h --hostname
  462. --link
  463. --lxc-conf
  464. -m --memory
  465. --name
  466. --net
  467. -p --publish
  468. --restart
  469. --security-opt
  470. -u --user
  471. --volumes-from
  472. -v --volume
  473. -w --workdir
  474. "
  475. local all_options="$options_with_args
  476. -i --interactive
  477. -P --publish-all
  478. --privileged
  479. -t --tty
  480. "
  481. [ "$command" = "run" ] && all_options="$all_options
  482. -d --detach
  483. --rm
  484. --sig-proxy
  485. "
  486. case "$prev" in
  487. -a|--attach)
  488. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  489. return
  490. ;;
  491. --cidfile|--env-file)
  492. _filedir
  493. return
  494. ;;
  495. --volumes-from)
  496. __docker_containers_all
  497. return
  498. ;;
  499. -v|--volume|--device)
  500. case "$cur" in
  501. *:*)
  502. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  503. ;;
  504. '')
  505. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  506. compopt -o nospace
  507. ;;
  508. /*)
  509. _filedir
  510. compopt -o nospace
  511. ;;
  512. esac
  513. return
  514. ;;
  515. -e|--env)
  516. COMPREPLY=( $( compgen -e -- "$cur" ) )
  517. compopt -o nospace
  518. return
  519. ;;
  520. --link)
  521. case "$cur" in
  522. *:*)
  523. ;;
  524. *)
  525. __docker_containers_running
  526. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  527. compopt -o nospace
  528. ;;
  529. esac
  530. return
  531. ;;
  532. --add-host)
  533. case "$cur" in
  534. *:)
  535. __docker_resolve_hostname
  536. return
  537. ;;
  538. esac
  539. ;;
  540. --cap-add|--cap-drop)
  541. __docker_capabilities
  542. return
  543. ;;
  544. --net)
  545. case "$cur" in
  546. container:*)
  547. local cur=${cur#*:}
  548. __docker_containers_all
  549. ;;
  550. *)
  551. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  552. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  553. compopt -o nospace
  554. fi
  555. ;;
  556. esac
  557. return
  558. ;;
  559. --restart)
  560. case "$cur" in
  561. on-failure:*)
  562. ;;
  563. *)
  564. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  565. ;;
  566. esac
  567. return
  568. ;;
  569. --security-opt)
  570. case "$cur" in
  571. label:*:*)
  572. ;;
  573. label:*)
  574. local cur=${cur##*:}
  575. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  576. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  577. compopt -o nospace
  578. fi
  579. ;;
  580. *)
  581. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  582. compopt -o nospace
  583. ;;
  584. esac
  585. return
  586. ;;
  587. --entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|--cpuset|-c|--cpu-shares|-n|--name|-p|--publish|--expose|--dns|--lxc-conf|--dns-search)
  588. return
  589. ;;
  590. esac
  591. case "$cur" in
  592. -*)
  593. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  594. ;;
  595. *)
  596. local counter=$( __docker_pos_first_nonflag $( echo $options_with_args | tr -d "\n" | tr " " "|" ) )
  597. if [ $cword -eq $counter ]; then
  598. __docker_image_repos_and_tags_and_ids
  599. fi
  600. ;;
  601. esac
  602. }
  603. _docker_save() {
  604. local counter=$(__docker_pos_first_nonflag)
  605. if [ $cword -eq $counter ]; then
  606. __docker_image_repos_and_tags_and_ids
  607. fi
  608. }
  609. _docker_search() {
  610. case "$prev" in
  611. -s|--stars)
  612. return
  613. ;;
  614. esac
  615. case "$cur" in
  616. -*)
  617. COMPREPLY=( $( compgen -W "--no-trunc --automated -s --stars" -- "$cur" ) )
  618. ;;
  619. esac
  620. }
  621. _docker_start() {
  622. case "$cur" in
  623. -*)
  624. COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) )
  625. ;;
  626. *)
  627. __docker_containers_stopped
  628. ;;
  629. esac
  630. }
  631. _docker_stop() {
  632. case "$prev" in
  633. -t|--time)
  634. return
  635. ;;
  636. esac
  637. case "$cur" in
  638. -*)
  639. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  640. ;;
  641. *)
  642. __docker_containers_running
  643. ;;
  644. esac
  645. }
  646. _docker_tag() {
  647. case "$cur" in
  648. -*)
  649. COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) )
  650. ;;
  651. *)
  652. local counter=$(__docker_pos_first_nonflag)
  653. if [ $cword -eq $counter ]; then
  654. __docker_image_repos_and_tags
  655. return
  656. fi
  657. (( counter++ ))
  658. if [ $cword -eq $counter ]; then
  659. __docker_image_repos_and_tags
  660. return
  661. fi
  662. ;;
  663. esac
  664. }
  665. _docker_unpause() {
  666. local counter=$(__docker_pos_first_nonflag)
  667. if [ $cword -eq $counter ]; then
  668. __docker_containers_unpauseable
  669. fi
  670. }
  671. _docker_top() {
  672. local counter=$(__docker_pos_first_nonflag)
  673. if [ $cword -eq $counter ]; then
  674. __docker_containers_running
  675. fi
  676. }
  677. _docker_version() {
  678. return
  679. }
  680. _docker_wait() {
  681. __docker_containers_all
  682. }
  683. _docker() {
  684. local commands=(
  685. attach
  686. build
  687. commit
  688. cp
  689. create
  690. diff
  691. events
  692. exec
  693. export
  694. history
  695. images
  696. import
  697. info
  698. insert
  699. inspect
  700. kill
  701. load
  702. login
  703. logs
  704. pause
  705. port
  706. ps
  707. pull
  708. push
  709. restart
  710. rm
  711. rmi
  712. run
  713. save
  714. search
  715. start
  716. stop
  717. tag
  718. top
  719. unpause
  720. version
  721. wait
  722. )
  723. COMPREPLY=()
  724. local cur prev words cword
  725. _get_comp_words_by_ref -n : cur prev words cword
  726. local command='docker' cpos=0
  727. local counter=1
  728. while [ $counter -lt $cword ]; do
  729. case "${words[$counter]}" in
  730. -H)
  731. (( counter++ ))
  732. ;;
  733. -*)
  734. ;;
  735. *)
  736. command="${words[$counter]}"
  737. cpos=$counter
  738. (( cpos++ ))
  739. break
  740. ;;
  741. esac
  742. (( counter++ ))
  743. done
  744. local completions_func=_docker_${command}
  745. declare -F $completions_func >/dev/null && $completions_func
  746. return 0
  747. }
  748. complete -F _docker docker