-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.docker-functions
123 lines (102 loc) · 3.77 KB
/
.docker-functions
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
###########################
# Docker Utilities
###########################
set_docker_config() {
mkdir ~/.docker
# set default `ps` format to be much shorter
touch ~/.docker/config.json
# the default ps format is way too large. Shorten it up and then
yq -i '.psFormat = "table {{.ID}}\t{{.Image}}\t{{.Names}}"' ~/.docker/config.json
# set default log size to be a reasonable, but not unbounded, size
touch ~/.docker/daemon.json
yq -i '.log-driver = "json-file" | .log-opts.max-size = "10m" | .log-opts.max-file = "3"' ~/.docker/daemon.json
# https://docs.orbstack.dev/features/debug
# `docker context ls` to get a list of contexts
# TODO maybe only do this on macos environment?
for host in $DOCKER_HOSTS; do
docker context create $host --docker host=ssh://$host@$host.lan
done
}
# from within a container, scan to see what it has access to
docker-container-scan() {
# 1. Identify your container's network and subnet:
ip addr show
# 2. Scan for active IPs:
nmap -sn 172.17.0.0/16 -oG scan.txt
# 3. For each detected IP, do a reverse lookup:
for ip in $(awk '/Up$/{print $2}' scan.txt); do
dig -x $ip +short
done
}
# Usage: docker-push-image <image> <host>
docker-push-image() {
docker save $1 | ssh -C $2 docker load
}
# docker shell: open up an interactive shell in a container, across all of your docker contexts
dsh() {
# Turn off any shell debugging/tracing (uncomment if still seeing debug output)
# set +x
# unsetopt xtrace
local list=""
local contexts
# 1) Collect all Docker context names quietly
contexts="$(docker context ls --format '{{.Name}}' 2>/dev/null)"
if [ -z "$contexts" ]; then
echo "No Docker contexts found."
return 1
fi
# 2) For each context, gather containers and append them to "list"
while IFS= read -r ctx; do
# Use a process-substitution with ps output, reading ID & Name directly
while read -r cid cname; do
# If the line was empty or something was off, skip
[ -z "$cid" ] && continue
list+="${ctx} ${cid} ${cname}"$'\n'
done < <(docker --context "$ctx" ps --format '{{.ID}} {{.Names}}' 2>/dev/null)
done <<<"$contexts"
# 3) If no containers found, exit
if [ -z "$list" ]; then
echo "No running containers across any contexts."
return 1
fi
# 4) Send the container list into fzf (prompt at top with --layout=reverse)
local selected
selected="$(
printf "%s" "$list" | fzf \
--height=40% \
--layout=reverse \
--no-preview \
--header="ctrl-o: orb debug | ctrl-l: logs | ctrl-i: inspect | ctrl-e: exec" \
--prompt="Select Docker container: " \
--bind="ctrl-o:execute(orb debug -c {1} {2})" \
--bind="ctrl-l:execute(docker --context {1} logs -f {2} | ov)" \
--bind="ctrl-i:execute(docker --context {1} inspect {2} | ov)" \
--bind="ctrl-e:execute(docker --context {1} exec -it {2} bash -l)"
)"
# 5) If user cancels or no result, exit
if [ -z "$selected" ]; then
echo "No container selected."
return 1
fi
# 6) Extract the chosen context and container ID from the selected line
local chosen_context chosen_id
chosen_context="$(awk '{print $1}' <<<"$selected")"
chosen_id="$(awk '{print $2}' <<<"$selected")"
echo "Chosen context: $chosen_context"
echo "Chosen container ID: $chosen_id"
echo "docker --context $chosen_context $chosen_id"
}
# get files that would be included in a Dockerfile, with size in megabytes
dockerignore-test() {
rsync --dry-run -av --exclude-from='.dockerignore' --out-format="%l %n" ./ /tmp/ |
awk '{size_mb = $1 / (1024 * 1024); printf "%.2f MB %s\n", size_mb, $2}' |
sort -k1,1nr -k2,2 |
less
}
# TODO docker-new-bash, run instead of exec
# add key to remove server
# ssh-add-key <server>
ssh-add-key() {
ssh-copy-id -i ~/.ssh/id_rsa.pub $1
ssh $1
}