-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdkr
executable file
·166 lines (143 loc) · 5.49 KB
/
dkr
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
set -e
if ! docker-machine ls | grep default
then
echo "No running docker-machine called 'default', please use 'dkr mac-new' to fix"
else
if ! docker-machine ls | grep default | grep Running &> /dev/null
then
docker-machine start default > /dev/null
fi
fi
eval "$(docker-machine env default)" || :
version=0.0.6
me=$(basename "$0")
if [[ $me == "_dkr" ]]
then
branch=dev
else
branch=master
fi
if [[ -f /usr/local/bin/.${me} ]]
then
sudo mv -f "/usr/local/bin/.${me}" "/usr/local/bin/${me}"
exec "/usr/local/bin/${me}" "$@"
fi
function usage() {
echo "dkr v${version}"
cat <<'EOF'
Usage: dkr [ <command> [<args>] ]
If no arguments are supplied a dkr> prompt is started and commands can be typed directly without the dkr prefix.
Commands are:
upd|update-dkr get the latest version of this script (curl must be installed)
cmp|co|compose <args> docker-compose <args>
mac|mc|machine <args> docker-machine <args>
swm|sw|swarm <args> docker-swarm <args>
cup <args> docker-compose up <args>
crn|crun docker-compose kill; docker-compose rm; \
docker-compose build && docker-compose up
it <args> docker run -t -i -P <args>
open <port> [<url>] opens a browser on macs at port and url on docker default host
cln|clean remove all images and containers
fcn|force-clean remove all images and containers forcefully
bash <args> docker run -t -i <args> /bin/bash
bld|make [<tag>] [<dir>] docker build -t <tag> <dir>
rn|drun docker run -t -i $@ this
do|build-run [<tag>] [<dir>] docker build -t <tag> <dir> && docker run -t -i this
ex|exe docker exec into the last container created
fix-mac restart docker-machine
tag Tags an image with multiple repositories and/or tags
Usage: dkr tag foo/bar {}:latest {}:0.1 quay.io/{}:0.1
Usage: dkr tag this foo/bar quay.io/{}:0.1
help this info
If not recognized the arguments are passed directly to the `docker` command.
EOF
}
function bishbashbosh() {
if (( $# == 0 ))
then
docker run -t -i ubuntu:14.04 /bin/bash
else
docker run -t -i "$@" /bin/bash
fi
}
function repl() {
while read -r -p "dkr> " i
do
command "${i}" || :
done
}
## tag foo/bar quay.io/{}:latest {}:latest quay.io/{}:0.1 {}:0.1
## tag this foo/bar quay.io/{}:latest {}:latest quay.io/{}:0.1 {}:0.1
function tag() {
if [[ "$1" = "this" ]]; then
for tag in "${@:3}"; do
docker tag "$1" "$(echo "$tag" | sed -e 's/{}/'"$2"'/g')"
done
else
for tag in "${@:2}"; do
docker tag "$1" "$(echo "$tag" | sed -e 's/{}/'"$1"'/g')"
done
fi
}
## bldrec . foo/bar
function build_recursive() {
find "$1" -name "Dockerfile" | while read -r f; do
docker build -t "$2-$(echo "$f" | cut -d "/" -f 2- | rev | cut -d "/" -f 2- | rev | sed -e 's/\//-/g')" "$(echo "$f" | rev | cut -d "/" -f 2- | rev)/"
done
}
function killall() {
if (( $# == 0 ))
then
containers="$(docker ps -q)" || :
else
containers="$(docker ps -q | grep "$1")" || :
fi
if [[ -n $containers ]]
then
docker kill "$containers"
else
echo "No containers match $1"
fi
}
function update() {
sudo curl -s -L https://raw.githubusercontent.com/sillelien/dkr/$branch/dkr -o "/usr/local/bin/.${me}"
sudo chmod 755 "/usr/local/bin/.${me}"
echo "Updated $me to version: "
exec "/usr/local/bin/${me}" version
}
function command() {
case "$1" in
cmp|co|compose) shift; docker-compose "$@" ;;
mac|mc|machine) shift; docker-machine "$@" ;;
swm|sw|swarm) shift; docker-swarm "$@" ;;
cup) shift; docker-compose up "$@" ;;
cmp-build|cbd|cbuild) shift; docker-compose build "$@" ;;
cmp-run|crn|crun) docker-compose kill; docker-compose rm; docker-compose build && docker-compose up ;;
it) shift; docker run -t -i "$@" ;;
opn|open) shift; open "http://$(docker-machine ip default):$1/$2" ;;
cln|clean) docker rmi "$(docker images -q)"; docker rmi "$(docker images -q -f dangling=true)"; docker rm "$(docker ps -q -a)" ; docker rmi "$(docker images -q)";;
fcn|force-clean) docker rmi -f "$(docker images -q)"; docker rmi -f "$(docker images -q -f dangling=true)"; docker rm -f "$(docker ps -q -a)" ; docker rmi -f "$(docker images -q)";;
bsh|bash) shift; bishbashbosh "$@" ;;
bld|make) shift; docker build -t "${1:-this}" "${2:-.}" ;;
rn|drun) shift; docker run -t -i "$@" this ;;
"do"|build-run) shift; docker build -t "${1:-this}" "${2:-.}" && docker run -t -i "${@:3}" this ;;
exe|ex|debug|dbg) shift; docker exec -it "$(docker ps -q | head -n 1)" bash ;;
tag) shift; tag "$@" ;;
kal|killall) shift; killall "$@" ;;
mac-fix) shift; docker-machine restart default ;;
mac-rm) shift; docker-machine rm default ;;
mac-new) shift; docker-machine create -d virtualbox default ;;
bldrec) shift; build_recursive "$@" ;;
ver|version) echo $version ;;
upd|update-dkr) update ;;
hlp|help|usage) usage ;;
*) docker "$@" ;;
esac
}
if (( $# == 0 ))
then
repl
else
command "$@"
fi