_docker 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 (e.g. "John Hannibal Smith <hannibal@a-team.com>")]' \
  48. '(-c,--change=)'{-c,--change=}'[Apply Dockerfile instruction to the created image]' \
  49. '(-m,--message=)'{-m,--message=}'[Commit message]' \
  50. '(-p,--pause=)'{-p,--pause=}'[Pause container during commit]' \
  51. }
  52. __cp() {
  53. __docker_containers
  54. }
  55. __create() {
  56. _arguments \
  57. '(-P,--publish-all=)'{-P,--publish-all=}'[Publish all exposed ports to the host interfaces]' \
  58. '(-a,--attach=)'{-a,--attach=}'[Attach to STDIN, STDOUT or STDERR]' \
  59. '--add-host=[Add a custom host-to-IP mapping]' \
  60. '--cap-add=[Add Linux capabilities]' \
  61. '--cap-drop=[Drop Linux capabilities]' \
  62. '--cpuset-cpus=[CPUs in which to allow execution (0-3, 0,1)]' \
  63. '(-c,--cpu-shares=)'{-c,--cpu-shares=}'[CPU shares (relative weight)]' \
  64. '--cidfile=[Write the container ID to the file]' \
  65. '--device=[Add a host device to the container]' \
  66. '--dns=[Set custom dns servers]' \
  67. '--dns-search=[Set custom DNS search domains]' \
  68. '(-e,--env=)'{-e,--env=}'[Set environment variables]' \
  69. '--env-file=[Read in a file of environment variables]' \
  70. '--entrypoint=[Overwrite the default entrypoint of the image]' \
  71. '--expose=[Expose a port from the container without publishing it to your host]' \
  72. '(-h,--hostname=)'{-h,--hostname=}'[Container host name]' \
  73. '(-i,--interactive=)'{-i,--interactive=}'[Keep STDIN open even if not attached]' \
  74. '--ipc=[IPC namespace to use]' \
  75. '(-l,--label=)'{-l,--label=}'[Set meta data on a container]' \
  76. '--link=[Add link to another container (name:alias)]' \
  77. '--log-driver=[Logging driver for the container]' \
  78. '--lxc-conf=[Add custom LXC options]' \
  79. '--mac-address=[Container MAC address (e.g. 92:d0:c6:0a:29:33)]' \
  80. '(-m,--memory=)'{-m,--memory=}'[Memory limit (format: <number><optional unit>, where unit = b, k, m or g)]' \
  81. '--net=[Set the Network mode for the container]' \
  82. '--name=[Assign a name to the container]' \
  83. '--pid=[PID namespace to use]' \
  84. '(-p,--publish=)'{-p,--publish=}'[Publish a container''s port to the host (format: ip:hostPort:containerPort/protocol)]' \
  85. '--privileged=[Give extended privileges to this container]' \
  86. '--restart=[Restart policy to apply when a container exits]' \
  87. '--security-opt=[Security Options]' \
  88. '--sig-proxy=[Proxify all received signal to the process (even in non-tty mode)]' \
  89. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-tty]' \
  90. '(-u,--user=)'{-u,--user=}'[Username or UID]' \
  91. '--ulimit=[Ulimit options]' \
  92. '(-v,--volume=)'{-v,--volume=}'[Bind mount a volume (e.g. -v /host:/container or -v /container)]' \
  93. '--volumes-from=[Mount volumes from the specified container(s)]' \
  94. '(-w,--workdir=)'{-w,--workdir=}'[Working directory inside the container]'
  95. __docker_images
  96. }
  97. __diff() {
  98. __docker_containers
  99. }
  100. __events() {
  101. _arguments \
  102. '--since=[Show previously created events and then stream.]'
  103. }
  104. __export() {
  105. __docker_containers
  106. }
  107. __history() {
  108. _arguments \
  109. '--no-trunc=[Don''t truncate output]' \
  110. '(-q,--quiet)'{-q,--quiet}'[Only show numeric IDs]'
  111. __docker_images
  112. }
  113. __images() {
  114. _arguments \
  115. '(-a,--all)'{-a,--all}'[Show all images (by default filter out the intermediate images used to build)]' \
  116. '--no-trunc[Don''t truncate output]' \
  117. '(-q,--quiet=)'{-q,--quiet=}'[Only show numeric IDs]' \
  118. '(-t,--tree=)'{-t,--tree=}'[Output graph in tree format]' \
  119. '(-v,--viz=)'{-v,--viz=}'[Output graph in graphviz format]'
  120. __docker_images
  121. }
  122. __import() {
  123. _arguments '*:files:_files'
  124. }
  125. __info() {
  126. # no arguments
  127. }
  128. __inspect() {
  129. __docker_images
  130. __docker_all_containers
  131. }
  132. __kill() {
  133. _arguments \
  134. '(-s,--signal=)'{-s,--signal=}'[KILL Signal]'
  135. __docker_containers
  136. }
  137. __load() {
  138. _arguments '*:files:_files'
  139. }
  140. __login() {
  141. _arguments \
  142. '(-e,--email=)'{-e,-email=}'[Email]' \
  143. '(-p,--password=)'{-p,-password=}'[Password]' \
  144. '(-u,--username=)'{-u,-username=}'[Username]'
  145. }
  146. __logs() {
  147. _arguments \
  148. '(-f,--follow)'{-f,-follow}'[Follow log output]'
  149. __docker_containers
  150. }
  151. __port() {
  152. __docker_containers
  153. }
  154. __top() {
  155. __docker_containers
  156. }
  157. __ps() {
  158. _arguments \
  159. '(-a,--all)'{-a,--all}'[Show all containers. Only running containers are shown by default.]' \
  160. '--before-id=[Show only container created before Id, include non-running ones.]' \
  161. '(-l,--latest)'{-l,--latest}'[Show only the latest created container, include non-running ones.]' \
  162. '-n=[Show n last created containers, include non-running ones. default=-1.]' \
  163. '--no-trunc[Don''t truncate output]' \
  164. '(-q,--quiet)'{-q,--quiet}'[Only display numeric IDs]' \
  165. '(-s,--size)'{-s,--size}'[Display sizes]' \
  166. '--since-id=[Show only containers created since Id, include non-running ones.]'
  167. }
  168. __pull() {
  169. _arguments \
  170. '(-t,--tag=)'{-t,--tag=}'[Download tagged image in repository]'
  171. }
  172. __push() {
  173. # no arguments
  174. }
  175. __restart() {
  176. _arguments \
  177. '(-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]'
  178. __docker_containers
  179. }
  180. __rm() {
  181. _arguments \
  182. '(-f,--force=)'{-f,--force=}'[Force removal of running container]' \
  183. '(-l,--link=)'{-l,--link=}'[Remove the specified link and not the underlying container]' \
  184. '(-v,--volumes=)'{-v,--volumes=}'[Remove the volumes associated to the container]'
  185. __docker_all_containers
  186. }
  187. __rmi() {
  188. _arguments \
  189. '(-f,--force=)'{-f,--force=}'[Force]'
  190. __docker_images
  191. }
  192. __run() {
  193. _arguments \
  194. '(-P,--publish-all=)'{-P,--publish-all=}'[Publish all exposed ports to the host interfaces]' \
  195. '(-a,--attach=)'{-a,--attach=}'[Attach to STDIN, STDOUT or STDERR]' \
  196. '--add-host=[Add a custom host-to-IP mapping]' \
  197. '--cap-add=[Add Linux capabilities]' \
  198. '--cap-drop=[Drop Linux capabilities]' \
  199. '--cpuset-cpus=[CPUs in which to allow execution (0-3, 0,1)]' \
  200. '(-c,--cpu-shares=)'{-c,--cpu-shares=}'[CPU shares (relative weight)]' \
  201. '--cidfile=[Write the container ID to the file]' \
  202. '(-d,--detach=)'{-d,--detach=}'[Run container in the background, print new container id]' \
  203. '--device=[Add a host device to the container]' \
  204. '--dns=[Set custom dns servers]' \
  205. '--dns-search=[Set custom DNS search domains]' \
  206. '(-e,--env=)'{-e,--env=}'[Set environment variables]' \
  207. '--env-file=[Read in a file of environment variables]' \
  208. '--entrypoint=[Overwrite the default entrypoint of the image]' \
  209. '--expose=[Expose a port from the container without publishing it to your host]' \
  210. '(-h,--hostname=)'{-h,--hostname=}'[Container host name]' \
  211. '(-i,--interactive=)'{-i,--interactive=}'[Keep STDIN open even if not attached]' \
  212. '--ipc=[IPC namespace to use]' \
  213. '(-l,--label=)'{-l,--label=}'[Set meta data on a container]' \
  214. '--link=[Add link to another container (name:alias)]' \
  215. '--log-driver=[Logging driver for the container]' \
  216. '--lxc-conf=[Add custom LXC options]' \
  217. '--mac-address=[Container MAC address (e.g. 92:d0:c6:0a:29:33)]' \
  218. '(-m,--memory=)'{-m,--memory=}'[Memory limit (format: <number><optional unit>, where unit = b, k, m or g)]' \
  219. '--net=[Set the Network mode for the container]' \
  220. '--name=[Assign a name to the container]' \
  221. '--pid=[PID namespace to use]' \
  222. '(-p,--publish=)'{-p,--publish=}'[Publish a container''s port to the host (format: ip:hostPort:containerPort/protocol)]' \
  223. '--privileged=[Give extended privileges to this container]' \
  224. '--restart=[Restart policy to apply when a container exits]' \
  225. '--rm=[Automatically remove the container when it exits (incompatible with -d)]' \
  226. '--security-opt=[Security Options]' \
  227. '--sig-proxy=[Proxify all received signal to the process (even in non-tty mode)]' \
  228. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-tty]' \
  229. '(-u,--user=)'{-u,--user=}'[Username or UID]' \
  230. '--ulimit=[Ulimit options]' \
  231. '(-v,--volume=)'{-v,--volume=}'[Bind mount a volume (e.g. -v /host:/container or -v /container)]' \
  232. '--volumes-from=[Mount volumes from the specified container(s)]' \
  233. '(-w,--workdir=)'{-w,--workdir=}'[Working directory inside the container]'
  234. __docker_images
  235. }
  236. __search() {
  237. _arguments \
  238. '--no-trunc=[Don''t truncate output]' \
  239. '-s,--stars=)'{-s,--stars=}'[Only displays with at least xxx stars]' \
  240. '-t,--trusted=)'{-t,--trusted=}'[Only show trusted builds]'
  241. }
  242. __save() {
  243. __docker_images
  244. }
  245. __start() {
  246. _arguments \
  247. '(-a,--attach=)'{-a,--attach=}'[Attach container''s STDOUT/STDERR and forward all signals to the process]' \
  248. '(-i,--interactive=)'{-i,--interactive=}'[Attach container''s STDIN]'
  249. __docker_all_containers
  250. }
  251. __stats() {
  252. __docker_containers
  253. }
  254. __stop() {
  255. _arguments \
  256. '(-t,--time=)'{-t,--time=}'[Number of seconds to wait for the container to stop before killing it.]'
  257. __docker_containers
  258. }
  259. __tag() {
  260. _arguments \
  261. '(-f,--force=)'{-f,--force=}'[Force]'
  262. __docker_images
  263. }
  264. __version() {
  265. # no arguments
  266. }
  267. __wait() {
  268. __docker_containers
  269. }
  270. __exec() {
  271. _arguments \
  272. '(-d,--detach=)'{-d,--detach=}'[Detached mode: run command in the background]' \
  273. '(-i,--interactive=)'{-i,--interactive=}'[Keep STDIN open even if not attached]' \
  274. '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-TTY]'
  275. __docker_containers
  276. }
  277. # end commands ---------
  278. # ----------------------
  279. local -a _1st_arguments
  280. _1st_arguments=(
  281. "attach":"Attach to a running container"
  282. "build":"Build a container from a Dockerfile"
  283. "commit":"Create a new image from a container's changes"
  284. "cp":"Copy files/folders from the containers filesystem to the host path"
  285. "create":"Create new container without running it"
  286. "diff":"Inspect changes on a container's filesystem"
  287. "events":"Get real time events from the server"
  288. "export":"Stream the contents of a container as a tar archive"
  289. "history":"Show the history of an image"
  290. "images":"List images"
  291. "import":"Create a new filesystem image from the contents of a tarball"
  292. "info":"Display system-wide information"
  293. "inspect":"Return low-level information on a container"
  294. "kill":"Kill a running container"
  295. "load":"Load an image from a tar archive"
  296. "login":"Register or Login to the docker registry server"
  297. "logs":"Fetch the logs of a container"
  298. "port":"Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"
  299. "ps":"List containers"
  300. "pull":"Pull an image or a repository from the docker registry server"
  301. "push":"Push an image or a repository to the docker registry server"
  302. "restart":"Restart a running container"
  303. "rm":"Remove one or more containers"
  304. "rmi":"Remove one or more images"
  305. "run":"Run a command in a new container"
  306. "save":"Save an image to a tar archive"
  307. "search":"Search for an image in the docker index"
  308. "start":"Start a stopped container"
  309. "stats":"Display a live stream of one or more containers' resource usage statistics"
  310. "stop":"Stop a running container"
  311. "tag":"Tag an image into a repository"
  312. "top":"Lookup the running processes of a container"
  313. "version":"Show the docker version information"
  314. "wait":"Block until a container stops, then print its exit code"
  315. "exec":"Run a task inside a running container"
  316. )
  317. _arguments '*:: :->command'
  318. if (( CURRENT == 1 )); then
  319. _describe -t commands "docker command" _1st_arguments
  320. return
  321. fi
  322. local -a _command_args
  323. case "$words[1]" in
  324. attach)
  325. __attach ;;
  326. build)
  327. __build ;;
  328. commit)
  329. __commit ;;
  330. cp)
  331. __cp ;;
  332. create)
  333. __create ;;
  334. diff)
  335. __diff ;;
  336. events)
  337. __events ;;
  338. export)
  339. __export ;;
  340. history)
  341. __history ;;
  342. images)
  343. __images ;;
  344. import)
  345. __import ;;
  346. info)
  347. __info ;;
  348. inspect)
  349. __inspect ;;
  350. kill)
  351. __kill ;;
  352. load)
  353. __load ;;
  354. login)
  355. __login ;;
  356. logs)
  357. __logs ;;
  358. port)
  359. __port ;;
  360. ps)
  361. __ps ;;
  362. pull)
  363. __pull ;;
  364. push)
  365. __push ;;
  366. restart)
  367. __restart ;;
  368. rm)
  369. __rm ;;
  370. rmi)
  371. __rmi ;;
  372. run)
  373. __run ;;
  374. save)
  375. __save ;;
  376. search)
  377. __search ;;
  378. stats)
  379. __stats ;;
  380. start)
  381. __start ;;
  382. stop)
  383. __stop ;;
  384. tag)
  385. __tag ;;
  386. top)
  387. __top ;;
  388. version)
  389. __version ;;
  390. wait)
  391. __wait ;;
  392. exec)
  393. __exec ;;
  394. esac