We use a command-line tool,
cli50, that essentially makes it easier to spawn a Linux container on a host and mount one’s $PWD inside of it. Per https://github.com/cs50/cli50/blob/develop/cli50, it essentially does this:
docker pull cs50/cli docker run --publish-all --volume "$PWD":/home/ubuntu/workspace --workdir /home/ubuntu/workspace cs50/cli bash --login
(Omitted from the above are some customization features.)
Dockerfile, meanwhile, per https://github.com/cs50/cli/blob/master/Dockerfile, exposes 3 ports:8080, 8081, and 8082. We’d like the user to know to what ports those 3 have been pseudorandomly mapped (per
--publish-all), without hardcoding a mapping (so that you can run multiple containers on multiple trios of ports). We initially tried, in a nutshell
docker run --detach cs50/cli bash --login # to run container in background docker port # to figure out port mapping docker attach # to re-attach to container
but that approach seemed suboptimal, since when we re-attach to the container, the user doesn’t see a bash prompt (since it printed when the container was background), so it’s non-obvious that they have an interactive terminal at that point.
As a workaround, we’ve since started doing, in a nutshell:
docker run --detach cs50/cli sleep infinity # to run container in background docker port # to figure out port mapping docker exec --interactive --tty bash --login # to start bash in container
But a side effect is that, when you exit out of
sleep is still running in the background, so the container doesn’t automatically stop.
Is there a more elegant solution here that would yield the same UX, a la the below?
$ cli50 latest: Pulling from cs50/cli Digest: sha256:7ba11681b4eb4eef363a94f38ba23973b32b8c5bcc8bf212bf5b57127ee156b8 Status: Image is up to date for cs50/cli:latest 8080/tcp -> 0.0.0.0:32776 8081/tcp -> 0.0.0.0:32775 8082/tcp -> 0.0.0.0:32774 This is CS50 CLI. root@9183ff478d12:/home/ubuntu/workspace #