如何调用非交互式docker compose-run命令-SSH



情况描述:我有一个运行在远程计算机B上的Ubuntu虚拟机。我可以通过在我的主机(计算机a(上执行的SSH访问这台计算机B。当我在计算机B上通过SSH进入交互式shell时,我可以毫无问题地执行所有docker命令,步骤如下:

  • ssh user@remote_IP(在远程shell中交互输入(
  • docker-compose run -d --name wfirst --user 1000:1000 container(组成一个docker容器并启动它(

这非常有效,我的容器已经安装、启动并运行。然而,如果我试图通过在我的主机终端中写入来以非交互式的方式运行该命令

  • ssh user@remoteIP "cd /path_to_docker_file;docker-compose run -d --name wfirst --user 1000:1000 container"

命令未成功,我的容器未装入且未运行。通过使用docker compose命令上的"--verbose"标志,我可以获得更多信息。

这里有趣的部分输出的成功方法:

compose.config.config.find: Using configuration files: ./docker-compose.yml
WARNING: compose.config.environment.__getitem__: The DISPLAY variable is not set. Defaulting to a blank string.
WARNING: compose.config.environment.__getitem__: The NO_PROXY variable is not set. Defaulting to a blank string.
docker.utils.config.find_config_file: Trying paths: ['/home/user/.docker/config.json', '/home/user/.dockercfg']
docker.utils.config.find_config_file: No config file found
docker.utils.config.find_config_file: Trying paths: ['/home/user/.docker/config.json', '/home/user/.dockercfg']
docker.utils.config.find_config_file: No config file found
urllib3.connectionpool._new_conn: Starting new HTTP connection (1): localhost:2375
urllib3.connectionpool._make_request: http://localhost:2375 "GET /v1.25/version HTTP/1.1" 200 758
compose.cli.command.get_client: docker-compose version 1.21.0, build unknown
docker-py version: 3.4.1
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.1c  28 May 2019
compose.cli.command.get_client: Docker base_url: http://localhost:2375
...

我们可以看到,到docker的HTTP连接已经成功建立。然后,该命令继续执行,并能够从docker映像创建容器。

这里是失败方法的输出:

compose.config.config.find: Using configuration files: ./docker-compose.yml
compose.config.environment.__getitem__: The DISPLAY variable is not set. Defaulting to a blank string.
compose.config.environment.__getitem__: The NO_PROXY variable is not set. Defaulting to a blank string.
docker.utils.config.find_config_file: Trying paths: ['/home/user/.docker/config.json', '/home/user/.dockercfg']
docker.utils.config.find_config_file: No config file found
docker.utils.config.find_config_file: Trying paths: ['/home/user/.docker/config.json', '/home/user/.dockercfg']
docker.utils.config.find_config_file: No config file found
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 384, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.7/http/client.py", line 1344, in getresponse
response.begin()
File "/usr/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.7/http/client.py", line 267, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/lib/python3/dist-packages/six.py", line 693, in reraise
raise value
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 386, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 306, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: UnixHTTPConnectionPool(host='localhost', port=None): Read timed out. (read timeout=60)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/bin/docker-compose", line 11, in <module>
load_entry_point('docker-compose==1.21.0', 'console_scripts', 'docker-compose')()
File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 71, in main
command()
File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 124, in perform_command
project = project_from_options('.', options)
File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 41, in project_from_options
compatibility=options.get('--compatibility'),
File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 121, in get_project
host=host, environment=environment
File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 95, in get_client
version_info = six.iteritems(client.version())
File "/usr/lib/python3/dist-packages/docker/api/daemon.py", line 181, in version
return self._result(self._get(url), json=True)
File "/usr/lib/python3/dist-packages/docker/utils/decorators.py", line 46, in inner
return f(self, *args, **kwargs)
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 198, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 546, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 529, in send
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: UnixHTTPConnectionPool(host='localhost', port=None): Read timed out. (read timeout=60)

我们可以看到HTTP连接无法建立。

为什么我需要以非交互式方式发送命令?最初,我想使用Jenkins发送这些命令(我在Jenkins中添加了一个SSH插件(,我注意到docker命令不起作用(与本文中显示的输出相同(。经过几次测试,我意识到当Jenkins使用SSH时,他会以非交互式的方式发送命令:-ssh user@remote_IP "commands_to_execute"

这种非交互式方式对简单的命令来说不是问题,但对一些需要在交互式shell中执行的docker命令来说似乎是个问题?有人找到了在非交互式shell中成功执行docker命令的解决方案吗?任何帮助、提示或替代解决方案都将不胜感激,因为到目前为止,我尝试了很多事情,但都没有成功。。

您检查它是在tcp上运行的docker引擎还是通过unix套接字运行的?

当您通过ssh u@h"命令"登录时,可能没有读取正确的环境变量。

如果它在unix套接字上运行(默认情况下(,请尝试。。。

ssh user@remoteIP"cd/path_to_docker_file;docker_HOST=unix:///var/run/docker.sockdocker compose run-d--name wfirst--user 1000:1000 container">

或者如果运行在TCP上,您可以尝试。。。

ssh user@remoteIP"cd/path_to_docker_file;docker_HOST=tcp://127.0.0.1:2375docker compose run-d--name wfirst--user 1000:1000 container">

最新更新