Created
May 24, 2019 19:14
-
-
Save Jules-Baratoux/1709406aedb30a8ffc4700c69d8e357c to your computer and use it in GitHub Desktop.
docker run based on docker service commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
# Usage: | |
# docker-service-run --tty alpine ls | |
# | |
function docker-service-wait # SERVICE | |
{ | |
local interval=0.5s | |
while true | |
do | |
state=$(docker inspect --format '{{.Status.State}}' $(docker service ps "$1" --format "{{.ID}}")) | |
if [[ "$state" = "complete" ]] | |
then | |
break | |
fi | |
sleep $interval | |
done | |
} | |
function docker-service-run # [OPTIONS] IMAGE [COMMAND] [ARG...] | |
{ | |
local service=$(docker service create --detach --restart-condition none "$@") | |
trap "close $service" SIGINT SIGTERM EXIT | |
docker service logs --follow --no-resolve --raw --no-trunc "$service" & | |
docker-service-wait "$service" | |
} | |
function close # SERVICE | |
{ | |
kill $(jobs -p) | |
docker service rm "$1" > /dev/null | |
} | |
docker-service-run "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment