This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-aliaser.py
executable file
·64 lines (44 loc) · 1.93 KB
/
docker-aliaser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import argparse
from os.path import expanduser
import os
import subprocess
def write_alias(alias_name, image_name, keep, ports=None):
if ports is not None:
ports = ports.split(",")
bash_profile = open(expanduser("~") + "/.bash_profile", "a")
alias_statement = "alias " + alias_name + "='docker run -it"
if not keep:
alias_statement = alias_statement + " --rm "
if ports is not None:
for port in ports:
alias_statement = alias_statement + " -p " + port + ":" + port
alias_statement = alias_statement + " -v \"$(PWD):/shared\" -w \"/shared\" " + image_name + "'\n"
bash_profile.write(alias_statement)
cmd = "source " + expanduser("~") + "/.bash_profile"
print ("Alias created. Please run \"" + cmd + "\" to update your current terminal with your new alias.")
def alias_exists(alias_name):
profile_text = ""
with open(expanduser("~") + "/.bash_profile", "r") as bash_profile:
for line in bash_profile:
profile_text = profile_text + line
text_to_check = "alias " + alias_name + "="
if text_to_check in profile_text:
return True
else:
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("alias_name", help="Name of created alias",
type=str)
parser.add_argument("image_name", help="Name of docker image",
type=str)
parser.add_argument('--ports', action="store",
dest="ports", help="Ports to forward in the docker image, separated by commas")
parser.add_argument("--keep", help="Keep image after running",
action="store_true")
args = parser.parse_args()
if alias_exists(args.alias_name):
print ("Alias already exists. Please choose another alias name.")
else:
write_alias(args.alias_name, args.image_name, args.keep, args.ports)