Repository navigation
Expand file tree
/
Copy pathshell-api-dock.sh
More file actions
271 lines (229 loc) · 7.16 KB
/
Copy pathshell-api-dock.sh
File metadata and controls
271 lines (229 loc) · 7.16 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
###############################################################################
#
# Copyright (c) 2026 Michel Mehl. All rights reserved.
#
# -----------------------------------------------------------------------------
#
# A shell API of wrapper functions for running docker commands
#
# -----------------------------------------------------------------------------
#
# Report bugs to michel.mehl@slashetc.fr
#
###############################################################################
__SHELL_API_DEV_DIR__=$(readlink -f $(dirname ${BASH_SOURCE[0]}))
source "${__SHELL_API_DEV_DIR__}/shell-api-core.sh"
if _loaded "${BASH_SOURCE[0]}" ; then
return 0
fi
:<<'EOF'
Fills the passed array with the names of actual running containers.
EOF
dock__ps() {
local -n __outContainerNames=$1
local cmd="docker ps -a| awk '{ if (NR > 1) printf(\"%s \", \$NF)}'"
_logf "DOCKER COMMAND: ${cmd}"
__outContainerNames=($(eval "$cmd"))
}
:<<'EOF'
Tells whether the passed container is running
EOF
dock__isRunning() {
local containerName="$1"
[ "$( ${__SUDO__}docker container inspect -f '{{.State.Status}}' "${containerName}" 2>/dev/null)" = "running" ]
}
:<<'EOF'
Opens a shell in the passed existing container
EOF
dock__openShell() {
local containerName="$1"
local cmd=""
cmd="${__SUDO__}docker"
cmd+=" exec -it '${containerName}' /bin/bash"
eval "$cmd"
}
:<<'EOF'
Executes a 'run' docker command
@param[1] image name
@param[2] container name
@param[3] a ref to a map giving option values (key=option name, value=option value).
Supported values:
'interactive' :(true/false) : if true, adds '--it' as command option,
'privileged' (true/false) : if true, adds '--privileged' as command option,
'lan' (true/false) : if true, adds '--net host' as command option, meaning container has access to the local LAN
@param[4] a ref to a map giving environment variable value (key=var name, value=value)
@param[5] a ref to a map giving mount mappings (key=src folder, value=destination)
EOF
dock__run() {
local containerImage="$1"
local containerName="$2"
local -n containerOpt=$3
local -n containerEnv=$4
local -n containerMounts=$5
local cmd=""
cmd="${__SUDO__}docker"
cmd+=" run --rm"
if [ "${containerOpt["interactive"]}" = true ] ; then
cmd+=" -it"
fi
if [ "${containerOpt["privileged"]}" = true ] ; then
cmd+=" --privileged"
fi
if [ "${containerOpt["lan"]}" = true ] ; then
cmd+=" --net host"
fi
local K=""
for K in "${!containerEnv[@]}" ; do
cmd+=" -e '${K}=${containerEnv["$K"]}'"
done
for K in "${!containerMounts[@]}" ; do
cmd+=" --mount type=bind,src='$K',dst='${containerMounts["$K"]}'"
done
#--user $(id -u):$(id -g) -w /home/ubuntu \
#-e TZ="Europe/Paris" \
#-p 3006:3006 -p 8080:8080
cmd+=" --name '${containerName}' '${containerImage}'"
_logf "DOCKER COMMAND: ${cmd}"
eval "$cmd"
}
:<<'EOF'
Executes a 'exec' docker command
@param[1] container name
@param[2] a ref to a map giving option values (key=option name, value=option value).
Supported values:
'interactive' :(true/false) : if true, adds '--it' as command option,
'privileged' (true/false) : if true, passes '--privileged' on as command option,
@param[n] command to execute inside container and arguments
EOF
dock__exec() {
local containerName="$1"
local -n containerOpt=$2
local cmd=""
cmd="${__SUDO__}docker"
cmd+=" exec"
if [ "${containerOpt["interactive"]}" = true ] ; then
cmd+=" -it"
fi
if [ "${containerOpt["privileged"]}" = true ] ; then
cmd+=" --privileged"
fi
cmd+=" '${containerName}'"
shift 2
cmd+=" $@"
_logf "DOCKER COMMAND: ${cmd}"
eval "$cmd"
}
:<<'EOF'
Copy a file into the docker container
@param[1] container name
@param[2] source file path
@param[3] destination file path
EOF
dock__copy() {
local containerName="$1"
local src="$2"
local dst="$3"
local cmd=""
cmd="${__SUDO__}docker"
cmd+=" cp"
cmd+=" '${src}'"
cmd+=" '${containerName}:${dst}'"
_log_vars cmd >&2
eval "$cmd"
}
:<<'EOF'
Stops a docker container
@param[1] container name
EOF
dock__stop() {
local containerName="$1"
local cmd=""
cmd="${__SUDO__}docker"
cmd+=" stop"
cmd+=" '${containerName}'"
_logf "DOCKER COMMAND: ${cmd}"
eval "$cmd"
}
:<<'EOF'
Commits a new image from the passed container
@param[1] container name
@param[1] image name
EOF
dock__commit() {
local containerName="$1"
local containerImageName="$2"
local cmd=""
cmd="${__SUDO__}docker"
cmd+=" commit"
cmd+=" '${containerName}'"
cmd+=" '${containerImageName}'"
_logf "DOCKER COMMAND: ${cmd}"
eval "$cmd"
}
:<<'EOF'
Starts or stop a container from a compose file
@param[1] in compose file name
@param[2] in compose env file or empty string for using implicit default (.env)
@param[3] in compose command : up, down, run, exec, "exec /bin/bash"...
@param[4] in compose service to handle or empty for all
@param[5] in bool telling whether to detach
EOF
dock__compose() {
local composeFile="$1"
local envFile="$2"
local composeCommand="$3"
local composeCommandOptions="$4"
local composeService="$5"
local ServiceCommand="$6"
if ! File__fileExists "${composeFile}" ; then
_log_err "File '${composeFile}' does not exist"
return 1
fi
local cmd=""
cmd="${__SUDO__}docker"
cmd+=" compose -f '${composeFile}'"
if ! Str__isEmpty "$envFile" ; then
cmd+=" --env-file '${envFile}'"
fi
cmd+=" ${composeCommand}"
if ! Str__isEmpty "$composeCommandOptions" ; then
cmd+=" ${composeCommandOptions}"
fi
cmd+=" --remove-orphans"
if ! Str__isEmpty "$composeService" ; then
cmd+=" ${composeService}"
fi
if ! Str__isEmpty "$ServiceCommand" ; then
cmd+=" ${ServiceCommand}"
fi
_logf "DOCKER COMMAND: ${cmd}"
eval "$cmd"
}
:<<'EOF'
This function does the same as dock__launch, except it builds all necessary arguments from a map already filled from a YAML file
and passes it on to dock__launch
@param[1] Ref to the map where are stored the YAML data as read by YAML__readAll.
When this latter is called via YAML__setFile, the global map YAML_data must be passed on as argument
@param[2] Parent node providing resp. image name and container name.
The values are then read from the following child node:
- 'image' : name of the image from which to create the container
- 'container' : name of the container that is created
@param[3] Parent node providing the options.
The values are then read from the following child node:
- 'privileged' (yes/no): name of the image from which to create the container
- 'net' : network (host means container gets access to LAN. See docker doc)
@param[3] a ref to a map giving option values (key=option name, value=option value).
Supported values:
'privileged' (true/false) : if true, passes '--privileged' on as command option,
'lan' (true/false) : if true, passes '--net host' on as command option, meaning container has access to the local LAN
@param[4] a ref to a map giving environment variable value (key=var name, value=value)
@param[5] a ref to a map giving mount mappings (key=src folder, value=destination)
dock__launchFromConfig() {
local containerImage="$1"
local containerName="$2"
local -n containerOpt=$3
local -n containerEnv=$4
local -n containerMounts=$5
}
EOF