Using lazydocker with SSH-based remote contexts
A quick hack to work around the current issue with lazydocker
and SSH-based remote Docker context definitions.
I've used the excellent lazydocker before, but only occasionally, reverting to the docker
CLI for most of my monitoring work. I thought I'd start using it again and learn more about it. While I can (and have) run lazydocker
as a Docker container I wanted to run it locally in the Linux container of my Chromebook and connect remotely to my Docker engines running on various machines.
If you are going to run
lazydocker
as a Docker container then I recommend you build the image yourself (theDockerfile
is in the repo) as the image on DockerHub is rather out of date.
I manage these remote engines via Docker contexts. Running docker context ls
here shows me this:
NAME DESCRIPTION DOCKER ENDPOINT
default Current DOCKER_HOST based configuration unix:///var/run/docker.sock
docker * Docker Host on PVE LXC ssh://dj@docker
homeops Docker Host on homeops ssh://dj@homeops
kkhw42xrfy M2 Air ssh://user@kkhw42xrfy
synology Docker Host on Synology NAS ssh://dj@synology
If you're interested in finding out how to define and use these contexts, see the post Remote access to Docker on my Synology NAS.
Currently there are some issues with lazydocker
correctly using the SSH-based remote addresses - see for example issue 510 which, while closed, describes exactly what I experience even with the latest release, as does another user. I have no doubt that this will be sorted soon, but in the meantime I decided to set up a workaround, based on the fact that the DOCKER_HOST
environment variable is correctly evaluated (see feat(docker): Honor the host specified in current docker context) even if the host info in the context is not.
If my context is set to docker
, for example, like this:
docker context set docker
then if I start lazydocker
I get an error:
Docker event stream returned error: error during connect: Get "http://dj%40docker/v1.25/events": dial tcp: lookup dj@docker: no such host
However, if I set DOCKER_HOST
before the invocation, like this:
DOCKER_HOST=ssh://dj@docker lazydocker
then lazydocker
connects successfully.
The context name
docker
here might be slightly confusing - it's because that's what I called the LXC container I set up on Proxmox for running a Docker engine.
I can use docker context inspect
to look into the details of the context, which look like this:
[
{
"Name": "docker",
"Metadata": {
"Description": "Docker Host on PVE LXC"
},
"Endpoints": {
"docker": {
"Host": "ssh://dj@docker",
"SkipTLSVerify": false
}
},
"TLSMaterial": {},
"Storage": {
"MetadataPath": "/home/qmacro/.docker/contexts/meta/d548...",
"TLSPath": "/home/qmacro/.docker/contexts/tls/d548..."
}
}
]
And using the --format
option of docker context inspect
like this:
docker context inspect --format '{{.Endpoints.docker.Host}}'
I can get the SSH-based host value ssh://dj@docker
which I can then set in DOCKER_HOST
and invoke lazydocker
.
Putting this all together I have now an alias lado
defined thus:
alias lado="DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}') lazydocker"
which sets the appropriate value, from the current context, in DOCKER_HOST
, and invokes lazydocker
.