简体   繁体   English

使用ssh从Gitlab CI运行docker-compose在远程主机上

[英]Run docker-compose up on remote host from Gitlab CI using ssh

In my docker-compose.yml I use some .env variables. 在我的docker-compose.yml文件中,我使用了一些.env变量。 Just like that: 就像这样:

services:
  ms:
    image: 'template:latest'
    build:
      context: .
    restart: always
    ports:
      - '${PORT}:${PORT}'
    env_file:
      - .env

Docker documentation says, that I have to run docker-compose up command from directory when .env and docker-compose.yml files are present together. Docker文档说,当.env和docker-compose.yml文件同时存在时,我必须从目录运行docker-compose up命令。

I want to use GitLab CI to deploy mi service automatically. 我想使用GitLab CI自动部署mi服务。 This is the part of gitlab-ci-yml file: 这是gitlab-ci-yml文件的一部分:

image: docker:latest

services:
  - docker:dind

...

run-deploy-prod:
  stage: deploy
  cache: {}
  before_script:
  # ssh configuration for alpine linux
  script:
    # move docker-compose.yml and .env to the location ~/docker_app/app
    - ssh $USER_NAME@$HOST_ADDRESS 'docker-compose -f '~/docker_app/app/docker-compose.yml' up --no-build -d'

The question: How can I run docker-compose exactly from the location ~/docker_app/app to load .env variables properly? 问题:如何才能从〜/ docker_app / app位置准确运行docker-compose以正确加载.env变量?

I tried to use ssh with -T, -tt and -t options, like: 我试图将ssh与-T,-tt和-t选项一起使用,例如:

ssh -T bob@foo "cd ~/docker_app/app && exec \$SHELL && docker-compose up..."

without any success. 没有任何成功。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I'm pretty sure … && exec \\$SHELL && … cannot work (see eg the official doc on the exec builtin ). 我非常确定… && exec \\$SHELL && …无法工作(例如,参见exec内置的官方文档)。

However, it seems your first attempt should be OK, after fixing the quotes : could you try the following? 但是,在修正引号之后,您的首次尝试似乎应该可以:您可以尝试以下操作吗?

- (…)
- ssh $USER_NAME@$HOST_ADDRESS "docker-compose -f ~/docker_app/app/docker-compose.yml up --no-build -d"

or equivalently: 或等效地:

- (…)
- ssh $USER_NAME@$HOST_ADDRESS "cd ~/docker_app/app/ && docker-compose up --no-build -d"

Otherwise, could you paste the error log you obtain? 否则,您可以粘贴获取的错误日志吗?

(Also, you should make sure that the docker-compose.yml and .env files have been copied to your remote server at $HOST_ADDRESS in the appropriate folder. − This is obvious, but I wasn't sure you had used scp or rsync given the related comment in your question "move docker-compose.yml and .env …") (此外,您应确保将docker-compose.yml.env文件已复制到相应文件夹中位于$HOST_ADDRESS .env远程服务器上.env这很明显,但是我不确定您是否使用过scprsync在您的问题“中移动docker-compose.yml和.env…”中给出了相关评论)

For anyone that comes across Gitlab Runner's and SSH timeout, this is the only thread that helped me realize and fix the issue. 对于遇到Gitlab Runner和SSH超时的任何人,这是唯一帮助我意识到并解决问题的线程。 As stashco commented, for your cd command, you must use it like so: 正如stashco所评论的那样,对于cd命令,您必须像这样使用它:

cd /path/to/docker-compose &> /dev/null

Without this &> /dev/null , the runner stalls out every time. 没有这个&> /dev/null ,跑步者每次都会停滞。 Hope this helps someone searching for Gitlab Runner timeouts, just like I was stumped over here: How to fix SSH Webpack build timeout on CI/CD (Gitlab) . 希望这对搜索Gitlab Runner超时的人有所帮助,就像我在这里陷入困境一样: 如何在CI / CD(Gitlab)上修复SSH Webpack构建超时

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

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