Docker Community Forums

Share and learn in the Docker community.

How to deploy an image from the gitlab container registry to a directory on a webserver


(Joergklein) #1

I wrote a .gitlab-ci.yml to build, test, release and deploy an image. The build, test and release works fine on GitLab. My question is, how to deploy and run the image on the web server? Git and an apache web server are installed. It is not possible to install docker on this server. The jupyterhub should work in an isolated environment and should run in a sub domain. Is rsync the right tool?

Please check the deploy stage. Is the idea right?

.gitlab-ci.yml

image: docker:latest

variables:
  DOCKER_DRIVER: overlay2
  CONTAINER_IMAGE: registry.gitlab.com/joklein
  DOCKER_IMAGE: jupyterhub
  TAG: 0.1.0

services:
  - docker:dind

stages:
  - build
  - test
  - release
  - deploy

before_script:
  - apk update && apk add --no-cache git openssh-client rsync
  - echo "$GITLAB_PASSWORD" | docker login registry.gitlab.com --username $GITLAB_USER --password-stdin

build:
  stage: build
  script:
    - docker build -t $CONTAINER_IMAGE/$DOCKER_IMAGE .
    - docker push $CONTAINER_IMAGE/$DOCKER_IMAGE

test:
  stage: test
  script:
    - docker pull $CONTAINER_IMAGE/$DOCKER_IMAGE
    - docker run -d -p 8000:8000 --name jupyterhub $CONTAINER_IMAGE/$DOCKER_IMAGE jupyterhub

release:
  stage: release
  script:
    - docker pull $CONTAINER_IMAGE/$DOCKER_IMAGE
    - docker tag  $CONTAINER_IMAGE/$DOCKER_IMAGE:latest $CONTAINER_IMAGE/$DOCKER_IMAGE:$TAG
    - docker push $CONTAINER_IMAGE/$DOCKER_IMAGE:$TAG

deploy:
  stage: deploy
  script:
    - docker pull $CONTAINER_IMAGE/$DOCKER_IMAGE
    - mkdir .public
    - cp -r * .public
    - mv .public public
    - ls -la public
    - mkdir "${HOME}/.ssh"
    - echo "${SSH_HOST_KEY}" > "${HOME}/.ssh/known_hosts"
    - echo "${SSH_PRIVATE_KEY}" > "${HOME}/.ssh/id_rsa"
    - chmod 700 "${HOME}/.ssh/id_rsa"
    - rsync -hrvz --delete --exclude=_ public/ user@example.com:www/jupyter/
    - docker run -d -p 8000:8000 --name jupyterhub $CONTAINER_IMAGE/$DOCKER_IMAGE jupyterhub
  only:
    - master