_docker 11.1 KB
Newer Older
Ahmed Azaan's avatar
Ahmed Azaan committed
1
2
3
4
#compdef docker

# Docker autocompletion for oh-my-zsh
# Requires: Docker installed
5
6
# Author: Azaan (@aeonazaan)
# Updates: Bob Maerten (@bobmaerten) for Docker v0.9+
7
#          Paul van den Berg (@bergvandenp) for Docker v1.3+ 
Ahmed Azaan's avatar
Ahmed Azaan committed
8
9
10
11
12
13


# ----- Helper functions
# Output a selectable list of all running docker containers
__docker_containers() {
    declare -a cont_cmd
14
    cont_cmd=($(docker ps | awk 'NR>1{print $NF":[CON("$1")"$2"("$3")]"}'))
15
16
17
18
19
20
21
22
23
24
    if [[  'X$cont_cmd' != 'X' ]]
        _describe 'containers' cont_cmd
}

# Output a selectable list of all containers, even not running
__docker_all_containers() {
    declare -a cont_cmd
    cont_cmd=($(docker ps -a | awk 'NR>1{print $NF":[CON("$1")"$2"("$3")]"}'))
    if [[  'X$cont_cmd' != 'X' ]]
        _describe 'containers' cont_cmd
Ahmed Azaan's avatar
Ahmed Azaan committed
25
26
27
28
29
30
31
32
33
34
35
36
37
}

# output a selectable list of all docker images
__docker_images() {
    declare -a img_cmd
    img_cmd=($(docker images | awk 'NR>1{print $1}'))
    _describe 'images' img_cmd
}

# ----- Commands
# Seperate function for each command, makes extension easier later
# ---------------------------
__attach() {
38
39
40
    _arguments \
        '--no-stdin[Do not attach stdin]' \
        '--sig-proxy[Proxify all received signal to the process (even in non-tty mode)]'
Ahmed Azaan's avatar
Ahmed Azaan committed
41
42
43
44
45
    __docker_containers
}

__build() {
    _arguments \
46
47
48
49
        '--no-cache[Do not use cache when building the image]' \
        '(-q,--quiet)'{-q,--quiet}'[Suppress the verbose output generated by the containers]' \
        '--rm[Remove intermediate containers after a successful build]' \
        '(-t,--tag=)'{-t,--tag=}'[Repository name (and optionally a tag) to be applied to the resulting image in case of success]' \
Ahmed Azaan's avatar
Ahmed Azaan committed
50
        '*:files:_files'
Ahmed Azaan's avatar
Ahmed Azaan committed
51
52
53
54
}

__commit() {
    _arguments \
55
56
57
58
59
60
61
        '(-a,--author=)'{-a,--author=}'[Author (eg. "John Hannibal Smith <hannibal@a-team.com>"]' \
        '(-m,--message=)'{-m,--message=}'[Commit message]' \
        '--run=[Config automatically applied when the image is run.]'
    __docker_containers
}

__cp() {
Ahmed Azaan's avatar
Ahmed Azaan committed
62
63
64
65
66
67
68
    __docker_containers
}

__diff() {
    __docker_containers
}

69
__events() {    
70
71
72
73
    _arguments \
        '--since=[Show previously created events and then stream.]'
}

Ahmed Azaan's avatar
Ahmed Azaan committed
74
75
76
77
78
__export() {
   __docker_containers
}

__history() {
79
80
81
    _arguments \
        '--no-trunc=[Don''t truncate output]' \
        '(-q,--quiet)'{-q,--quiet}'[Only show numeric IDs]'
Ahmed Azaan's avatar
Ahmed Azaan committed
82
83
84
85
86
    __docker_images
}

__images() {
    _arguments \
87
88
89
90
91
        '(-a,--all)'{-a,--all}'[Show all images (by default filter out the intermediate images used to build)]' \
        '--no-trunc[Don''t truncate output]' \
        '(-q,--quiet=)'{-q,--quiet=}'[Only show numeric IDs]' \
        '(-t,--tree=)'{-t,--tree=}'[Output graph in tree format]' \
        '(-v,--viz=)'{-v,--viz=}'[Output graph in graphviz format]'
Ahmed Azaan's avatar
Ahmed Azaan committed
92
93
94
95
96
97
98
99
100
101
102
103
104
    __docker_images
}

__import() {
    _arguments '*:files:_files'
}

__info() {
    # no arguments
}

__inspect() {
    __docker_images
105
    __docker_all_containers
Ahmed Azaan's avatar
Ahmed Azaan committed
106
107
108
}

__kill() {
109
110
    _arguments \
        '(-s,--signal=)'{-s,--signal=}'[KILL Signal]'
Ahmed Azaan's avatar
Ahmed Azaan committed
111
112
113
    __docker_containers
}

114
115
116
117
__load() {
    _arguments '*:files:_files'
}

Ahmed Azaan's avatar
Ahmed Azaan committed
118
119
__login() {
    _arguments \
120
121
122
        '(-e,--email=)'{-e,-email=}'[Email]' \
        '(-p,--password=)'{-p,-password=}'[Password]' \
        '(-u,--username=)'{-u,-username=}'[Username]'
Ahmed Azaan's avatar
Ahmed Azaan committed
123
124
125
}

__logs() {
126
127
    _arguments \
        '(-f,--follow)'{-f,-follow}'[Follow log output]'
Ahmed Azaan's avatar
Ahmed Azaan committed
128
129
130
131
132
133
134
135
136
137
138
139
140
    __docker_containers
}

__port() {
    __docker_containers
}

__top() {
    __docker_containers
}

__ps() {
    _arguments \
141
142
143
144
145
146
147
148
        '(-a,--all)'{-a,--all}'[Show all containers. Only running containers are shown by default.]' \
        '--before-id=[Show only container created before Id, include non-running ones.]' \
        '(-l,--latest)'{-l,--latest}'[Show only the latest created container, include non-running ones.]' \
        '-n=[Show n last created containers, include non-running ones. default=-1.]' \
        '--no-trunc[Don''t truncate output]' \
        '(-q,--quiet)'{-q,--quiet}'[Only display numeric IDs]' \
        '(-s,--size)'{-s,--size}'[Display sizes]' \
        '--since-id=[Show only containers created since Id, include non-running ones.]'
Ahmed Azaan's avatar
Ahmed Azaan committed
149
150
151
}

__pull() {
152
153
    _arguments \
        '(-t,--tag=)'{-t,--tag=}'[Download tagged image in repository]'
Ahmed Azaan's avatar
Ahmed Azaan committed
154
155
156
}

__push() {
157
    # no arguments
Ahmed Azaan's avatar
Ahmed Azaan committed
158
159
160
}

__restart() {
161
162
    _arguments \
        '(-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]'
Ahmed Azaan's avatar
Ahmed Azaan committed
163
164
165
166
    __docker_containers
}

__rm() {
167
168
169
170
    _arguments \
        '(-f,--force=)'{-f,--force=}'[Force removal of running container]' \
        '(-l,--link=)'{-l,--link=}'[Remove the specified link and not the underlying container]' \
        '(-v,--volumes=)'{-v,--volumes=}'[Remove the volumes associated to the container]'
171
    __docker_all_containers
Ahmed Azaan's avatar
Ahmed Azaan committed
172
173
174
}

__rmi() {
175
    _arguments \
176
        '(-f,--force=)'{-f,--force=}'[Force]'
Ahmed Azaan's avatar
Ahmed Azaan committed
177
178
179
180
181
    __docker_images
}

__run() {
    _arguments \
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        '(-P,--publish-all=)'{-P,--publish-all=}'[Publish all exposed ports to the host interfaces]' \
        '(-a,--attach=)'{-a,--attach=}'[Attach to stdin, stdout or stderr.]' \
        '(-c,--cpu-shares=)'{-c,--cpu-shares=}': CPU shares (relative weight)]' \
        '--cidfile=[Write the container ID to the file]' \
        '(-d,--detach=)'{-d,--detach=}'[Detached mode: Run container in the background, print new container id]' \
        '--dns=[Set custom dns servers]' \
        '(-e,--env=)'{-e,--env=}'[Set environment variables]' \
        '--entrypoint=[Overwrite the default entrypoint of the image]' \
        '--expose=[Expose a port from the container without publishing it to your host]' \
        '(-h,--hostname=)'{-h,--hostname=}'[Container host name]' \
        '(-i,--interactive=)'{-i,--interactive=}'[Keep stdin open even if not attached]' \
        '--link=[Add link to another container (name:alias)]' \
        '--lxc-conf=[Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"]' \
        '(-m,--memory=)'{-m,--memory=}'[Memory limit (format: <number><optional unit>, where unit = b, k, m or g)]' \
        '(-n,--networking=)'{-n,--networking=}'[Enable networking for this container]' \
        '--name=[Assign a name to the container]' \
        '(-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)]' \
        '--privileged=[Give extended privileges to this container]' \
        '--rm=[Automatically remove the container when it exits (incompatible with -d)]' \
        '--sig-proxy=[Proxify all received signal to the process (even in non-tty mode)]' \
        '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-tty]' \
        '(-u,--user=)'{-u,--user=}'[Username or UID]' \
        '(-v,--volume=)'{-v,--volume=}'[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]' \
        '--volumes-from=[Mount volumes from the specified container(s)]' \
        '(-w,--workdir=)'{-w,--workdir=}'[Working directory inside the container]'
Ahmed Azaan's avatar
Ahmed Azaan committed
207
208
209
210
    __docker_images
}

__search() {
211
212
213
214
215
216
217
218
    _arguments \
        '--no-trunc=[Don''t truncate output]' \
        '-s,--stars=)'{-s,--stars=}'[Only displays with at least xxx stars]' \
        '-t,--trusted=)'{-t,--trusted=}'[Only show trusted builds]'
}

__save() {
    __docker_images
Ahmed Azaan's avatar
Ahmed Azaan committed
219
220
221
}

__start() {
222
223
    _arguments \
        '(-a,--attach=)'{-a,--attach=}'[Attach container''s stdout/stderr and forward all signals to the process]' \
Josh Comer's avatar
Josh Comer committed
224
        '(-i,--interactive=)'{-i,--interactive=}'[Attach container''s stdin]'
225
    __docker_all_containers
Ahmed Azaan's avatar
Ahmed Azaan committed
226
227
}

228
229
230
231
__stats() {
    __docker_containers
}

Ahmed Azaan's avatar
Ahmed Azaan committed
232
__stop() {
233
234
    _arguments \
        '(-t,--time=)'{-t,--time=}'[Number of seconds to wait for the container to stop before killing it.]'
Ahmed Azaan's avatar
Ahmed Azaan committed
235
236
237
238
    __docker_containers
}

__tag() {
239
240
    _arguments \
        '(-f,--force=)'{-f,--force=}'[Force]'
Ahmed Azaan's avatar
Ahmed Azaan committed
241
242
243
244
    __docker_images
}

__version() {
245
    # no arguments
Ahmed Azaan's avatar
Ahmed Azaan committed
246
247
248
}

__wait() {
Ahmed Azaan's avatar
Ahmed Azaan committed
249
    __docker_containers
Ahmed Azaan's avatar
Ahmed Azaan committed
250
251
}

252
253
254
255
256
257
258
259
__exec() {
    _arguments \
      '(-d,--detach=)'{-d,--detach=}'[Detached mode: run command in the background]' \
      '(-i,--interactive=)'{-i,--interactive=}'[Keep STDIN open even if not attached]' \
      '(-t,--tty=)'{-t,--tty=}'[Allocate a pseudo-TTY]'
    __docker_containers
}

Ahmed Azaan's avatar
Ahmed Azaan committed
260
261
262
263
264
265
266
267
# end commands ---------
# ----------------------

local -a _1st_arguments
_1st_arguments=(
    "attach":"Attach to a running container"
    "build":"Build a container from a Dockerfile"
    "commit":"Create a new image from a container's changes"
268
    "cp":"Copy files/folders from the containers filesystem to the host path"
Ahmed Azaan's avatar
Ahmed Azaan committed
269
    "diff":"Inspect changes on a container's filesystem"
270
    "events":"Get real time events from the server"
Ahmed Azaan's avatar
Ahmed Azaan committed
271
272
273
274
275
276
277
    "export":"Stream the contents of a container as a tar archive"
    "history":"Show the history of an image"
    "images":"List images"
    "import":"Create a new filesystem image from the contents of a tarball"
    "info":"Display system-wide information"
    "inspect":"Return low-level information on a container"
    "kill":"Kill a running container"
278
    "load":"Load an image from a tar archive"
Ahmed Azaan's avatar
Ahmed Azaan committed
279
280
281
282
283
284
285
286
287
288
    "login":"Register or Login to the docker registry server"
    "logs":"Fetch the logs of a container"
    "port":"Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"
    "ps":"List containers"
    "pull":"Pull an image or a repository from the docker registry server"
    "push":"Push an image or a repository to the docker registry server"
    "restart":"Restart a running container"
    "rm":"Remove one or more containers"
    "rmi":"Remove one or more images"
    "run":"Run a command in a new container"
289
    "save":"Save an image to a tar archive"
Ahmed Azaan's avatar
Ahmed Azaan committed
290
291
    "search":"Search for an image in the docker index"
    "start":"Start a stopped container"
292
    "stats":"Display a live stream of one or more containers' resource usage statistics"
Ahmed Azaan's avatar
Ahmed Azaan committed
293
294
    "stop":"Stop a running container"
    "tag":"Tag an image into a repository"
295
    "top":"Lookup the running processes of a container"
Ahmed Azaan's avatar
Ahmed Azaan committed
296
297
    "version":"Show the docker version information"
    "wait":"Block until a container stops, then print its exit code"
298
    "exec":"Run a task inside a running container"
Ahmed Azaan's avatar
Ahmed Azaan committed
299
300
301
302
303
304
305
306
307
308
309
310
)

_arguments '*:: :->command'

if (( CURRENT == 1 )); then
    _describe -t commands "docker command" _1st_arguments
    return
fi

local -a _command_args
case "$words[1]" in
    attach)
311
       __attach ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
312
313
314
315
    build)
        __build ;;
    commit)
        __commit ;;
316
317
    cp)
        __cp ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
318
319
    diff)
        __diff ;;
320
321
    events)
        __events ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
    export)
        __export ;;
    history)
        __history ;;
    images)
        __images ;;
    import)
        __import ;;
    info)
        __info ;;
    inspect)
        __inspect ;;
    kill)
        __kill ;;
336
337
    load)
        __load ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    login)
        __login ;;
    logs)
        __logs ;;
    port)
        __port ;;
    ps)
        __ps ;;
    pull)
        __pull ;;
    push)
        __push ;;
    restart)
        __restart ;;
    rm)
        __rm ;;
    rmi)
        __rmi ;;
    run)
        __run ;;
358
359
    save)
        __save ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
360
361
    search)
        __search ;;
362
363
    stats)
        __stats ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
364
365
366
367
368
369
    start)
        __start ;;
    stop)
        __stop ;;
    tag)
        __tag ;;
370
371
    top)
        __top ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
372
373
374
375
    version)
        __version ;;
    wait)
        __wait ;;
376
377
    exec)
        __exec ;;
Ahmed Azaan's avatar
Ahmed Azaan committed
378
esac