Skip to content

Instantly share code, notes, and snippets.

@devymex
Last active June 15, 2021 03:06
Show Gist options
  • Save devymex/96cba35cdc831b7f89b6a88357d59471 to your computer and use it in GitHub Desktop.
Save devymex/96cba35cdc831b7f89b6a88357d59471 to your computer and use it in GitHub Desktop.
Run an docker image as a container with public ssh port
#!/bin/bash -e
if [ $# -eq 0 ]; then
echo "Please specify an name for the new container!"
exit
fi
indices=""
for cid in $(docker container ls -a | sed 1,1d | awk '{print $1}'); do
port=$(docker inspect --format='{{if .NetworkSettings.Ports}}{{(index (index .NetworkSettings.Ports "22/tcp") 0).HostPort}}{{else}}{{"null"}}{{end}}' $cid)
indices=$indices" "$port
done
available_port=""
for i in {3003..3099}; do
if [ -z $(echo $indices | grep -o "$i") ]; then
available_port=$i
break
fi
done
docker run -it -d --name $1 -p $available_port:22 --gpus all -v /mnt/data:/data --shm-size=1G nvidia/cuda:devbase /bin/init
echo "Listening port "$available_port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment