This commit is contained in:
Executable
+65
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env bash
|
||||
# Review before running: sudo bash scripts/install-native-docker.sh --install
|
||||
# Does not remove Snap, its image/volume storage, or lab7 bind data.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
root="$PWD"
|
||||
case "${1:---check}" in
|
||||
--check) exec scripts/check-docker-hardening.sh ;;
|
||||
--install) ;;
|
||||
*) echo 'Usage: install-native-docker.sh [--check|--install]' >&2; exit 2 ;;
|
||||
esac
|
||||
[[ "$EUID" -eq 0 ]] || { echo 'Run from your terminal: sudo bash scripts/install-native-docker.sh --install' >&2; exit 1; }
|
||||
command -v apt-get >/dev/null
|
||||
command -v snap >/dev/null
|
||||
command -v systemctl >/dev/null
|
||||
# Global daemon switching is permitted only while no existing container is running.
|
||||
snap_cli=/snap/docker/current/bin/docker
|
||||
if systemctl is-active --quiet docker.service && ! systemctl is-active --quiet snap.docker.dockerd.service; then
|
||||
operator="${SUDO_USER:?Invoke via sudo as the repository owner}"
|
||||
exec env DOCKER_BIN=/usr/bin/docker PROBE_UID="$(id -u "$operator")" PROBE_GID="$(id -g "$operator")" scripts/check-docker-hardening.sh
|
||||
fi
|
||||
[[ -x "$snap_cli" ]]
|
||||
[[ -z "$("$snap_cli" ps -q)" ]] || { echo 'Stop existing containers safely before switching Docker daemons. Nothing changed.' >&2; exit 1; }
|
||||
mkdir -p build/docker-migration
|
||||
chmod 700 build/docker-migration
|
||||
# Export only the images used by this repository; no volume or secrets export.
|
||||
mapfile -t images < <(scripts/dc.sh --profile operator config --images | sort -u)
|
||||
[[ "${#images[@]}" -gt 0 ]]
|
||||
for image in "${images[@]}"; do "$snap_cli" image inspect "$image" >/dev/null; done
|
||||
"$snap_cli" image save -o "$root/build/docker-migration/lab7-images.tar" "${images[@]}"
|
||||
chmod 600 build/docker-migration/lab7-images.tar
|
||||
[[ -z "$("$snap_cli" ps -q)" ]] || { echo 'A container started during image export; aborting without stopping it.' >&2; exit 1; }
|
||||
restore_snap() {
|
||||
echo 'Native Docker installation failed; restoring the Snap daemon without deleting either storage.' >&2
|
||||
systemctl disable --now docker.service docker.socket 2>/dev/null || true
|
||||
snap enable docker >/dev/null 2>&1 || true
|
||||
snap start docker >/dev/null 2>&1 || true
|
||||
}
|
||||
trap restore_snap ERR
|
||||
# Reversible: Snap package and /var/snap/docker data are preserved.
|
||||
snap stop docker
|
||||
snap disable docker
|
||||
apt-get update
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io docker-compose-v2
|
||||
systemctl enable --now docker.service
|
||||
/usr/bin/docker image load -i "$root/build/docker-migration/lab7-images.tar"
|
||||
# Materialize platform-specific public image blobs after a cross-version import.
|
||||
scripts/dc.sh --profile operator pull --ignore-buildable
|
||||
# Use the operator UID for the actual non-root probe, not root's UID.
|
||||
operator="${SUDO_USER:?Invoke via sudo as the repository owner}"
|
||||
uid=$(id -u "$operator")
|
||||
gid=$(id -g "$operator")
|
||||
/usr/bin/docker run --rm --network none --user "$uid:$gid" --read-only --cap-drop ALL \
|
||||
--security-opt no-new-privileges:true --entrypoint /bin/sh postgres:16.15-alpine -ec '
|
||||
test "$(id -u)" -ne 0
|
||||
grep -Eq "^NoNewPrivs:[[:space:]]+1$" /proc/self/status
|
||||
grep -Eq "^CapEff:[[:space:]]+0+$" /proc/self/status
|
||||
echo "PASS: native Docker starts non-root with NoNewPrivs=1 and no capabilities"
|
||||
'
|
||||
trap - ERR
|
||||
if ! id -nG "$operator" | tr ' ' '\n' | grep -qx docker; then
|
||||
usermod -aG docker "$operator"
|
||||
echo 'Docker group added. Start a new login session before non-sudo Docker commands.'
|
||||
fi
|
||||
printf 'Native Docker ready. Snap storage and all lab7 data preserved.\n'
|
||||
Reference in New Issue
Block a user