8
8
# Script version
9
9
VERSION=" 0.1.3"
10
10
11
+ # Ensure localhost connections bypass proxy
12
+ export no_proxy=" ${no_proxy:- } ,localhost,127.0.0.1"
13
+
11
14
# Return metadata expected by the Docker CLI plugin framework: https://github.com/docker/cli/pull/1564
12
15
if [ " ${1:- } " = " docker-cli-plugin-metadata" ]; then
13
16
cat << EOF
@@ -111,12 +114,12 @@ ssh_remote() {
111
114
SSH_ARGS+=(" ${target} " )
112
115
}
113
116
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
115
118
# to run docker commands.
116
119
REMOTE_SUDO=" "
117
120
118
121
# 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 ".
120
123
check_remote_docker () {
121
124
# Check if docker command is available.
122
125
if ! ssh " ${SSH_ARGS[@]} " " command -v docker" > /dev/null 2>&1 ; then
@@ -125,8 +128,8 @@ check_remote_docker() {
125
128
# Check if we need sudo to run docker commands.
126
129
if ! ssh " ${SSH_ARGS[@]} " " docker version" > /dev/null 2>&1 ; then
127
130
# 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 "
130
133
else
131
134
error " Failed to run docker commands on remote host. Please ensure:
132
135
- Docker is installed and running on the remote host
@@ -145,12 +148,48 @@ random_port() {
145
148
UNREGISTRY_CONTAINER=" "
146
149
# Unregistry port on the remote host that is bound to localhost. It's populated by run_unregistry function.
147
150
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
+ }
148
184
149
185
# Run unregistry container on remote host with retry logic for port binding conflicts.
150
186
# Sets UNREGISTRY_PORT and UNREGISTRY_CONTAINER global variables.
151
187
run_unregistry () {
152
188
local output
153
189
190
+ # Find containerd socket first
191
+ find_containerd_socket
192
+
154
193
# Pull unregistry image if it doesn't exist on the remote host. This is done separately to not capture the output
155
194
# and print the pull progress to the terminal.
156
195
# shellcheck disable=SC2029
@@ -166,12 +205,22 @@ run_unregistry() {
166
205
if output=$( ssh " ${SSH_ARGS[@]} " " $REMOTE_SUDO docker run -d \
167
206
--name $UNREGISTRY_CONTAINER \
168
207
-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 \
170
209
--userns=host \
171
210
--user root:root \
172
211
$UNREGISTRY_IMAGE " 2>&1 ) ;
173
212
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
175
224
fi
176
225
177
226
# Remove the container that failed to start if it was created.
@@ -399,9 +448,25 @@ if [ -n "$DOCKER_PLATFORM" ]; then
399
448
DOCKER_PUSH_OPTS+=(" --platform" " $DOCKER_PLATFORM " )
400
449
fi
401
450
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."
405
470
fi
406
471
407
472
REMOTE_REGISTRY_IMAGE=" "
0 commit comments