'How to run docker command remotely using ssh on mac?

I tried running remote commands with sshd open locally on Mac.

For example

$ ssh username@localhost ls

Command like this work just fine.

But

$ ssh username@localhost docker

is not executed and the following error is displayed.

$ ssh username@localhost docker
sh: docker: command not found

If I connect with the command and run the docker command, it runs normally.

$ssh username@localhost
$docker

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/Users/7143213/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set
                           with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/Users/7143213/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/Users/7143213/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/Users/7143213/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
...
ssh username@localhost /usr/local/bin/docker

Even if I write down the full path of docker as above, it runs normally.

The /usr/local/bin path is registered in .bash_profile and .profile.

Docker was launched as docker desktop on mac.

The Mac version is macOS monterey 12.2.

Update

In my case, the issue was that the path was not set in non-interactive non-login of ssh.

Because I was using zsh I solved it by adding alias docker=/usr/local/bin/docker to /etc/zshenv .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source