Skip to content

Instantly share code, notes, and snippets.

@Jules-Baratoux
Created May 24, 2019 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jules-Baratoux/1709406aedb30a8ffc4700c69d8e357c to your computer and use it in GitHub Desktop.
Save Jules-Baratoux/1709406aedb30a8ffc4700c69d8e357c to your computer and use it in GitHub Desktop.
docker run based on docker service commands
#!/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