Idiosyncrasy of the Docker CLI

Although I use Docker on a daily basis, I never really got into its CLI. Most of the time I am just baffled how complex it is to get really simple things done.

Just to set up the stage (I plan on writing a detailed post about Redpanda):

During my experiments with the latest Quarkus version, I stumbled upon a faster replacement for Kafka, which is either used as a default devservice now or some default of Kogito.

A devservices you might be wondering? Without going much into detail, it is basically an integration of Testcontainers to start required infrastructure services like a database for testing in Docker. Devservices also take care of the config, so all the required mapping is set automatically.

To configure Redpanda, I need to know the exposed port to connect to and the Testcontainers integration prints it dutifully to the log though:

2021-07-23 07:48:44,130 INFO  [io.qua.kaf.cli.dep.DevServicesKafkaProcessor] (build-38) Dev Services for Kafka started. Start applications that need to use the same Kafka broker using -Dkafka.bootstrap.servers=PLAINTEXT://localhost:55002

This is still no option for custom scripts or in any way convenient.

Docker CLI

Docker comes with a CLI as you probably know. After some digging into and a pinch of despair, mapping of image name to the actual container is quite complicated. For my taste it takes way too many calls to different shell tools, which probably is not portable, but it can be done with port and inspect:

$ docker port $(docker ps | grep redpanda | awk '{print $1}') | grep -m 1 9092 | cut -d ':' -f 2

Another thing I probably just lack experience with, is the format string. I am quite sure this is just awesome for any seasoned Go developer and it is basically everywhere from there starting with supporting tools like Helm or the foundation of the cloud itself, Kubernetes.

So if the next example is pretty straight forward to you have fun with it, I am in a corner reading specs:

{% raw %}$ docker inspect --format='{{(index (index .NetworkSettings.Ports "9092/tcp") 0).HostPort}}' $(docker ps --format "{{.ID}}" --filter="ancestor=vectorized/redpanda:v21.5.5"){% endraw %}