1 #compdef docker-compose
5 # zsh completion for docker-compose
6 # -------------------------------------------------------------------------
9 # * Steve Durrheimer <s.durrheimer@gmail.com>
10 # -------------------------------------------------------------------------
13 # * @albers docker-compose bash completion script
14 # * @felixr docker zsh completion script : https://github.com/felixr/docker-zsh-completion
15 # -------------------------------------------------------------------------
17 __docker-compose_q() {
18 docker-compose 2>/dev/null $compose_options "$@"
21 # All services defined in docker-compose.yml
22 __docker-compose_all_services_in_compose_file() {
23 local already_selected
25 already_selected=$(echo $words | tr " " "|")
26 __docker-compose_q ps --services "$@" \
27 | grep -Ev "^(${already_selected})$"
30 # All services, even those without an existing container
31 __docker-compose_services_all() {
32 [[ $PREFIX = -* ]] && return 1
34 services=$(__docker-compose_all_services_in_compose_file "$@")
35 _alternative "args:services:($services)" && ret=0
40 # All services that are defined by a Dockerfile reference
41 __docker-compose_services_from_build() {
42 [[ $PREFIX = -* ]] && return 1
43 __docker-compose_services_all --filter source=build
46 # All services that are defined by an image
47 __docker-compose_services_from_image() {
48 [[ $PREFIX = -* ]] && return 1
49 __docker-compose_services_all --filter source=image
52 __docker-compose_pausedservices() {
53 [[ $PREFIX = -* ]] && return 1
54 __docker-compose_services_all --filter status=paused
57 __docker-compose_stoppedservices() {
58 [[ $PREFIX = -* ]] && return 1
59 __docker-compose_services_all --filter status=stopped
62 __docker-compose_runningservices() {
63 [[ $PREFIX = -* ]] && return 1
64 __docker-compose_services_all --filter status=running
67 __docker-compose_services() {
68 [[ $PREFIX = -* ]] && return 1
69 __docker-compose_services_all
72 __docker-compose_caching_policy() {
73 oldp=( "$1"(Nmh+1) ) # 1 hour
77 __docker-compose_commands() {
80 zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
81 if [[ -z "$cache_policy" ]]; then
82 zstyle ":completion:${curcontext}:" cache-policy __docker-compose_caching_policy
85 if ( [[ ${+_docker_compose_subcommands} -eq 0 ]] || _cache_invalid docker_compose_subcommands) \
86 && ! _retrieve_cache docker_compose_subcommands;
89 lines=(${(f)"$(_call_program commands docker-compose 2>&1)"})
90 _docker_compose_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I) *]}]}## #}/ ##/:})
91 (( $#_docker_compose_subcommands > 0 )) && _store_cache docker_compose_subcommands _docker_compose_subcommands
93 _describe -t docker-compose-commands "docker-compose command" _docker_compose_subcommands
96 __docker-compose_subcommand() {
97 local opts_help opts_force_recreate opts_no_recreate opts_no_build opts_remove_orphans opts_timeout opts_no_color opts_no_deps
99 opts_help='(: -)--help[Print usage]'
100 opts_force_recreate="(--no-recreate)--force-recreate[Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.]"
101 opts_no_recreate="(--force-recreate)--no-recreate[If containers already exist, don't recreate them. Incompatible with --force-recreate.]"
102 opts_no_build="(--build)--no-build[Don't build an image, even if it's missing.]"
103 opts_remove_orphans="--remove-orphans[Remove containers for services not defined in the Compose file]"
104 opts_timeout=('(-t --timeout)'{-t,--timeout}"[Specify a shutdown timeout in seconds. (default: 10)]:seconds: ")
105 opts_no_color='--no-color[Produce monochrome output.]'
106 opts_no_deps="--no-deps[Don't start linked services.]"
114 "*--build-arg=[Set build-time variables for one service.]:<varname>=<value>: " \
115 '--force-rm[Always remove intermediate containers.]' \
116 '(--quiet -q)'{--quiet,-q}'[Curb build output]' \
117 '(--memory -m)'{--memory,-m}'[Memory limit for the build container.]' \
118 '--no-cache[Do not use cache when building the image.]' \
119 '--pull[Always attempt to pull a newer version of the image.]' \
120 '--compress[Compress the build context using gzip.]' \
121 '--parallel[Build images in parallel.]' \
122 '*:services:__docker-compose_services_from_build' && ret=0
127 '(--quiet -q)'{--quiet,-q}"[Only validate the configuration, don't print anything.]" \
128 '--resolve-image-digests[Pin image tags to digests.]' \
129 '--services[Print the service names, one per line.]' \
130 '--volumes[Print the volume names, one per line.]' \
131 '--hash[Print the service config hash, one per line. Set "service1,service2" for a list of specified services.]' \ && ret=0
136 $opts_force_recreate \
139 "(--no-build)--build[Build images before creating containers.]" \
140 '*:services:__docker-compose_services' && ret=0
146 "--rmi[Remove images. Type must be one of: 'all': Remove all images used by any service. 'local': Remove only images that don't have a custom tag set by the \`image\` field.]:type:(all local)" \
147 '(-v --volumes)'{-v,--volumes}"[Remove named volumes declared in the \`volumes\` section of the Compose file and anonymous volumes attached to containers.]" \
148 $opts_remove_orphans && ret=0
153 '--json[Output events as a stream of json objects]' \
154 '*:services:__docker-compose_services' && ret=0
159 '-d[Detached mode: Run command in the background.]' \
160 '--privileged[Give extended privileges to the process.]' \
161 '(-u --user)'{-u,--user=}'[Run the command as this user.]:username:_users' \
162 '-T[Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY.]' \
163 '--index=[Index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
164 '*'{-e,--env}'[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
165 '(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
166 '(-):running services:__docker-compose_runningservices' \
167 '(-):command: _command_names -e' \
168 '*::arguments: _normal' && ret=0
171 _arguments ':subcommand:__docker-compose_commands' && ret=0
176 '-q[Only display IDs]' \
177 '*:services:__docker-compose_services' && ret=0
182 '-s[SIGNAL to send to the container. Default signal is SIGKILL.]:signal:_signals' \
183 '*:running services:__docker-compose_runningservices' && ret=0
188 '(-f --follow)'{-f,--follow}'[Follow log output]' \
190 '--tail=[Number of lines to show from the end of the logs for each container.]:number of lines: ' \
191 '(-t --timestamps)'{-t,--timestamps}'[Show timestamps]' \
192 '*:services:__docker-compose_services' && ret=0
197 '*:running services:__docker-compose_runningservices' && ret=0
202 '--protocol=[tcp or udp \[default: tcp\]]:protocol:(tcp udp)' \
203 '--index=[index of the container if there are multiple instances of a service \[default: 1\]]:index: ' \
204 '1:running services:__docker-compose_runningservices' \
205 '2:port:_ports' && ret=0
210 '-q[Only display IDs]' \
211 '--filter KEY=VAL[Filter services by a property]:<filtername>=<value>:' \
212 '*:services:__docker-compose_services' && ret=0
217 '--ignore-pull-failures[Pull what it can and ignores images with pull failures.]' \
218 '--no-parallel[Disable parallel pulling]' \
219 '(-q --quiet)'{-q,--quiet}'[Pull without printing progress information]' \
220 '--include-deps[Also pull services declared as dependencies]' \
221 '*:services:__docker-compose_services_from_image' && ret=0
226 '--ignore-push-failures[Push what it can and ignores images with push failures.]' \
227 '*:services:__docker-compose_services' && ret=0
232 '(-f --force)'{-f,--force}"[Don't ask to confirm removal]" \
233 '-v[Remove any anonymous volumes attached to containers]' \
234 '*:stopped services:__docker-compose_stoppedservices' && ret=0
240 '-d[Detached mode: Run container in the background, print new container name.]' \
241 '*-e[KEY=VAL Set an environment variable (can be used multiple times)]:environment variable KEY=VAL: ' \
242 '*'{-l,--label}'[KEY=VAL Add or override a label (can be used multiple times)]:label KEY=VAL: ' \
243 '--entrypoint[Overwrite the entrypoint of the image.]:entry point: ' \
244 '--name=[Assign a name to the container]:name: ' \
245 '(-p --publish)'{-p,--publish=}"[Publish a container's port(s) to the host]" \
246 '--rm[Remove container after run. Ignored in detached mode.]' \
247 "--service-ports[Run command with the service's ports enabled and mapped to the host.]" \
248 '-T[Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.]' \
249 '(-u --user)'{-u,--user=}'[Run as specified username or uid]:username or uid:_users' \
250 '(-v --volume)*'{-v,--volume=}'[Bind mount a volume]:volume: ' \
251 '(-w --workdir)'{-w,--workdir=}'[Working directory inside the container]:workdir: ' \
252 "--use-aliases[Use the services network aliases in the network(s) the container connects to]" \
253 '(-):services:__docker-compose_services' \
254 '(-):command: _command_names -e' \
255 '*::arguments: _normal' && ret=0
261 '*:running services:__docker-compose_runningservices' && ret=0
266 '*:stopped services:__docker-compose_stoppedservices' && ret=0
272 '*:running services:__docker-compose_runningservices' && ret=0
277 '*:running services:__docker-compose_runningservices' && ret=0
282 '*:paused services:__docker-compose_pausedservices' && ret=0
287 '(--abort-on-container-exit)-d[Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit and --attach-dependencies.]' \
290 $opts_force_recreate \
293 "(--no-build)--build[Build images before starting containers.]" \
294 "(-d)--abort-on-container-exit[Stops all containers if any container was stopped. Incompatible with -d.]" \
295 "(-d)--attach-dependencies[Attach to dependent containers. Incompatible with -d.]" \
296 '(-t --timeout)'{-t,--timeout}"[Use this timeout in seconds for container shutdown when attached or when containers are already running. (default: 10)]:seconds: " \
297 '--scale[SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.]:service scale SERVICE=NUM: ' \
298 '--exit-code-from=[Return the exit code of the selected service container. Implies --abort-on-container-exit]:service:__docker-compose_services' \
299 $opts_remove_orphans \
300 '*:services:__docker-compose_services' && ret=0
305 "--short[Shows only Compose's version number.]" && ret=0
308 _message 'Unknown sub command' && ret=1
316 # Support for subservices, which allows for `compdef _docker docker-shell=_docker_containers`.
317 # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
318 if [[ $service != docker-compose ]]; then
319 _call_function - _$service
323 local curcontext="$curcontext" state line
327 local file_description
329 if [[ -n ${words[(r)-f]} || -n ${words[(r)--file]} ]] ; then
330 file_description="Specify an override docker-compose file (default: docker-compose.override.yml)"
332 file_description="Specify an alternate docker-compose file (default: docker-compose.yml)"
336 '(- :)'{-h,--help}'[Get help]' \
337 '*'{-f,--file}"[${file_description}]:file:_files -g '*.yml'" \
338 '(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
339 '--env-file[Specify an alternate environment file (default: .env)]:env-file:_files' \
340 "--compatibility[If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent]" \
341 '(- :)'{-v,--version}'[Print version and exit]' \
342 '--verbose[Show more output]' \
343 '--log-level=[Set log level]:level:(DEBUG INFO WARNING ERROR CRITICAL)' \
344 '--no-ansi[Do not print ANSI control characters]' \
345 '--ansi=[Control when to print ANSI control characters]:when:(never always auto)' \
346 '(-H --host)'{-H,--host}'[Daemon socket to connect to]:host:' \
347 '--tls[Use TLS; implied by --tlsverify]' \
348 '--tlscacert=[Trust certs signed only by this CA]:ca path:' \
349 '--tlscert=[Path to TLS certificate file]:client cert path:' \
350 '--tlskey=[Path to TLS key file]:tls key path:' \
351 '--tlsverify[Use TLS and verify the remote]' \
352 "--skip-hostname-check[Don't check the daemon's hostname against the name specified in the client certificate (for example if your docker host is an IP address)]" \
354 '(-)*:: :->option-or-argument' && ret=0
356 local -a relevant_compose_flags relevant_compose_repeatable_flags relevant_docker_flags compose_options docker_options
358 relevant_compose_flags=(
362 "--project-name" "-p"
368 "--skip-hostname-check"
371 relevant_compose_repeatable_flags=(
375 relevant_docker_flags=(
384 for k in "${(@k)opt_args}"; do
385 if [[ -n "${relevant_docker_flags[(r)$k]}" ]]; then
387 if [[ -n "$opt_args[$k]" ]]; then
388 docker_options+=$opt_args[$k]
391 if [[ -n "${relevant_compose_flags[(r)$k]}" ]]; then
392 if [[ -n "${relevant_compose_repeatable_flags[(r)$k]}" ]]; then
393 values=("${(@s/:/)opt_args[$k]}")
397 compose_options+=$value
401 if [[ -n "$opt_args[$k]" ]]; then
402 compose_options+=$opt_args[$k]
410 __docker-compose_commands && ret=0
413 curcontext=${curcontext%:*:*}:docker-compose-$words[1]:
414 __docker-compose_subcommand && ret=0