_docker 11 KB

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