Skip to content

Instantly share code, notes, and snippets.

@samyranavela
Created November 9, 2016 13:47
Show Gist options
  • Save samyranavela/dcfb0afae6a7d574dc5ec6bbce0d0aac to your computer and use it in GitHub Desktop.
Save samyranavela/dcfb0afae6a7d574dc5ec6bbce0d0aac to your computer and use it in GitHub Desktop.
Start a traefik container & connect to all network
#!/bin/sh
#set -ex
# The daemon's name (to ensure uniqueness and for stop, restart and status)
name="traefik"
# The path of the client executable
command="$HOME/traefik/traefik.sh"
# Any command line arguments for the client executable
command_args=""
# The path of the daemon executable
daemon=$(which docker)
# Gets traifik container Id
container_id=$(docker ps -aq -f NAME=$name 2> /dev/null)
# Checks if the traefik container is running.
running=$(docker inspect --format="{{ .State.Running }}" $name 2> /dev/null)
case "$1" in
start)
# This if statement isn't strictly necessary but it's user friendly
if docker ps -aq -f NAME=$name > /dev/null
then
docker start $name
else
if $running
then
echo "$name container is already running."
else
echo -n "Starting $name container..."
docker run --name=traefik -d -p 8080:8080 -p 80:80 -v $HOME/traefik/traefik.toml:/etc/traefik/traefik.toml -v /var/run/docker.sock:/var/run/docker.sock traefik
# Adds the traefik container to all network.
for network in $(docker network ls -q); do
docker network connect $network $name
done
echo done.
fi
fi
;;
stop)
# This if statement isn't strictly necessary but it's user friendly
if docker ps -aq -f NAME=$name > /dev/null
then
if $running
then
echo "Stopping $name container..."
docker stop traefik > /dev/null;
echo done.
else
echo "$name container is not running."
fi
else
echo "$name container do not exists. Start it first."
exit 1
fi
;;
restart|reload)
# This if statement isn't strictly necessary but it's user friendly
if docker ps -aq -f NAME=$name > /dev/null
then
if $running
then
echo -n "Restarting $name container..."
docker restart traefik;
echo done.
else
echo "$name container is not running."
exit 1
fi
else
echo "$name container do not exists. Start it first."
exit 1
fi
;;
status)
# This if statement isn't strictly necessary but it's user friendly
if docker ps -aq -f NAME=$name > /dev/null
then
if $running
then
echo "$name container is up & running.";
else
echo "$name container is not running."
fi
else
echo "$name container do not exists."
exit 1
fi
;;
clean)
# This if statement isn't strictly necessary but it's user friendly
if docker ps -aq -f NAME=$name > /dev/null
then
docker rm -f $container_id
else
echo "$name container do not exists."
exit 1
fi
;;
rebuild)
# This if statement isn't strictly necessary but it's user friendly
if docker ps -aq -f NAME=$name > /dev/null
then
docker rm -f $container_id
docker run --name=traefik -d -p 8080:8080 -p 80:80 -v $HOME/traefik/traefik.toml:/etc/traefik/traefik.toml -v /var/run/docker.sock:/var/run/docker.sock traefik
else
echo "$name container do not exists."
exit 1
fi
;;
*)
echo "usage: $0 <start|stop|restart|reload|status|clean|rebuild>" >&2
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment