Skip to content

Commit

Permalink
optimize dk.sh, support containers with sh in
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Nov 1, 2023
1 parent 4ad87d6 commit 76248d8
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions dk.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
#!/bin/sh

# this script provides access to scratch-based containers by temporary copy busybox in.
# removes all of this on exit
# this script provides access to scratch-based containers ot any other container without shell
# by temporary copy busybox in. removes all of this on the exit

# check if sh is already available
if docker exec -it "$1" sh -c ls 1>/dev/null 2>/dev/null; then
# check for available shells
for shell in bash zsh ash sh; do
if docker exec -it "$1" $shell -c ls 1>/dev/null 2>/dev/null; then
docker exec -it "$1" $shell
exit 0
fi
done
exit 1
fi

id="$$" # randomize file names to prevent collisions if run multiple times

# copy busybox into running container as /busybox.<random>
docker run -d --rm --name=bbox busybox:latest sleep 10 1>/dev/null 2>/dev/null
docker cp bbox:/bin/busybox .
docker cp ./busybox "$1":/busybox.${id}
docker run -d --rm --name=bbox busybox:stable-musl sleep 10 1>/dev/null 2>/dev/null
docker cp bbox:/bin/busybox . 1>/dev/null 2>/dev/null
docker cp ./busybox "$1":/busybox.${id} 1>/dev/null 2>/dev/null
rm -f ./busybox

# install busybox inside the container
docker exec -u 0 -it "$1" /busybox.${id} sh -c "
export "PATH=/busybin.${id}"
export PATH=/busybin.${id}
/busybox.${id} mkdir /busybin.${id} 2>/dev/null
/busybox.${id} --install /busybin.${id}
"

# make shell script with busybin path and copy it in
script="
export "PATH=/busybin.${id}"
sh
"
echo "$script" > sh.${id}
chmod +x sh.${id}
docker cp sh.${id} "$1":/sh.${id}
rm -rf sh.${id}

# invoke container shell
docker exec -it "$1" /busybox.${id} sh -c /sh.${id}
docker exec -it "$1" /busybox.${id} sh -c "export PATH=/busybin.${id}; exec sh"

# cleanup temp files on exit
docker exec -u 0 -it "$1" /busybox.${id} sh -c "
echo cleaning...
/busybox.${id} rm -rf /busybin.${id}
/busybox.${id} rm -f /sh.${id}
/busybox.${id} rm -f /busybox.${id}
Expand Down

0 comments on commit 76248d8

Please sign in to comment.