Skip to content

Commit 1a9522a

Browse files
authored
Add docker presets to stop & remove containers (#76)
* Add docker preset command to stop all containers * Add docker preset command to remove all containers * Add stop & remove docker presets in help * Update remove docker presets * Add remove stopped docker presets * Add alias remove docker presets * Update docker presets help text * Add support to run shell args * Sanatize argument for shell run * Fix sanitize arguments for shell run * Remove remove running no force docker preset
1 parent c5a51eb commit 1a9522a

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

dockerc

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,22 @@ else
163163
CONTEXT=''
164164
fi
165165

166-
# Other parameters are arguments passed to docker compose
167-
ARGS="$@"
168-
169166
# Check for docker preset
170167
if [ "$(echo "$CONTEXT" | cut -c1)" = '@' ]; then
171168

169+
# Other arguments to pass to docker
170+
# Use single quotes to safely pass arguments to shell
171+
# Escape single quotes in arguments
172+
ARGS=''
173+
while [ $# -gt 0 ]; do
174+
NEW_ARG=$(echo "$1" | sed "s/'/'\"'\"'/g")
175+
ARGS="$ARGS '$NEW_ARG'"
176+
shift
177+
done
178+
172179
PRINT_DOCKER_PRESETS_HELP=false
173180
DOCKER_ARGS=''
181+
RUN_SHELL=false
174182

175183
# Remove first char
176184
CONTEXT="${CONTEXT#?}"
@@ -200,6 +208,38 @@ if [ "$(echo "$CONTEXT" | cut -c1)" = '@' ]; then
200208
DOCKER_ARGS='system prune -f -a --volumes'
201209
;;
202210

211+
'sa')
212+
# Stop all containers
213+
DOCKER_ARGS='stop $(docker ps -q)'
214+
RUN_SHELL=true
215+
;;
216+
217+
'rmf')
218+
# Remove running containers, force
219+
DOCKER_ARGS='rm -f $(docker ps -a -q)'
220+
RUN_SHELL=true
221+
;;
222+
'rms')
223+
# Remove stopped containers
224+
DOCKER_ARGS='rm $(docker ps -a -q -f status=exited -f status=dead)'
225+
RUN_SHELL=true
226+
;;
227+
'rmsf'|'rmfs')
228+
# Remove stopped containers, force
229+
DOCKER_ARGS='rm -f $(docker ps -a -q -f status=exited -f status=dead)'
230+
RUN_SHELL=true
231+
;;
232+
'rma')
233+
# Remove all containers
234+
DOCKER_ARGS='rm $(docker ps -a -q)'
235+
RUN_SHELL=true
236+
;;
237+
'rmaf'|'rmfa')
238+
# Remove all containers, force
239+
DOCKER_ARGS='rm -f $(docker ps -q)'
240+
RUN_SHELL=true
241+
;;
242+
203243
*)
204244
echo "Error: Unknown docker preset '@$CONTEXT'"
205245
exit 1
@@ -218,29 +258,57 @@ if [ "$(echo "$CONTEXT" | cut -c1)" = '@' ]; then
218258
echo ' @rf Remove unused containers, networks and images'
219259
echo ' @rfa Remove all unused containers, networks and images'
220260
echo ' @rfav Remove all unused containers, networks, images and volumes'
261+
echo ' @sa Stop all running containers'
262+
echo ' @rmf Remove running containers, force'
263+
echo ' @rms Remove stopped containers'
264+
echo ' @rmsf Remove stopped containers, force'
265+
echo ' (aliases: @rmfs)'
266+
echo ' @rma Remove all containers'
267+
echo ' @rmaf Remove all containers, force'
268+
echo ' (aliases: @rmfa)'
221269
echo ' options:'
222270
echo ' -f Force, for example ignores abstract contexts'
223271
echo ' -n Dry run, print docker command without running it'
224272
echo ' -q Quiet, do not print docker command'
225273
exit 0
226274
fi
227275

228-
if [ "$QUIET" = false ]; then
229-
# Print docker command
230-
echo ''
231-
echo "> docker $DOCKER_ARGS $ARGS"
232-
echo ''
233-
fi
276+
if [ "$RUN_SHELL" = false ]; then
277+
278+
if [ "$QUIET" = false ]; then
279+
# Print docker command
280+
echo ''
281+
echo "> docker ${DOCKER_ARGS}${ARGS}"
282+
echo ''
283+
fi
284+
285+
if [ "$DRY_RUN" = false ]; then
286+
# Run docker command
287+
exec docker ${DOCKER_ARGS}${ARGS}
288+
fi
289+
290+
else # "$RUN_SHELL" = true
291+
292+
if [ "$QUIET" = false ]; then
293+
# Print docker command
294+
echo ''
295+
echo "> sh -c 'docker ${DOCKER_ARGS}${ARGS}'"
296+
echo ''
297+
fi
298+
299+
if [ "$DRY_RUN" = false ]; then
300+
# Run docker command
301+
exec sh -c "docker ${DOCKER_ARGS}${ARGS}"
302+
fi
234303

235-
if [ "$DRY_RUN" = false ]; then
236-
# Run docker
237-
exec docker $DOCKER_ARGS $ARGS
238304
fi
239305

240306
exit 0
241307

242308
fi
243309

310+
# Other parameters are arguments passed to docker compose
311+
ARGS="$@"
244312

245313
COMPOSE_FILE_ARGS=''
246314
NO_COMPOSE_FILE_ARGS=false

test/test/help_docker/test_help.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ def get_help_stdout(dockerc_path: str):
1111
b' @rf Remove unused containers, networks and images\n'
1212
b' @rfa Remove all unused containers, networks and images\n'
1313
b' @rfav Remove all unused containers, networks, images and volumes\n'
14+
b' @sa Stop all running containers\n'
15+
b' @rmf Remove running containers, force\n'
16+
b' @rms Remove stopped containers\n'
17+
b' @rmsf Remove stopped containers, force\n'
18+
b' (aliases: @rmfs)\n'
19+
b' @rma Remove all containers\n'
20+
b' @rmaf Remove all containers, force\n'
21+
b' (aliases: @rmfa)\n'
1422
b' options:\n'
1523
b' -f Force, for example ignores abstract contexts\n'
1624
b' -n Dry run, print docker command without running it\n'

0 commit comments

Comments
 (0)