Seeing port mappings

Hi all,

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.)

The tool’s 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 bash, 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 #

Many thanks!

djm

is --net=host an option? it’s just an utility script, so shouldn’t be that much of an issue?
then you wouldn’t need the port mapping at all.

Hm, I think the catch there would be that we couldn’t run multiple cs50/cli containers at a time, if each wants to use 8080, 8081, and/or 8082? (We could manually run the port-using services in each container on randomly chosen ports, but that would just push the pseudorandomness on to the user?)

Thanks!

But when you use --net=host, you would not need the infinity-sleep anymore….so maybe this problem is then gone?
BTW: You anyway got problems to figuring out once there are multiple cs50/cli-containers, whose port mappings you should read and use.

so, 1st… what ports…

docker inspect on the containers will show you what host ports have been mapped to what container ports.

and for the name, you should use --name container{number} or something…

if --net=host is no option for you, then just use your sleep-inspect way. :slight_smile: