Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto deploy Docker Image on own server with GitLab?

I am trying to Google it for few hours, but can't find it. I have Java/Spring application (+MySQL if it matters) and I am looking to create CI for that.

I know what to do and how:

  1. I know that I have to move my Git repo to Gitlab.
  2. Push to repo will trigger CI script.
  3. Gitlab will build my docker image into Gitlab Docker Registry.

Question is:

What do I have to do to force docker compose on my VPS to pull the new image from Gitlab and restart the server? I know (correct me if I am wrong) that on my VPS I should run docker-compose pull && docker-compose up inside my app folder, but I have literally no idea how to make it automatically with Gitlab?

like image 665
Marek Urbanowicz Avatar asked Jun 14 '17 13:06

Marek Urbanowicz


People also ask

Can we push Docker image to GitLab?

You can build your own Docker images and publish them in the GitLab Container Registry, which can act as a private registry. Maybe you are wondering if there is a way to store Docker images at GitLab and use them in pipelines. There are many reasons why you may want to use the GitLab Container Registry.

How does Docker integrate with GitLab?

Run your CI/CD jobs in Docker containers. For example, you can tell GitLab CI/CD to use a Node image that's hosted on Docker Hub or in the GitLab Container Registry. Your job then runs in a container that's based on the image. The container has all the Node dependencies you need to build your app.


1 Answers

What do I have to do to force docker compose on my VPS to pull the new image from Gitlab and restart the server?

@m-uu, you don't need restart the server at all, just do docker-compose up to pull new image and restart service

I know (correct me if I am wrong) that on my VPS I should run docker-compose pull && docker-compose up inside my app folder, but I have literally no idea how to make it automatically with Gitlab?

Yes, you are on the right way. Look at my Gitlab CI configuration file, I think it doesn't difficult to change it for Java project. Just give you ideas how to build, push to your registry and deploy an image to your server. One thing you need to do is generate SSH keys and push public to server (.ssh/authorized_keys) and private to GITLAB pipeline secret variable (https://docs.gitlab.com/ee/ci/variables/#secret-variables)

cache:
  key: "cache"
  paths:
  - junte-api

stages:
  - build
  - build_image
  - deploy

build:
  image: golang:1.7
  stage: build
  script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" > ~/key && chmod 600 ~/key
    - ssh-add ~/key
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    - go get -u github.com/kardianos/govendor
    - mkdir -p $GOPATH/src/github.com/junte/junte-api
    - mv * $GOPATH/src/github.com/junte/junte-api
    - cd $GOPATH/src/github.com/junte/junte-api
    - govendor sync
    - go build -o junte-api
    - cd -
    - cp $GOPATH/src/github.com/junte/junte-api .

build_image:
  image: docker:latest
  stage: build_image
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE

deploy-dev:
  stage: deploy
  image: junte/ssh-agent
  variables:
    # should be set up at Gitlab CI env vars
    SSH_PRIVATE_KEY: $SSH_DEV_PRIVATE_KEY
  script:
    # copy docker-compose yml to server
    - scp docker-compose.dev.yml root@SERVER_IP:/home/junte/junte-api/
    # login to gitlab registry       
    - ssh root@SERVER_IP docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    # then we cd to folder with docker-compose, run docker-compose pull to update images, and run services with `docker-compose up -d`
    - ssh root@SERVER_IP "cd /home/junte/junte-api/ && docker-compose -f docker-compose.dev.yml pull api-dev && HOME=/home/dev docker-compose -f docker-compose.dev.yml up -d"
  environment:
      name: dev
  only:
    - dev

You also need Gitlab runner with Docker support. How install it look at in Gitlab doc, please.

About stages:

  • build - just change it to build what you need
  • build_image - very simple, just login to gitlab registry, build new image and push it to registry. Look at cache part, it need to cache files between stages and can be different for you.
  • deploy-dev - that part more about what you asked. Here first 6 commands just install ssh and create your private key file to have access to your VPS. Just copy it and add your SSH_PRIVATE_KEY to secret vars in Gitlab UI. Last 3 SSH commands more interesting for you.
like image 175
Vitaly Velikodny Avatar answered Oct 02 '22 12:10

Vitaly Velikodny