2

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 .

battlecook
  • 471
  • 6
  • 17
  • try compare result of `ssh ... echo $PATH` and with manually exuecte? – Lei Yang Feb 15 '22 at 09:48
  • 2
    This might be similar to another issue I ran into the other day: `.bashrc` is only run if the shell is in interactive mode. I suspect that when you run the command directly, the shell is in non-interactive mode and .bashrc isn't run. If Docker Desktop sets up the path in .bashrc, then that could explain why it can't find it. You can test by forcing it into interactive mode with `ssh username@localhost bash -i -c docker`. – Hans Kilian Feb 15 '22 at 09:50
  • 2
    Thanks @HansKilian I got a hint from your answer. I searched for interactive, non-interactive, login, and non-login of ssh. https://stackoverflow.com/Questions/940533/how-do-i-set-path-such-that-ssh-userhost-command-works I noticed in this question and in the comments that path is not set for non-interactive . After some more searching, I found the answer: https://stackoverflow.com/a/27742679/13429399 I added "docker=/usr/local/bin/docker" to "/etc/zshenv" folder. It works the way I want it to. – battlecook Feb 17 '22 at 16:13
  • I had this problem and fixed it by extending the PATH in `.bashrc`, but there's a trick. (At least on my system) `.bashrc` is also run in non-interactive mode, _but_ in that case the first thing typical `.bashrc` files do is exit — `# If not running interactively, don't do anything` ... `case $- in *i*) ;; *) return;; esac`. So I extended the PATH _before_ that line. – Blaisorblade Aug 06 '23 at 12:51

0 Answers0