简体   繁体   English

docker-compose 到使用 ssh 的远程主机在 gitlab ci 中失败

[英]docker-compose to a remote host using ssh fails in gitlab ci

I'm unable to docker-compose successfully on a remote host using SSH from a gitlab CI/CD pipeline runner.我无法使用 gitlab CI/CD 管道运行器中的 SSH 在远程主机上成功执行 docker-compose。

docker-compose fails with the following errors: docker-compose 失败并出现以下错误:

http.client.RemoteDisconnected: Remote end closed connection without response
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

I'm not able to repro the issue outside Gitlab.我无法在 Gitlab 之外重现该问题。 Meaning, I tried on my local machine by running the same docker image my gitlab deploy step use, do the exact same commands in my script manually and it worked like a charm...意思是,我尝试在我的本地机器上运行相同的 docker 图像我的 gitlab 部署步骤使用,在我的脚本中手动执行完全相同的命令,它就像一个魅力......

I'm even trying the ssh config in the script by connecting to the host and printing docker version and it succeeds properly.我什至通过连接到主机并打印docker version来尝试脚本中的 ssh 配置,并且它成功成功。

deploy_to_staging.sh deploy_to_staging.sh

#!/bin/bash

apk update
apk add openssh-client
apk add --update bash

eval $(ssh-agent -s)
mkdir -p ~/.ssh
chmod 600 ~/.ssh
# copy the server private key
echo "$SSH_PRIVATE_KEY"
echo "$SSH_PRIVATE_KEY" | ssh-add -
# copy server host key .pub file from /etc/ssh/ and paste it into this variable
echo "$SSH_SERVER_HOSTKEYS"
ssh-keyscan -p 12345 11.222.3.444
ssh-keyscan -p 12345 11.222.3.444 >> ~/.ssh/known_hosts

ssh root@11.222.3.444 -p 12345 "docker version; exit"

# Docker-compose app and db
docker-compose --verbose -H "ssh://root@11.222.3.444:12345" -f docker-compose.staging.yml up -d --build

exit

gitlab-ci.yml gitlab-ci.yml

image: alpine:latest

services:
  - docker:dind

stages:
  - build
  - package
  - deploy

[...]

deploy:
  image: docker/compose:alpine-1.28.2
  stage: deploy
  environment:
    name: staging
  only:
    - master
  script:
    - sh deploy_to_staging.sh

Thanks for your help谢谢你的帮助

I had this exact problem, depending on your configuration it might work or not.我遇到了这个确切的问题,具体取决于您的配置,它可能有效或无效。 Basically, i created a user deployer on my server which I was trying to ssh to.基本上,我在我的服务器上创建了一个用户deployer程序,我试图 ssh 到。

Since i had forgotten to add this new user ( deployer ) to the docker usergroup, he had no docker permissions.由于我忘记将此新用户( deployer )添加到docker用户组,他没有 docker 权限。 So basically all I did was所以基本上我所做的就是

[root@myserver ~]# groupadd docker
groupadd: group 'docker' already exists
[root@myserver ~]# usermod -aG docker deployer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM