_docker 10 KB

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