Skip to content

Commit f2f1c0f

Browse files
committed
dockerd-rootless-setuptool.sh: create CLI context "rootless"
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 2ae2ddf commit f2f1c0f

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

contrib/dockerd-rootless-setuptool.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ERROR() {
2323
# constants
2424
DOCKERD_ROOTLESS_SH="dockerd-rootless.sh"
2525
SYSTEMD_UNIT="docker.service"
26+
CLI_CONTEXT="rootless"
2627

2728
# CLI opt: --force
2829
OPT_FORCE=""
@@ -350,6 +351,23 @@ install_nonsystemd() {
350351
echo
351352
}
352353

354+
cli_ctx_exists() {
355+
name="$1"
356+
"${BIN}/docker" context inspect -f "{{.Name}}" "${name}" > /dev/null 2>&1
357+
}
358+
359+
cli_ctx_create() {
360+
name="$1"
361+
host="$2"
362+
description="$3"
363+
"${BIN}/docker" context create "${name}" --docker "host=${host}" --description "${description}" > /dev/null
364+
}
365+
366+
cli_ctx_rm() {
367+
name="$1"
368+
"${BIN}/docker" context rm -f "${name}" > /dev/null
369+
}
370+
353371
# CLI subcommand: "install"
354372
cmd_entrypoint_install() {
355373
# requirements are already checked in init()
@@ -359,6 +377,14 @@ cmd_entrypoint_install() {
359377
install_systemd
360378
fi
361379

380+
if cli_ctx_exists "${CLI_CONTEXT}"; then
381+
INFO "CLI context \"${CLI_CONTEXT}\" already exists"
382+
else
383+
INFO "Creating CLI context \"${CLI_CONTEXT}\""
384+
cli_ctx_create "${CLI_CONTEXT}" "unix://${XDG_RUNTIME_DIR}/docker.sock" "Rootless mode"
385+
fi
386+
387+
echo
362388
INFO "Make sure the following environment variables are set (or add them to ~/.bashrc):"
363389
echo
364390
if [ -n "$XDG_RUNTIME_DIR_CREATED" ]; then
@@ -390,6 +416,11 @@ cmd_entrypoint_uninstall() {
390416
INFO "Uninstalled ${SYSTEMD_UNIT}"
391417
fi
392418

419+
if cli_ctx_exists "${CLI_CONTEXT}"; then
420+
cli_ctx_rm "${CLI_CONTEXT}"
421+
INFO "Deleted CLI context \"${CLI_CONTEXT}\""
422+
fi
423+
393424
INFO "This uninstallation tool does NOT remove Docker binaries and data."
394425
INFO "To remove data, run: \`$BIN/rootlesskit rm -rf $HOME/.local/share/docker\`"
395426
}

0 commit comments

Comments
 (0)