-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
fdocker
CLI to help manage Docker daemon (#106)
Co-authored-by: Steven Briscoe <me@stevenbriscoe.com> Co-authored-by: Luke Childs <lukechilds123@gmail.com>
- Loading branch information
1 parent
a741734
commit 178ee29
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
DOCKER_ROOT="/var/lib/docker" | ||
|
||
if [[ "$EUID" -ne 0 ]]; then | ||
echo "Please run as root" | ||
exit | ||
fi | ||
|
||
show_help() { | ||
cat << EOF | ||
fdocker 0.0.1 | ||
CLI for managing Docker | ||
Usage: docker <command> <arguments...> | ||
Commands: | ||
clean Cleanup containers using 'containers' as argument | ||
EOF | ||
} | ||
|
||
if [ -z ${2+x} ]; then | ||
command="" | ||
else | ||
command="${1}" | ||
fi | ||
|
||
function delete_mount() { | ||
id_path="${1}" | ||
|
||
overlay_path="${DOCKER_ROOT}/overlay2" | ||
|
||
if [[ -f "${id_path}" ]]; then | ||
overlay_id=$(cat "${id_path}") | ||
|
||
overlay_path="${overlay_path}/${overlay_id}" | ||
|
||
if [[ -d "${overlay_path}" ]]; then | ||
echo "Deleting: ${overlay_path}" | ||
|
||
rm -rf "${overlay_path}" | ||
fi | ||
|
||
rm -rf "${id_path}" | ||
fi | ||
} | ||
|
||
function cleanup_containers() { | ||
echo "Cleaning up containers..." | ||
|
||
containers_path="${DOCKER_ROOT}/containers" | ||
mounts_path="${DOCKER_ROOT}/image/overlay2/layerdb/mounts" | ||
|
||
# Loop through all the containers | ||
containers="${containers_path}/*" | ||
for dir in $containers; do | ||
container_id=$(basename "${dir}") | ||
|
||
if [[ ! -d "${dir}" ]]; then | ||
continue | ||
fi | ||
|
||
echo "Removing container: ${container_id}" | ||
|
||
# Determine the overlay IDs from the mounts | ||
delete_mount "${mounts_path}/${container_id}/init-id" | ||
delete_mount "${mounts_path}/${container_id}/mount-id" | ||
|
||
# Delete the container folder | ||
rm -rf "${dir}" | ||
|
||
echo | ||
done | ||
|
||
echo "Done" | ||
} | ||
|
||
if [[ "${command}" == "clean" ]]; then | ||
resource="${2}" | ||
|
||
if [[ "${resource}" == "containers" ]]; then | ||
cleanup_containers | ||
else | ||
echo "Cannot cleanup: ${resource}" | ||
|
||
exit 1 | ||
fi | ||
|
||
exit | ||
fi | ||
|
||
# If we get here it means no valid command was supplied | ||
# Show help and exit | ||
show_help | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Umbrel Docker Cleanup | ||
# Installed at /etc/systemd/system/umbrel-docker-cleanup.service | ||
|
||
[Unit] | ||
Description=Umbrel Docker Cleanup | ||
After=umbrel-external-storage.service | ||
Before=docker.service | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/home/umbrel/umbrel/scripts/umbrel-os/fdocker clean containers | ||
User=root | ||
Group=root | ||
StandardOutput=syslog | ||
StandardError=syslog | ||
SyslogIdentifier=umbrel docker cleanup | ||
|
||
[Install] | ||
WantedBy=multi-user.target |