Last active
November 14, 2017 11:24
-
-
Save moqmar/2cb257c05dc44aff4e54fe9851062792 to your computer and use it in GitHub Desktop.
Script for simple management and updating of docker containers. Like docker-compose, but more flexible and with single-command upgrade functionality.
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/sh | |
# ./Dockerscript <prefix> | |
name=helloworld | |
[ -n "$PREFIX" ] && prefix=-$PREFIX | |
set -e | |
start() { | |
docker network create --driver bridge $name$prefix | |
docker run -id --restart=always --network=$name$prefix -v "$PWD/files:/data" --name=$name$prefix-main "$@" hello-world | |
} | |
stop() { | |
for s in main; do | |
docker stop $name$prefix-$s && | |
docker rm $name$prefix-$s || true | |
done | |
docker network rm $name$prefix || true | |
} | |
build() { | |
docker build --pull --no-cache -t $name . | |
} | |
[ ! -f /usr/share/Dockerscript ] && curl -so /usr/share/Dockerscript https://gist.githubusercontent.com/moqmar/2cb257c05dc44aff4e54fe9851062792/raw/Dockerscript.source | |
. /usr/share/Dockerscript |
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/sh | |
# Script for simple management and updating of docker containers. | |
# See https://gist.githubusercontent.com/moqmar/2cb257c05dc44aff4e54fe9851062792 for more information. | |
[ -z "$name" ] && echo "This script needs to be sourced from a Dockerscript file." | |
status() { | |
result="$(docker container ls --all --format '{{ .Names }} {{ .Status }}' | grep "^$name$prefix-")" | |
if [ -z "$result" ]; then | |
echo "Not running, containers don't exist." | |
exit 2 | |
elif [ -z "$(echo $result | grep -E '^\S+ Up')" ]; then | |
echo "Not running, containers exist:" | |
echo "$result" | |
exit 1 | |
else | |
echo "Running:" | |
echo "$result" | |
exit 0 | |
fi | |
} | |
if [ "$1" = "start" ]; then | |
shift | |
start | |
elif [ "$1" = "stop" ]; then | |
shift | |
stop | |
elif [ "$1" = "restart" ]; then | |
shift | |
stop | |
start | |
elif [ "$1" = "status" ]; then | |
status | |
elif [ "$1" = "update" ]; then | |
shift | |
build | |
stop | |
start | |
else | |
echo "./Dockerfile start|stop|restart|status|update" | |
fi |
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
[Unit] | |
Description=Dockerscript for ... | |
Wants=docker.service | |
[Service] | |
Type=forking | |
ExecStart=/.../Dockerscript start | |
ExecStop=/.../Dockerscript stop | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment