Skip to content

Commit 63c2ebf

Browse files
authored
Fixed incorrect containerd sock path on OpenSUSE (#30)
- Fixed incorrect containerd sock path on OpenSUSE - Use no_proxy environment variable instead of complex proxy save/restore logic - Replace all sudo calls with sudo -n to avoid password prompt blocking - Ensures script never blocks waiting for password input - Add CONTAINERD_SOCKET environment variable support for user override - Skip detection if CONTAINERD_SOCKET was explicitly set by user - Default to /run/containerd/containerd.sock if no socket found - Added snap docker support
1 parent da2720e commit 63c2ebf

File tree

1 file changed

+74
-9
lines changed

1 file changed

+74
-9
lines changed

docker-pussh

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ fi
88
# Script version
99
VERSION="0.1.3"
1010

11+
# Ensure localhost connections bypass proxy
12+
export no_proxy="${no_proxy:-},localhost,127.0.0.1"
13+
1114
# Return metadata expected by the Docker CLI plugin framework: https://github.com/docker/cli/pull/1564
1215
if [ "${1:-}" = "docker-cli-plugin-metadata" ]; then
1316
cat <<EOF
@@ -111,12 +114,12 @@ ssh_remote() {
111114
SSH_ARGS+=("${target}")
112115
}
113116

114-
# sudo prefix for remote docker commands. It's set to "sudo" if the remote user is not root and requires sudo
117+
# sudo prefix for remote docker commands. It's set to "sudo -n" if the remote user is not root and requires sudo
115118
# to run docker commands.
116119
REMOTE_SUDO=""
117120

118121
# Check if the remote host has Docker installed and if we can run docker commands.
119-
# If sudo is required, it sets the REMOTE_SUDO variable to "sudo".
122+
# If sudo is required, it sets the REMOTE_SUDO variable to "sudo -n".
120123
check_remote_docker() {
121124
# Check if docker command is available.
122125
if ! ssh "${SSH_ARGS[@]}" "command -v docker" >/dev/null 2>&1; then
@@ -125,8 +128,8 @@ check_remote_docker() {
125128
# Check if we need sudo to run docker commands.
126129
if ! ssh "${SSH_ARGS[@]}" "docker version" >/dev/null 2>&1; then
127130
# Check if we're not root and if sudo docker works.
128-
if ssh "${SSH_ARGS[@]}" "[ \$(id -u) -ne 0 ] && sudo docker version" >/dev/null; then
129-
REMOTE_SUDO="sudo"
131+
if ssh "${SSH_ARGS[@]}" "[ \$(id -u) -ne 0 ] && sudo -n docker version" >/dev/null; then
132+
REMOTE_SUDO="sudo -n"
130133
else
131134
error "Failed to run docker commands on remote host. Please ensure:
132135
- Docker is installed and running on the remote host
@@ -145,12 +148,48 @@ random_port() {
145148
UNREGISTRY_CONTAINER=""
146149
# Unregistry port on the remote host that is bound to localhost. It's populated by run_unregistry function.
147150
UNREGISTRY_PORT=""
151+
# Containerd socket path on remote host. It's populated by find_containerd_socket function.
152+
# Can be overridden by setting CONTAINERD_SOCKET environment variable.
153+
CONTAINERD_SOCKET=${CONTAINERD_SOCKET:-/run/containerd/containerd.sock}
154+
155+
# Find the containerd socket path on the remote host
156+
# If no socket is found, keeps the default value to avoid regression
157+
find_containerd_socket() {
158+
# Skip detection if CONTAINERD_SOCKET was explicitly set by user
159+
if [ "$CONTAINERD_SOCKET" != "/run/containerd/containerd.sock" ]; then
160+
return 0
161+
fi
162+
163+
local socket_paths=(
164+
"/var/run/docker/containerd/containerd.sock"
165+
"/run/containerd/containerd.sock"
166+
"/var/run/containerd/containerd.sock"
167+
"/run/docker/containerd/containerd.sock"
168+
"/run/snap.docker/containerd/containerd.sock"
169+
)
170+
171+
for socket_path in "${socket_paths[@]}"; do
172+
# Try without sudo first, then with sudo if REMOTE_SUDO is set
173+
# shellcheck disable=SC2029
174+
if ssh "${SSH_ARGS[@]}" "test -S '$socket_path'" 2>/dev/null ||
175+
ssh "${SSH_ARGS[@]}" "sudo -n test -S '$socket_path'" 2>/dev/null; then
176+
CONTAINERD_SOCKET="$socket_path"
177+
return 0
178+
fi
179+
done
180+
181+
# If no socket found, keep the default and let the container startup handle the error
182+
# This ensures we don't introduce a regression for users who had working setups
183+
}
148184

149185
# Run unregistry container on remote host with retry logic for port binding conflicts.
150186
# Sets UNREGISTRY_PORT and UNREGISTRY_CONTAINER global variables.
151187
run_unregistry() {
152188
local output
153189

190+
# Find containerd socket first
191+
find_containerd_socket
192+
154193
# Pull unregistry image if it doesn't exist on the remote host. This is done separately to not capture the output
155194
# and print the pull progress to the terminal.
156195
# shellcheck disable=SC2029
@@ -166,12 +205,22 @@ run_unregistry() {
166205
if output=$(ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker run -d \
167206
--name $UNREGISTRY_CONTAINER \
168207
-p 127.0.0.1:$UNREGISTRY_PORT:5000 \
169-
-v /run/containerd/containerd.sock:/run/containerd/containerd.sock \
208+
-v $CONTAINERD_SOCKET:/run/containerd/containerd.sock \
170209
--userns=host \
171210
--user root:root \
172211
$UNREGISTRY_IMAGE" 2>&1);
173212
then
174-
return 0
213+
# Wait a moment for the container to start
214+
sleep 1
215+
216+
# Verify the container is actually running and healthy
217+
if ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker ps --filter name=$UNREGISTRY_CONTAINER --filter status=running --quiet" | grep -q .; then
218+
return 0
219+
else
220+
warning "Unregistry container started but is not running properly, retrying..."
221+
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rm -f $UNREGISTRY_CONTAINER" >/dev/null 2>&1 || true
222+
continue
223+
fi
175224
fi
176225

177226
# Remove the container that failed to start if it was created.
@@ -399,9 +448,25 @@ if [ -n "$DOCKER_PLATFORM" ]; then
399448
DOCKER_PUSH_OPTS+=("--platform" "$DOCKER_PLATFORM")
400449
fi
401450

402-
# That DOCKER_PUSH_OPTS expansion is needed to avoid issues with empty array expansion in older bash versions.
403-
if ! docker push ${DOCKER_PUSH_OPTS[@]+"${DOCKER_PUSH_OPTS[@]}"} "$REGISTRY_IMAGE"; then
404-
error "Failed to push image."
451+
# Try push with retry logic for connection issues
452+
PUSH_RETRY_COUNT=3
453+
PUSH_SUCCESS=false
454+
455+
for attempt in $(seq 1 $PUSH_RETRY_COUNT); do
456+
# That DOCKER_PUSH_OPTS expansion is needed to avoid issues with empty array expansion in older bash versions.
457+
if docker push ${DOCKER_PUSH_OPTS[@]+"${DOCKER_PUSH_OPTS[@]}"} "$REGISTRY_IMAGE"; then
458+
PUSH_SUCCESS=true
459+
break
460+
else
461+
if [ "$attempt" -lt $PUSH_RETRY_COUNT ]; then
462+
warning "Push attempt $attempt failed, retrying in 3 seconds..."
463+
sleep 3
464+
fi
465+
fi
466+
done
467+
468+
if [ "$PUSH_SUCCESS" = false ]; then
469+
error "Failed to push image after $PUSH_RETRY_COUNT attempts."
405470
fi
406471

407472
REMOTE_REGISTRY_IMAGE=""

0 commit comments

Comments
 (0)