Basically what I have got working so far is as follows in the following order;
- Login to GitLab Container Registry
- Build Docker Containers from docker-compose.yml config file using GitLab shared runners CI/CD
- Push all built Docker containers to GitLab Container Registry
- Login to my Azure VM Ubuntu server via SSH
- Pull the Docker containers from GitLab Container Registry
- Run the desired Docker container, in this instance "*/client:latest", exosing it to port 80
- 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
Thank you
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.Keep in mind, your remote server will also need to have authentication configured to be able to pull from your registry.