0
votes

In my docker-compose.yml I use some .env variables. 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.

I want to use GitLab CI to deploy my service automatically. This is the part of .gitlab-ci.yml file:

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?

I tried to use ssh with -T, -tt and -t options, like:

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

without any success.

Any help would be greatly appreciated.

2

2 Answers

0
votes

I'm pretty sure … && exec \$SHELL && … cannot work (see e.g. the official doc on the exec builtin).

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 …")

0
votes

For anyone that comes across Gitlab Runner's and SSH timeout, this is the only thread that helped me realize and fix the issue. As stashco commented, for your cd command, you must use it like so:

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

Without this &> /dev/null, the runner stalls out every time. 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).