1

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

FARZAD
  • 85
  • 1
  • 9
  • 1
    It sounds like this is actually about how to expose a port and do dns routing, but your GitLab workflow is fine? Please clarify exactly what the specific issue is and limit your scope to that single issue. See the guide to [ask] – Michael Delgado May 14 '22 at 14:47
  • @MichaelDelgado thank you for the comment. Sorry for not explaining myself clearly! Basically my only problem right now is running that container that I pulled from the GitLab registry! – FARZAD May 14 '22 at 19:30
  • 1
    But your question title says “how to deploy” and your question itself covers a huge range of topics and doesn’t clearly explain exactly what the issue is. Can you edit your question to provide only the details which fully describe a single topic? If you have multiple independent questions feel free to create multiple question posts. Really try when asking to follow the guidance on creating a [mre]. See also this guide to [crafting a minimal bug report](//matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports). – Michael Delgado May 14 '22 at 19:42
  • Sure, yes agreed! – FARZAD May 14 '22 at 19:58
  • Are you trying to host a public website from inside a gitlab ci/cd runner? You can’t do this — there’s no way it would ever work. – Michael Delgado May 14 '22 at 20:05
  • @MichaelDelgado I have updated the question, hopefully it makes sense to what I'm trying to do! I have an Azure Virtual Machine with Docker installed and configured to serve on port 80! When I run the step #6 manually it works fine, but when done throught CI/CD it doesn't despite the job executes successfully. – FARZAD May 14 '22 at 21:19
  • Ok thanks for the clarification! Your question is now more clear and sytech’s answer definitely points to the issue I see. Good luck! – Michael Delgado May 15 '22 at 08:20

1 Answers1

2

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.

sytech
  • 29,298
  • 3
  • 45
  • 86