-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.sh
executable file
·212 lines (169 loc) · 4.79 KB
/
docker.sh
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
function main {
# loop args
if [[ $# -ne 0 ]] ; then
for var in "$@" ; do
eval $var
done
exit 1
fi
# menu
while true; do
read -n 1 -p "
server tools
===================
1) Docker Base - Arch
2) Docker Base - Debian/Arbmain
3) Docker Remove All
4) route port to 80
5) docker pipe
b) backup docker folder
u) user permissions
*) Any key to exit
:" ans;
reset
case $ans in
1) fn_docker_base_arch ;;
2) fn_docker_base_debian ;;
4) fn_remove_all ;;
4) fn_nftables ;;
5) fn_dockerpipe ;;
b) fn_backup ;;
u) fn_user_permissions ;;
*) $SHELL ;;
esac
done
}
function fn_user_permissions {
sudo usermod -aG docker $USER
newgrp docker # login to group, save reboot!
}
function fn_backup {
echo "backup docker folder..."
hostname=$(hostname)
archive=$HOME/Backups/docker-${hostname}.tar.gz
backup=$HOME/Docker
echo "listing containers"
containers=$(docker container list -qa)
echo $containers
mkdir $HOME/Backups
echo "stop containers"
sudo docker container stop ${containers}
echo "create backup..."
echo ${archive}
sudo tar -czvf ${archive} ${backup} > /dev/null
echo "restart containers"
sudo docker restart ${containers}
echo "done!"
}
function fn_nftables {
echo "Enter port to forward to 80: "
read port_forward
sudo systemctl --now enable nftables
sudo tee /etc/nftables.conf > /dev/null << EOL
#!/usr/bin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0;
}
chain forward {
type filter hook forward priority 0;
}
chain output {
type filter hook output priority 0;
}
}
table ip nat {
chain prerouting {
type nat hook prerouting priority 0; policy accept;
tcp dport 80 redirect to ${port_forward}
}
chain postrouting {
type nat hook postrouting priority 0; policy accept;
}
}
EOL
sudo nft -f /etc/nftables.conf
#sudo systemctl restart nftables
sudo nft list ruleset
}
function fn_dockerpipe {
# pipe
mkdir $HOME/Docker/pipe
mkfifo $HOME/Docker/pipe/pipe_in
mkfifo $HOME/Docker/pipe/pipe_out
# create script
sudo tee $HOME/Docker/pipe/start_pipe.sh > /dev/null << EOL
#!/bin/bash
while true; do eval "\$(cat pipe_in)" > pipe_out; done
EOL
sudo tee $HOME/Docker/pipe/run.sh > /dev/null << EOL
#!/bin/bash
echo "\$@" > /pipe/pipe_in
cat /pipe/pipe_out
EOL
sudo chmod +x $HOME/Docker/pipe/start_pipe.sh
sudo chmod +x $HOME/Docker/pipe/run.sh
# create service
sudo tee /etc/systemd/system/pipe.service > /dev/null << EOL
[Unit]
Description=docker pipe
After=sound.target
[Service]
ExecStart=$HOME/Docker/pipe/start_pipe.sh
WorkingDirectory=$HOME/Docker/pipe/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=$USER
Environment="PULSE_RUNTIME_PATH=/run/user/1000/pulse/"
[Install]
WantedBy=default.target
EOL
sudo systemctl reset-failed pipe
sudo systemctl enable pipe
sudo systemctl start pipe
systemctl status pipe.service
}
function fn_docker_base_debian {
# https://docs.docker.com/engine/install/debian/
# https://download.docker.com/linux/debian/dists/
VERSION="bookworm"
echo "install docker for debian: $VERSION"
# remove old
./util.sh -u docker.io docker-doc docker-compose podman-docker containerd runc
sudo apt autoremove -y
# Add Docker's official GPG key:
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
# install
./util.sh -i docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# add user
sudo usermod -aG docker ${USER}
echo "reboot required!"
}
function fn_docker_base_arch {
./util.sh -i docker docker-compose
sudo systemctl enable docker
sudo systemctl start docker
# add user
sudo usermod -aG docker {$USER}
echo "reboot required!"
}
function fn_remove_all {
sudo docker container stop $(sudo docker container ls -aq)
sudo docker container prune -f
sudo docker ps
}
# pass all args
main "$@"