2

I am using Docker CLI.
I want to print the current Docker endpoint according to the current Docker context.

Here is the output structure:

$ docker context ls --format '{{ json . }}' | jq
{
  "Current": true,
  "Description": "Current DOCKER_HOST based configuration",
  "DockerEndpoint": "unix:///run/user/1000/docker.sock",
  "KubernetesEndpoint": "",
  "Name": "default",
  "StackOrchestrator": "swarm"
}
{
  "Current": false,
  "Description": "Rootless mode",
  "DockerEndpoint": "unix:///run/user/1000/docker.sock",
  "KubernetesEndpoint": "",
  "Name": "rootless",
  "StackOrchestrator": ""
}

The closest solution I have is:

$ docker context ls --format '{{- if .Current -}} {{- .DockerEndpoint -}} {{- end -}}'
unix:///run/user/1000/docker.sock


Can I get rid of the output blank lines via a pure go template mere enhancement?

I don't want to rely on any additional shell command to keep it portable across all environments

Réda Housni Alaoui
  • 1,244
  • 2
  • 15
  • 22

0 Answers0