_docker 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. __insert() {
  87. __docker_images
  88. _arguments '*:files:_files'
  89. }
  90. __inspect() {
  91. __docker_images
  92. __docker_all_containers
  93. }
  94. __kill() {
  95. _arguments \
  96. '(-s,--signal=)'{-s,--signal=}'[KILL Signal]'
  97. __docker_containers
  98. }
  99. __load() {
  100. _arguments '*:files:_files'
  101. }
  102. __login() {
  103. _arguments \
  104. '(-e,--email=)'{-e,-email=}'[Email]' \
  105. '(-p,--password=)'{-p,-password=}'[Password]' \
  106. '(-u,--username=)'{-u,-username=}'[Username]'
  107. }
  108. __logs() {
  109. _arguments \
  110. '(-f,--follow)'{-f,-follow}'[Follow log output]'
  111. __docker_containers
  112. }
  113. __port() {
  114. __docker_containers
  115. }
  116. __top() {
  117. __docker_containers
  118. }
  119. __ps() {
  120. _arguments \
  121. '(-a,--all)'{-a,--all}'[Show all containers. Only running containers are shown by default.]' \
  122. '--before-id=[Show only container created before Id, include non-running ones.]' \
  123. '(-l,--latest)'{-l,--latest}'[Show only the latest created container, include non-running ones.]' \
  124. '-n=[Show n last created containers, include non-running ones. default=-1.]' \
  125. '--no-trunc[Don''t truncate output]' \
  126. '(-q,--quiet)'{-q,--quiet}'[Only display numeric IDs]' \
  127. '(-s,--size)'{-s,--size}'[Display sizes]' \
  128. '--since-id=[Show only containers created since Id, include non-running ones.]'
  129. }
  130. __pull() {
  131. _arguments \
  132. '(-t,--tag=)'{-t,--tag=}'[Download tagged image in repository]'
  133. }
  134. __push() {
  135. # no arguments
  136. }
  137. __restart() {
  138. _arguments \
  139. '(-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]'
  140. __docker_containers
  141. }
  142. __rm() {
  143. _arguments \
  144. '(-f,--force=)'{-f,--force=}'[Force removal of running container]' \
  145. '(-l,--link=)'{-l,--link=}'[Remove the specified link and not the underlying container]' \
  146. '(-v,--volumes=)'{-v,--volumes=}'[Remove the volumes associated to the container]'
  147. __docker_all_containers
  148. }
  149. __rmi() {
  150. _arguments \
  151. '(-f,--force=)'{-f,--force=}'[Force]'
  152. __docker_images
  153. }
  154. __run() {
  155. _arguments \
  156. '(-P,--publish-all=)'{-P,--publish-all=}'[Publish all exposed ports to the host interfaces]' \
  157. '(-a,--attach=)'{-a,--attach=}'[Attach to stdin, stdout or stderr.]' \
  158. '(-c,--cpu-shares=)'{-c,--cpu-shares=}': CPU shares (relative weight)]' \
  159. '--cidfile=[Write the container ID to the file]' \
  160. '(-d,--detach=)'{-d,--detach=}'[Detached mode: Run container in the background, print new container id]' \
  161. '--dns=[Set custom dns servers]' \
  162. '(-e,--env=)'{-e,--env=}'[Set environment variables]' \
  163. '--entrypoint=[Overwrite the default entrypoint of the image]' \
  164. '--expose=[Expose a port from the container without publishing it to your host]' \
  165. '(-h,--hostname=)'{-h,--hostname=}'[Container host name]' \
  166. '(-i,--interactive=)'{-i,--interactive=}'[Keep stdin open even if not attached]' \
  167. '--link=[Add link to another container (name:alias)]' \
  168. '--lxc-conf=[Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"]' \
  169. '(-m,--memory=)'{-m,--memory=}'[Memory limit (format: <number><optional unit>, where unit = b, k, m or g)]' \
  170. '(-n,--networking=)'{-n,--networking=}'[Enable networking for this container]' \
  171. '--name=[Assign a name to the container]' \
  172. '(-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)]' \
  173. '--privileged=[Give extended privileges to this container]' \
  174. '--rm=[Automatically remove the container when it exits (incompatible with -d)]' \
  175. '--sig-proxy=[Proxify all received signal to the process (even in non-tty mode)]' \
  176. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-tty]' \
  177. '(-u,--user=)'{-u,--user=}'[Username or UID]' \
  178. '(-v,--volume=)'{-v,--volume=}'[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]' \
  179. '--volumes-from=[Mount volumes from the specified container(s)]' \
  180. '(-w,--workdir=)'{-w,--workdir=}'[Working directory inside the container]'
  181. __docker_images
  182. }
  183. __search() {
  184. _arguments \
  185. '--no-trunc=[Don''t truncate output]' \
  186. '-s,--stars=)'{-s,--stars=}'[Only displays with at least xxx stars]' \
  187. '-t,--trusted=)'{-t,--trusted=}'[Only show trusted builds]'
  188. }
  189. __save() {
  190. __docker_images
  191. }
  192. __start() {
  193. _arguments \
  194. '(-a,--attach=)'{-a,--attach=}'[Attach container''s stdout/stderr and forward all signals to the process]' \
  195. '(-i,--interactive=)'{-i,--interactive=}'[Attach container''s stdin]'
  196. __docker_all_containers
  197. }
  198. __stats() {
  199. __docker_containers
  200. }
  201. __stop() {
  202. _arguments \
  203. '(-t,--time=)'{-t,--time=}'[Number of seconds to wait for the container to stop before killing it.]'
  204. __docker_containers
  205. }
  206. __tag() {
  207. _arguments \
  208. '(-f,--force=)'{-f,--force=}'[Force]'
  209. __docker_images
  210. }
  211. __version() {
  212. # no arguments
  213. }
  214. __wait() {
  215. __docker_containers
  216. }
  217. __exec() {
  218. _arguments \
  219. '(-d,--detach=)'{-d,--detach=}'[Detached mode: run command in the background]' \
  220. '(-i,--interactive=)'{-i,--interactive=}'[Keep STDIN open even if not attached]' \
  221. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-TTY]'
  222. __docker_containers
  223. }
  224. # end commands ---------
  225. # ----------------------
  226. local -a _1st_arguments
  227. _1st_arguments=(
  228. "attach":"Attach to a running container"
  229. "build":"Build a container from a Dockerfile"
  230. "commit":"Create a new image from a container's changes"
  231. "cp":"Copy files/folders from the containers filesystem to the host path"
  232. "diff":"Inspect changes on a container's filesystem"
  233. "events":"Get real time events from the server"
  234. "export":"Stream the contents of a container as a tar archive"
  235. "history":"Show the history of an image"
  236. "images":"List images"
  237. "import":"Create a new filesystem image from the contents of a tarball"
  238. "info":"Display system-wide information"
  239. "insert":"Insert a file in an image"
  240. "inspect":"Return low-level information on a container"
  241. "kill":"Kill a running container"
  242. "load":"Load an image from a tar archive"
  243. "login":"Register or Login to the docker registry server"
  244. "logs":"Fetch the logs of a container"
  245. "port":"Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"
  246. "ps":"List containers"
  247. "pull":"Pull an image or a repository from the docker registry server"
  248. "push":"Push an image or a repository to the docker registry server"
  249. "restart":"Restart a running container"
  250. "rm":"Remove one or more containers"
  251. "rmi":"Remove one or more images"
  252. "run":"Run a command in a new container"
  253. "save":"Save an image to a tar archive"
  254. "search":"Search for an image in the docker index"
  255. "start":"Start a stopped container"
  256. "stats":"Display a live stream of one or more containers' resource usage statistics"
  257. "stop":"Stop a running container"
  258. "tag":"Tag an image into a repository"
  259. "top":"Lookup the running processes of a container"
  260. "version":"Show the docker version information"
  261. "wait":"Block until a container stops, then print its exit code"
  262. "exec":"Run a task inside a running container"
  263. )
  264. _arguments '*:: :->command'
  265. if (( CURRENT == 1 )); then
  266. _describe -t commands "docker command" _1st_arguments
  267. return
  268. fi
  269. local -a _command_args
  270. case "$words[1]" in
  271. attach)
  272. __attach ;;
  273. build)
  274. __build ;;
  275. commit)
  276. __commit ;;
  277. cp)
  278. __cp ;;
  279. diff)
  280. __diff ;;
  281. events)
  282. __events ;;
  283. export)
  284. __export ;;
  285. history)
  286. __history ;;
  287. images)
  288. __images ;;
  289. import)
  290. __import ;;
  291. info)
  292. __info ;;
  293. insert)
  294. __insert ;;
  295. inspect)
  296. __inspect ;;
  297. kill)
  298. __kill ;;
  299. load)
  300. __load ;;
  301. login)
  302. __login ;;
  303. logs)
  304. __logs ;;
  305. port)
  306. __port ;;
  307. ps)
  308. __ps ;;
  309. pull)
  310. __pull ;;
  311. push)
  312. __push ;;
  313. restart)
  314. __restart ;;
  315. rm)
  316. __rm ;;
  317. rmi)
  318. __rmi ;;
  319. run)
  320. __run ;;
  321. save)
  322. __save ;;
  323. search)
  324. __search ;;
  325. stats)
  326. __stats ;;
  327. start)
  328. __start ;;
  329. stop)
  330. __stop ;;
  331. tag)
  332. __tag ;;
  333. top)
  334. __top ;;
  335. version)
  336. __version ;;
  337. wait)
  338. __wait ;;
  339. exec)
  340. __exec ;;
  341. esac