_docker 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #compdef docker
  2. # Docker autocompletion for oh-my-zsh
  3. # Requires: Docker installed
  4. # Author: Azaan (@aeonazaan)
  5. # Updates: Bob Maerten (@bobmaerten) for Docker v0.9+
  6. # Paul van den Berg (@bergvandenp) for Docker v1.3+
  7. # ----- Helper functions
  8. # Output a selectable list of all running docker containers
  9. __docker_containers() {
  10. declare -a cont_cmd
  11. cont_cmd=($(docker ps | awk 'NR>1{print $NF":[CON("$1")"$2"("$3")]"}'))
  12. if [[ 'X$cont_cmd' != 'X' ]]
  13. _describe 'containers' cont_cmd
  14. }
  15. # Output a selectable list of all containers, even not running
  16. __docker_all_containers() {
  17. declare -a cont_cmd
  18. cont_cmd=($(docker ps -a | awk 'NR>1{print $NF":[CON("$1")"$2"("$3")]"}'))
  19. if [[ 'X$cont_cmd' != 'X' ]]
  20. _describe 'containers' cont_cmd
  21. }
  22. # output a selectable list of all docker images
  23. __docker_images() {
  24. declare -a img_cmd
  25. img_cmd=($(docker images | awk 'NR>1{print $1}'))
  26. _describe 'images' img_cmd
  27. }
  28. # ----- Commands
  29. # Seperate function for each command, makes extension easier later
  30. # ---------------------------
  31. __attach() {
  32. _arguments \
  33. '--no-stdin[Do not attach stdin]' \
  34. '--sig-proxy[Proxify all received signal to the process (even in non-tty mode)]'
  35. __docker_containers
  36. }
  37. __build() {
  38. _arguments \
  39. '--no-cache[Do not use cache when building the image]' \
  40. '(-q,--quiet)'{-q,--quiet}'[Suppress the verbose output generated by the containers]' \
  41. '--rm[Remove intermediate containers after a successful build]' \
  42. '(-t,--tag=)'{-t,--tag=}'[Repository name (and optionally a tag) to be applied to the resulting image in case of success]' \
  43. '*:files:_files'
  44. }
  45. __commit() {
  46. _arguments \
  47. '(-a,--author=)'{-a,--author=}'[Author (eg. "John Hannibal Smith <hannibal@a-team.com>"]' \
  48. '(-m,--message=)'{-m,--message=}'[Commit message]' \
  49. '--run=[Config automatically applied when the image is run.]'
  50. __docker_containers
  51. }
  52. __cp() {
  53. __docker_containers
  54. }
  55. __diff() {
  56. __docker_containers
  57. }
  58. __events() {
  59. _arguments \
  60. '--since=[Show previously created events and then stream.]'
  61. }
  62. __export() {
  63. __docker_containers
  64. }
  65. __history() {
  66. _arguments \
  67. '--no-trunc=[Don''t truncate output]' \
  68. '(-q,--quiet)'{-q,--quiet}'[Only show numeric IDs]'
  69. __docker_images
  70. }
  71. __images() {
  72. _arguments \
  73. '(-a,--all)'{-a,--all}'[Show all images (by default filter out the intermediate images used to build)]' \
  74. '--no-trunc[Don''t truncate output]' \
  75. '(-q,--quiet=)'{-q,--quiet=}'[Only show numeric IDs]' \
  76. '(-t,--tree=)'{-t,--tree=}'[Output graph in tree format]' \
  77. '(-v,--viz=)'{-v,--viz=}'[Output graph in graphviz format]'
  78. __docker_images
  79. }
  80. __import() {
  81. _arguments '*:files:_files'
  82. }
  83. __info() {
  84. # no arguments
  85. }
  86. __inspect() {
  87. __docker_images
  88. __docker_all_containers
  89. }
  90. __kill() {
  91. _arguments \
  92. '(-s,--signal=)'{-s,--signal=}'[KILL Signal]'
  93. __docker_containers
  94. }
  95. __load() {
  96. _arguments '*:files:_files'
  97. }
  98. __login() {
  99. _arguments \
  100. '(-e,--email=)'{-e,-email=}'[Email]' \
  101. '(-p,--password=)'{-p,-password=}'[Password]' \
  102. '(-u,--username=)'{-u,-username=}'[Username]'
  103. }
  104. __logs() {
  105. _arguments \
  106. '(-f,--follow)'{-f,-follow}'[Follow log output]'
  107. __docker_containers
  108. }
  109. __port() {
  110. __docker_containers
  111. }
  112. __top() {
  113. __docker_containers
  114. }
  115. __ps() {
  116. _arguments \
  117. '(-a,--all)'{-a,--all}'[Show all containers. Only running containers are shown by default.]' \
  118. '--before-id=[Show only container created before Id, include non-running ones.]' \
  119. '(-l,--latest)'{-l,--latest}'[Show only the latest created container, include non-running ones.]' \
  120. '-n=[Show n last created containers, include non-running ones. default=-1.]' \
  121. '--no-trunc[Don''t truncate output]' \
  122. '(-q,--quiet)'{-q,--quiet}'[Only display numeric IDs]' \
  123. '(-s,--size)'{-s,--size}'[Display sizes]' \
  124. '--since-id=[Show only containers created since Id, include non-running ones.]'
  125. }
  126. __pull() {
  127. _arguments \
  128. '(-t,--tag=)'{-t,--tag=}'[Download tagged image in repository]'
  129. }
  130. __push() {
  131. # no arguments
  132. }
  133. __restart() {
  134. _arguments \
  135. '(-t,--time=)'{-t,--time=}'[Number of seconds to try to stop for before killing the container. Once killed it will then be restarted. Default=10]'
  136. __docker_containers
  137. }
  138. __rm() {
  139. _arguments \
  140. '(-f,--force=)'{-f,--force=}'[Force removal of running container]' \
  141. '(-l,--link=)'{-l,--link=}'[Remove the specified link and not the underlying container]' \
  142. '(-v,--volumes=)'{-v,--volumes=}'[Remove the volumes associated to the container]'
  143. __docker_all_containers
  144. }
  145. __rmi() {
  146. _arguments \
  147. '(-f,--force=)'{-f,--force=}'[Force]'
  148. __docker_images
  149. }
  150. __run() {
  151. _arguments \
  152. '(-P,--publish-all=)'{-P,--publish-all=}'[Publish all exposed ports to the host interfaces]' \
  153. '(-a,--attach=)'{-a,--attach=}'[Attach to stdin, stdout or stderr.]' \
  154. '(-c,--cpu-shares=)'{-c,--cpu-shares=}': CPU shares (relative weight)]' \
  155. '--cidfile=[Write the container ID to the file]' \
  156. '(-d,--detach=)'{-d,--detach=}'[Detached mode: Run container in the background, print new container id]' \
  157. '--dns=[Set custom dns servers]' \
  158. '(-e,--env=)'{-e,--env=}'[Set environment variables]' \
  159. '--entrypoint=[Overwrite the default entrypoint of the image]' \
  160. '--expose=[Expose a port from the container without publishing it to your host]' \
  161. '(-h,--hostname=)'{-h,--hostname=}'[Container host name]' \
  162. '(-i,--interactive=)'{-i,--interactive=}'[Keep stdin open even if not attached]' \
  163. '--link=[Add link to another container (name:alias)]' \
  164. '--lxc-conf=[Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"]' \
  165. '(-m,--memory=)'{-m,--memory=}'[Memory limit (format: <number><optional unit>, where unit = b, k, m or g)]' \
  166. '(-n,--networking=)'{-n,--networking=}'[Enable networking for this container]' \
  167. '--name=[Assign a name to the container]' \
  168. '(-p,--publish=)'{-p,--publish=}'[Publish a container''s port to the host (format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort) (use "docker port" to see the actual mapping)]' \
  169. '--privileged=[Give extended privileges to this container]' \
  170. '--rm=[Automatically remove the container when it exits (incompatible with -d)]' \
  171. '--sig-proxy=[Proxify all received signal to the process (even in non-tty mode)]' \
  172. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-tty]' \
  173. '(-u,--user=)'{-u,--user=}'[Username or UID]' \
  174. '(-v,--volume=)'{-v,--volume=}'[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]' \
  175. '--volumes-from=[Mount volumes from the specified container(s)]' \
  176. '(-w,--workdir=)'{-w,--workdir=}'[Working directory inside the container]'
  177. __docker_images
  178. }
  179. __search() {
  180. _arguments \
  181. '--no-trunc=[Don''t truncate output]' \
  182. '-s,--stars=)'{-s,--stars=}'[Only displays with at least xxx stars]' \
  183. '-t,--trusted=)'{-t,--trusted=}'[Only show trusted builds]'
  184. }
  185. __save() {
  186. __docker_images
  187. }
  188. __start() {
  189. _arguments \
  190. '(-a,--attach=)'{-a,--attach=}'[Attach container''s stdout/stderr and forward all signals to the process]' \
  191. '(-i,--interactive=)'{-i,--interactive=}'[Attach container''s stdin]'
  192. __docker_all_containers
  193. }
  194. __stats() {
  195. __docker_containers
  196. }
  197. __stop() {
  198. _arguments \
  199. '(-t,--time=)'{-t,--time=}'[Number of seconds to wait for the container to stop before killing it.]'
  200. __docker_containers
  201. }
  202. __tag() {
  203. _arguments \
  204. '(-f,--force=)'{-f,--force=}'[Force]'
  205. __docker_images
  206. }
  207. __version() {
  208. # no arguments
  209. }
  210. __wait() {
  211. __docker_containers
  212. }
  213. __exec() {
  214. _arguments \
  215. '(-d,--detach=)'{-d,--detach=}'[Detached mode: run command in the background]' \
  216. '(-i,--interactive=)'{-i,--interactive=}'[Keep STDIN open even if not attached]' \
  217. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-TTY]'
  218. __docker_containers
  219. }
  220. # end commands ---------
  221. # ----------------------
  222. local -a _1st_arguments
  223. _1st_arguments=(
  224. "attach":"Attach to a running container"
  225. "build":"Build a container from a Dockerfile"
  226. "commit":"Create a new image from a container's changes"
  227. "cp":"Copy files/folders from the containers filesystem to the host path"
  228. "diff":"Inspect changes on a container's filesystem"
  229. "events":"Get real time events from the server"
  230. "export":"Stream the contents of a container as a tar archive"
  231. "history":"Show the history of an image"
  232. "images":"List images"
  233. "import":"Create a new filesystem image from the contents of a tarball"
  234. "info":"Display system-wide information"
  235. "inspect":"Return low-level information on a container"
  236. "kill":"Kill a running container"
  237. "load":"Load an image from a tar archive"
  238. "login":"Register or Login to the docker registry server"
  239. "logs":"Fetch the logs of a container"
  240. "port":"Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"
  241. "ps":"List containers"
  242. "pull":"Pull an image or a repository from the docker registry server"
  243. "push":"Push an image or a repository to the docker registry server"
  244. "restart":"Restart a running container"
  245. "rm":"Remove one or more containers"
  246. "rmi":"Remove one or more images"
  247. "run":"Run a command in a new container"
  248. "save":"Save an image to a tar archive"
  249. "search":"Search for an image in the docker index"
  250. "start":"Start a stopped container"
  251. "stats":"Display a live stream of one or more containers' resource usage statistics"
  252. "stop":"Stop a running container"
  253. "tag":"Tag an image into a repository"
  254. "top":"Lookup the running processes of a container"
  255. "version":"Show the docker version information"
  256. "wait":"Block until a container stops, then print its exit code"
  257. "exec":"Run a task inside a running container"
  258. )
  259. _arguments '*:: :->command'
  260. if (( CURRENT == 1 )); then
  261. _describe -t commands "docker command" _1st_arguments
  262. return
  263. fi
  264. local -a _command_args
  265. case "$words[1]" in
  266. attach)
  267. __attach ;;
  268. build)
  269. __build ;;
  270. commit)
  271. __commit ;;
  272. cp)
  273. __cp ;;
  274. diff)
  275. __diff ;;
  276. events)
  277. __events ;;
  278. export)
  279. __export ;;
  280. history)
  281. __history ;;
  282. images)
  283. __images ;;
  284. import)
  285. __import ;;
  286. info)
  287. __info ;;
  288. inspect)
  289. __inspect ;;
  290. kill)
  291. __kill ;;
  292. load)
  293. __load ;;
  294. login)
  295. __login ;;
  296. logs)
  297. __logs ;;
  298. port)
  299. __port ;;
  300. ps)
  301. __ps ;;
  302. pull)
  303. __pull ;;
  304. push)
  305. __push ;;
  306. restart)
  307. __restart ;;
  308. rm)
  309. __rm ;;
  310. rmi)
  311. __rmi ;;
  312. run)
  313. __run ;;
  314. save)
  315. __save ;;
  316. search)
  317. __search ;;
  318. stats)
  319. __stats ;;
  320. start)
  321. __start ;;
  322. stop)
  323. __stop ;;
  324. tag)
  325. __tag ;;
  326. top)
  327. __top ;;
  328. version)
  329. __version ;;
  330. wait)
  331. __wait ;;
  332. exec)
  333. __exec ;;
  334. esac