How to run a Docker Containers pulled from GitLab Container Registry using Shared Runners?

1.2k Views Asked by At

Basically what I have got working so far is as follows in the following order;

  1. Login to GitLab Container Registry
  2. Build Docker Containers from docker-compose.yml config file using GitLab shared runners CI/CD
  3. Push all built Docker containers to GitLab Container Registry
  4. Login to my Azure VM Ubuntu server via SSH
  5. Pull the Docker containers from GitLab Container Registry
  6. Run the desired Docker container, in this instance "*/client:latest", exosing it to port 80
  7. Logging running Containers, which proves my desired contaier exists which does

The step #6 is the only step that seems running successfully, however when I visit my domain there is nothing running , despite the CI job completed successfully!

However, when I try doing the same thing manually using PuTTY, pulling the image from the registry and running on port 80, everything works fine when I check my domain!

I would appreciate it if someone could see what I'm possibly doing wrong, thank you.

Here is my GitLab YAML config file;

image: docker:latest

services:
  - docker:dind

stages:
  - deploy

deploy:
    stage: deploy
    before_script:
      - eval $(ssh-agent -s)
      - echo "$SERVER_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
      - mkdir -p ~/.ssh
      - chmod 700 ~/.ssh
      - ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
      - chmod 644 ~/.ssh/known_hosts
    script:
      - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
      - docker-compose --file docker-compose.prod.yml build --force-rm --no-cache
      - docker push registry.gitlab.com/[MY_GITLAB_ACCOUNT]/app/client:latest
      - ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST
      - docker pull registry.gitlab.com/[MY_GITLAB_ACCOUNT]/app/client:latest
      - docker images --all
      - docker run -p 80:8080 --detach registry.gitlab.com/[MY_GITLAB_ACCOUNT]/app/client:latest
      - docker ps --all

enter image description here

Thank you

1

There are 1 best solutions below

0
On

In your GitLab CI job, your SSH command is executing, then closing the remote shell immediately before moving onto the next shell step. So, you're just running docker run in your GitLab job, not on your server.

To run a command on the remote server, you need to inline the command you want to run with the ssh command.

script:
  # ...
  - ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST "docker run --pull -p 80:8080 -d registry.gitlab.com/[MY_GITLAB_ACCOUNT]/app/client:latest"

Keep in mind, your remote server will also need to have authentication configured to be able to pull from your registry.