_docker 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. _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. __stats() {
  189. __docker_containers
  190. }
  191. __stop() {
  192. _arguments \
  193. '(-t,--time=)'{-t,--time=}'[Number of seconds to wait for the container to stop before killing it.]'
  194. __docker_containers
  195. }
  196. __tag() {
  197. _arguments \
  198. '(-f,--force=)'{-f,--force=}'[Force]'
  199. __docker_images
  200. }
  201. __version() {
  202. # no arguments
  203. }
  204. __wait() {
  205. __docker_containers
  206. }
  207. __exec() {
  208. _arguments \
  209. '(-d,--detach=)'{-d,--detach=}'[Detached mode: run command in the background]' \
  210. '(-i,--interactive=)'{-i,--interactive=}'[Keep STDIN open even if not attached]' \
  211. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-TTY]'
  212. __docker_containers
  213. }
  214. # end commands ---------
  215. # ----------------------
  216. local -a _1st_arguments
  217. _1st_arguments=(
  218. "attach":"Attach to a running container"
  219. "build":"Build a container from a Dockerfile"
  220. "commit":"Create a new image from a container's changes"
  221. "cp":"Copy files/folders from the containers filesystem to the host path"
  222. "diff":"Inspect changes on a container's filesystem"
  223. "events":"Get real time events from the server"
  224. "export":"Stream the contents of a container as a tar archive"
  225. "history":"Show the history of an image"
  226. "images":"List images"
  227. "import":"Create a new filesystem image from the contents of a tarball"
  228. "info":"Display system-wide information"
  229. "insert":"Insert a file in an image"
  230. "inspect":"Return low-level information on a container"
  231. "kill":"Kill a running container"
  232. "load":"Load an image from a tar archive"
  233. "login":"Register or Login to the docker registry server"
  234. "logs":"Fetch the logs of a container"
  235. "port":"Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"
  236. "ps":"List containers"
  237. "pull":"Pull an image or a repository from the docker registry server"
  238. "push":"Push an image or a repository to the docker registry server"
  239. "restart":"Restart a running container"
  240. "rm":"Remove one or more containers"
  241. "rmi":"Remove one or more images"
  242. "run":"Run a command in a new container"
  243. "save":"Save an image to a tar archive"
  244. "search":"Search for an image in the docker index"
  245. "start":"Start a stopped container"
  246. "stats":"Display a live stream of one or more containers' resource usage statistics"
  247. "stop":"Stop a running container"
  248. "tag":"Tag an image into a repository"
  249. "top":"Lookup the running processes of a container"
  250. "version":"Show the docker version information"
  251. "wait":"Block until a container stops, then print its exit code"
  252. "exec":"Run a task inside a running container"
  253. )
  254. _arguments '*:: :->command'
  255. if (( CURRENT == 1 )); then
  256. _describe -t commands "docker command" _1st_arguments
  257. return
  258. fi
  259. local -a _command_args
  260. case "$words[1]" in
  261. attach)
  262. __attach ;;
  263. build)
  264. __build ;;
  265. commit)
  266. __commit ;;
  267. cp)
  268. __cp ;;
  269. diff)
  270. __diff ;;
  271. events)
  272. __events ;;
  273. export)
  274. __export ;;
  275. history)
  276. __history ;;
  277. images)
  278. __images ;;
  279. import)
  280. __import ;;
  281. info)
  282. __info ;;
  283. insert)
  284. __insert ;;
  285. inspect)
  286. __inspect ;;
  287. kill)
  288. __kill ;;
  289. load)
  290. __load ;;
  291. login)
  292. __login ;;
  293. logs)
  294. __logs ;;
  295. port)
  296. __port ;;
  297. ps)
  298. __ps ;;
  299. pull)
  300. __pull ;;
  301. push)
  302. __push ;;
  303. restart)
  304. __restart ;;
  305. rm)
  306. __rm ;;
  307. rmi)
  308. __rmi ;;
  309. run)
  310. __run ;;
  311. save)
  312. __save ;;
  313. search)
  314. __search ;;
  315. stats)
  316. __stats ;;
  317. start)
  318. __start ;;
  319. stop)
  320. __stop ;;
  321. tag)
  322. __tag ;;
  323. top)
  324. __top ;;
  325. version)
  326. __version ;;
  327. wait)
  328. __wait ;;
  329. exec)
  330. __exec ;;
  331. esac