Skip to content

Instantly share code, notes, and snippets.

@ankitrgadiya
Last active July 31, 2018 08:11
Show Gist options
  • Save ankitrgadiya/685d6496c4d6ce7fa31bdba8c34e0a1e to your computer and use it in GitHub Desktop.
Save ankitrgadiya/685d6496c4d6ce7fa31bdba8c34e0a1e to your computer and use it in GitHub Desktop.
Doch - Docker Helper

Doch

It is a dirty script which basically helps me run and stop pre-defined containers on Euclid. It just uses subprocess in Python to run the commands with the options pre-defined in CONTAINERS dictionary. This script is actually port of docker-func script which I wrote in Bash few months ago for the same thing. The point of this is so that I don't have to remember and type all the options that need to be specified for those containers.

Functions:

names()

names function takes no arguments can returns a list of names of all the running containers. It gets the names from the docker ps command and no third party module is involved in it.

start_cont()

start_cont function takes a string with name of a predefined container as argument and it runs that container using all the predefined options specified in the CONTAINERS list.

stop_cont()

stop_cont function takes a string with name of a predefined container as argument and it stops the container with -t 0 option. It do not remove the container though.

rem_cont()

rem_cont function takes a string with name of a predefined container as argument and it removes the container.

res_cont()

res_cont function takes a string with name of a predefined container as argument and then calls stop_cont, rem_cont and start_cont in that order with name as argument, effectively restarting the container.

Command Line

doch function when called without any arguments just returns a help message which lists, sub commands and predefined containers.

Each sub command can be called with any number of arguments and it calls the corresponding function in a loop each time with new argument until it exhausts the list of all arguments.

#!/usr/bin/python3
"""
Docker Helper Function
Written by Ankit R Gadiya
"""
from sys import argv
import subprocess
CONTAINERS = {
"nginx" : """
docker run \
--name nginx \
-v /home/web/sites:/www \
-v /home/web/conf:/etc/nginx/conf.d \
-e VIRTUAL_HOST=*.euclid.argp.in,euclid.argp.in,euclid.local,*.euclid.s,euclid.s,ankit.arg,wiki.arg,docs.arg,argp.arg,mirrors.arg,mirrors.argp.in \
-d \
-m 100mb \
--restart always \
nginx:latest
""",
"ikiwiki" : """
docker run \
--name ikiwiki \
-v /home/web/sites/ikiwiki:/wiki \
-e VIRTUAL_HOST=ikiwiki.arg \
-d \
-m 100mb \
--restart always \
ikiwiki:latest
""",
"proxy" : """
docker run \
--name proxy \
-e DEFAULT_HOST=euclid.local \
-v /var/run/docker.sock:/tmp/docker.sock:ro \
-v /home/elliot/.passwd:/etc/nginx/htpasswd \
-v /home/elliot/.certificates/docker:/etc/nginx/certs \
-p 80:80 \
-p 443:443 \
-d \
-m 100mb \
--restart always \
proxy:latest
""",
"git" : """
docker run \
--name git \
-e VIRTUAL_HOST=euclid.git,git.euclid.argp.in \
-v /home/git:/git \
-v /home/git/cgitrc:/etc/cgitrc \
-d \
-m 100mb \
--restart always \
git:latest
""",
"nnpy" : """
docker run \
--name nnpy \
-e VIRTUAL_HOST=nn.euclid.s,nn.euclid.argp.in \
-v /home/web/db/nnpy/nnpy.db:/app/nnpy.db \
-d \
-m 100mb \
--restart always \
nnpy:latest
"""
}
def names():
running_containers = []
output = subprocess.run(
["docker", "ps", "--format", "{{.Names}}"],
stdout=subprocess.PIPE).stdout.decode('utf-8')
for i in CONTAINERS.keys():
if i in output:
running_containers.append(i)
return running_containers
def start_cont(name):
assert type(name) is str, "Error: Invalid Name"
assert name in CONTAINERS.keys(), "Error: Invalid container!"
assert name not in names(), "Error: " + name + " is already running!"
subprocess.run(CONTAINERS[name].split())
def stop_cont(name):
assert type(name) is str, "Error: Invalid Name"
assert name in CONTAINERS.keys(), "Error: Invalid container!"
assert name in names(), "Error: " + name + " is not running!"
subprocess.run(["docker", "stop", name, "-t", "0"])
def rem_cont(name):
assert type(name) is str, "Error: Invalid Name"
assert name in CONTAINERS.keys(), "Error: Invalid container!"
assert name not in names(), "Error: " + name + " is running!"
subprocess.run(["docker", "rm", name])
def res_cont(name):
assert type(name) is str, "Error: Invalid Name"
assert name in CONTAINERS.keys(), "Error: Invalid container!"
assert name in names(), "Error: " + name + " is not running!"
stop_cont(name)
rem_cont(name)
start_cont(name)
def help():
print("""Usage: doch <command> [<args>]
\r
\rCommands:
\r- start
\r- stop
\r- remove
\r- restart
\r- help
\r
\rContainers:""")
for i in sorted(CONTAINERS.keys()):
print("-", i)
def main():
if len(argv) == 1:
help()
else:
if argv[1] == "start":
for i in argv[2:]:
try:
start_cont(i)
except AssertionError as e:
print(e)
elif argv[1] == "stop":
for i in argv[2:]:
try:
stop_cont(i)
except AssertionError as e:
print(e)
elif argv[1] == "remove":
for i in argv[2:]:
try:
rem_cont(i)
except AssertionError as e:
print(e)
elif argv[1] == "restart":
for i in argv[2:]:
try:
res_cont(i)
except AssertionError as e:
print(e)
elif argv[1] == "help":
help()
else:
print("Error: Invalid Command!")
help()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment