GitLab CI review enviroment with docker

So environment deploy on feature branch for review before merge is great option. I have 10+ projects with similar structure and same docker image. There is gulp for front-end and php framework for backend.
I made a little changes in .gitlab-ci.yml:

deploy:review:
  stage: deploy
  script:
    - rsync -av --delete httpdocs/ /home/gitlab-runner/dev_env/$CI_BUILD_REF_NAME.$CI_PROJECT_NAME.$CI_PROJECT_NAMESPACE
  environment:
    name: review/$CI_BUILD_REF_NAME
    url: http://$CI_BUILD_REF_NAME.$CI_PROJECT_NAME.$CI_PROJECT_NAMESPACE.env.mydomain.com
  only:
    - branches
  except:
    - master

And i configured nginx for loading folder for every subdomain. Thats work great for reviewing fronted part.
But i want to use docker for backend part. So i need somehow to start docker on same server where gitlab is. Then i need to hold all port generation and maybe nginx redirect routine. And last part, after merge or after few days i need to romove review docker.

  • CentOS7: Are you trying to connect to a TLS-enabled daemon without TLS?
  • OS Container vs Application Container
  • How to restrict environment variables passed to linked containers
  • Docker Java Image cannot connect to Docker Mongo
  • Docker stops immediately upon running - EOF error
  • Backup running postgres docker container
  • I believe this https://github.com/jwilder/nginx-proxy can help me control ports, but i need to generate them (using CI build id as port seems to be buggy, one day i’ll have build #65536) and still question about auto start/stop/destroy containers.

    May be there is some opensource docker cloudsolution for standalone server?

  • Docker stats 100% memory
  • How to set run arguments when using Ansible to deploy docker?
  • Docker db container running. Another process with pid <id> is using unix socket file
  • docker generates “fatal: write error: Read-only file” using git clone
  • Docker bootstrap unix sock is missing when reboot
  • How to set bash aliases for docker containers in Dockerfile?
  • One Solution collect form web for “GitLab CI review enviroment with docker”

    First part solved.

    jwilder/nginx-proxy – totally get ports routine.

    I run proxy on startap:

    docker run -d -p 80:80 -p 443:443 --name "dnginx" -v /root/certs:/etc/nginx/certs -v /var/run/docker.sock:/tmp/docker.sock:ro -v /var/log/nginx-proxy/:/var/log/nginx/ jwilder/nginx-proxy
    

    then run each container like this:

    sudo docker run -d --name "nginx-test" --expose 80 --env VIRTUAL_HOST=test.domain.com kitematic/hello-world-nginx
    

    note VIRTUAL_HOST=test.domain.com and –expose 80

    –env VIRTUAL_HOST=test.domain.com
    will say nginx-proxy wich subdomain to link with container

    –expose 80
    if your container doesn’t exposes port itself. This will open 80 port inside docker network. And nginx-proxy will autogenerate new config, adding part for your subdomain.

    Second part solved:

    I made my docker runner privileged in gitlab runner config.toml. Then my runner job works like this:

    deploy to review:
      image: docker:latest
      tags:
        - privileged
      stage: deploy
      script:
        - mkdir -p /home/devenv/$CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME/project
        - cp -r ./* /home/devenv/$CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME/project
        - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
        - ( docker stop $CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME && docker rm $CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME ) || echo "nothing to stop, starting new container"
        - docker run -d --env VIRTUAL_HOST="$CI_BUILD_REF_NAME.$CI_PROJECT_NAME.$CI_PROJECT_NAMESPACE.mydomain.com" --name "$CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME" -v /home/devenv/$CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME/project:/home/project -v /home/devenv/$CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME/logs:/var/logs myrepo.com:4567/group/projectimage
        - docker exec $CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME-$CI_BUILD_REF_NAME /bin/sh /home/project/build.sh
      environment:
        name: review/$CI_BUILD_REF_NAME
        url: http://$CI_BUILD_REF_NAME.$CI_PROJECT_NAME.$CI_PROJECT_NAMESPACE.mydomain.com
      only:
        - branches
      except:
        - master
    

    Where build.sh is npm+bower install command and gulp tasks.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.