<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hanzel Jesheen</title>
    <description>The latest articles on DEV Community by Hanzel Jesheen (@hanzeljesheen).</description>
    <link>https://dev.to/hanzeljesheen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2987%2F2674768.jpeg</url>
      <title>DEV Community: Hanzel Jesheen</title>
      <link>https://dev.to/hanzeljesheen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hanzeljesheen"/>
    <language>en</language>
    <item>
      <title>Setup GitLab for Docker based development</title>
      <dc:creator>Hanzel Jesheen</dc:creator>
      <pubDate>Mon, 13 Feb 2017 13:38:05 +0000</pubDate>
      <link>https://dev.to/hanzeljesheen/setup-gitlab-for-docker-based-development</link>
      <guid>https://dev.to/hanzeljesheen/setup-gitlab-for-docker-based-development</guid>
      <description>&lt;p&gt;&lt;a href="https://about.gitlab.com/"&gt;Gitlab&lt;/a&gt; is known as the open-source git repository manager. However, Gitlab does a lot more than that right now. It features a really powerful CI/CD engine and even packs a docker registry. It has become an essential part of my development process. In this article, we will discuss setting up Gitlab for docker based developments.&lt;/p&gt;

&lt;p&gt;We will start by setting up a VM in the cloud and installing Gitlab there. We will configure it to support HTTPS. We will also setup &lt;a href="https://about.gitlab.com/gitlab-ci/"&gt;Gitlab CI&lt;/a&gt;, the Continuous integration solution that comes with Gitlab with multiple runners to run the builds in parallel. We will also setup the docker registry to store our docker images and secure it with HTTPs. Finally we will test this setup with docker based project.&lt;/p&gt;

&lt;p&gt;For the cloud VM, I'm using &lt;a href="https://www.digitalocean.com/"&gt;Digital Ocean&lt;/a&gt;, but the process is similar for other vendors as well. We will use &lt;a href="https://letsencrypt.org/"&gt;Let's Encrypt&lt;/a&gt; for generating SSL certificates. To use this, you would also need a domain. The project used to test the gitlab setup is hosted &lt;a href="https://github.com/botleg/gitlab-nginx"&gt;here&lt;/a&gt;. It is simple project that build a docker image with &lt;a href="https://www.nginx.com/"&gt;nginx&lt;/a&gt; webserver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create the VM
&lt;/h2&gt;

&lt;p&gt;Create a VM using your cloud service, I am using Digital Ocean. You may also host Gitlab in your local system. I am choosing &lt;code&gt;Ubuntu 16.04 x64&lt;/code&gt; as the OS, but any linux distro will work fine. Also, since we are putting the entire setup in one VM, make it with atleast 8GB of RAM.&lt;/p&gt;

&lt;p&gt;We will register this Gitlab instance with a subdomain. Example: &lt;code&gt;gitlab.botleg.com&lt;/code&gt;. Once you have created your created your VM, create a new &lt;code&gt;A&lt;/code&gt; record in your DNS provider. The name will be subdomain name, &lt;code&gt;gitlab&lt;/code&gt; here and provide the public IP of the VM. SSH into the VM created and create a new user. Follow &lt;a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04"&gt;this&lt;/a&gt; tutorial to add this user and setup password-less access.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Gitlab
&lt;/h2&gt;

&lt;p&gt;We will be using the Community Edition of Gitlab. All the features that we need in our development process is supported in this edition. A comparison of the editions can be found &lt;a href="https://about.gitlab.com/products/#compare-options"&gt;here&lt;/a&gt;. Easiest way to install Gitlab is to use the Omnibus package. It contains everything required for Gitlab in a single package with &lt;a href="https://www.chef.io/"&gt;Chef&lt;/a&gt; recipes for the tasks.&lt;/p&gt;

&lt;p&gt;Just follow the steps in &lt;a href="https://about.gitlab.com/downloads/#ubuntu1604"&gt;this&lt;/a&gt; site to install it. You just have to install some dependencies, add the new repository and install the &lt;code&gt;gitlab-ce&lt;/code&gt; package. The command &lt;code&gt;sudo gitlab-ctl reconfigure&lt;/code&gt; triggers a chef receipe that reconfigures Gitlab based on the changes made to the configuration files.&lt;/p&gt;

&lt;p&gt;Once this is done, you will able to go to the domain setup in DNS service and see Gitlab login page. You will be asked to set password for the default admin account named &lt;code&gt;root&lt;/code&gt;. Set the admin password and login to Gitlab. Now, I recommend that you create a new admin user and use it for the next steps. This can be done from the Admin Area.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enabling HTTPS
&lt;/h2&gt;

&lt;p&gt;To enable HTTPS for Gitlab, we need to have a certificate generated for this domain. We will use &lt;code&gt;Let's Encrypt&lt;/code&gt; for this. To know more about this, visit &lt;a href="https://letsencrypt.org/getting-started/"&gt;here&lt;/a&gt;. We will start by installing &lt;code&gt;Let's Encrypt&lt;/code&gt; with,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;letsencrypt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create the certificate, we need to verify that we indeed own the domain. To do this, &lt;code&gt;letencrypt&lt;/code&gt; tool provide a plugin &lt;code&gt;standalone&lt;/code&gt; that creates a temporary web server and verifies the domain. So, for this to work, we need to temporarily disable gitlab. To do this, use the following commands and enter the domain name and email when prompted&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;gitlab-ctl stop
&lt;span class="nb"&gt;sudo &lt;/span&gt;letsencrypt certonly &lt;span class="nt"&gt;--standalone&lt;/span&gt; &lt;span class="nt"&gt;--agree-tos&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;gitlab-ctl start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The certificates generated will be in the folder &lt;code&gt;/etc/letsencrypt/live/&amp;lt;domain name&amp;gt;/&lt;/code&gt;. The certificate file is &lt;code&gt;fullchain.pem&lt;/code&gt; and the key is &lt;code&gt;privkey.pem&lt;/code&gt;. We need to change this is in the Gitlab configurations. This file is located at &lt;code&gt;/etc/gitlab/gitlab.rb&lt;/code&gt;. This file is full of comments. We will make a backup of this file and clear it,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo cp&lt;/span&gt; /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.bak
&lt;span class="nb"&gt;sudo truncate&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; 0 /etc/gitlab/gitlab.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to add the following configuration items:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;external_url&lt;/code&gt;: The URL  where gitlab can be accessed. It will contain &lt;code&gt;https://&lt;/code&gt; followed by the domain name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nginx['redirect_http_to_https']&lt;/code&gt;: We set it to &lt;code&gt;true&lt;/code&gt; to redirect all HTTP traffic to HTTPS.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nginx['ssl_certificate']&lt;/code&gt;: The location of the certificate file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nginx['ssl_certificate_key']&lt;/code&gt;: The location of the certificate key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Open the file &lt;code&gt;/etc/gitlab/gitlab.rb&lt;/code&gt; as root and enter the following lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;external_url &lt;span class="s1"&gt;'https://&amp;lt;domain name&amp;gt;'&lt;/span&gt;
nginx[&lt;span class="s1"&gt;'redirect_http_to_https'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true

&lt;/span&gt;nginx[&lt;span class="s1"&gt;'ssl_certificate'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"/etc/letsencrypt/live/&amp;lt;domain name&amp;gt;/fullchain.pem"&lt;/span&gt;
nginx[&lt;span class="s1"&gt;'ssl_certificate_key'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"/etc/letsencrypt/live/&amp;lt;domain name&amp;gt;/privkey.pem"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;&amp;lt;domain name&amp;gt;&lt;/code&gt; with the domain name registered for Gitlab. Reconfigure Gitlab with new settings with the command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;gitlab-ctl reconfigure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when we go the Gitlab site, it will be in HTTPS with the &lt;code&gt;Let's Encrypt&lt;/code&gt; certificate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gitlab CI Runners
&lt;/h2&gt;

&lt;p&gt;In this step, we will setup the runners needed to run the builds for CI/CD. The runners will build, test and publish the projects as defined by the &lt;code&gt;.gitlab-ci.yml&lt;/code&gt; file. We will discuss more about this later. Since we are doing docker based projects, we will use docker images for runners and use &lt;code&gt;Docker inside Docker&lt;/code&gt; for running our builds.&lt;/p&gt;

&lt;p&gt;The first step for this would be to actually install docker engine in this server,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-sSL&lt;/span&gt; https://get.docker.com/ | sh
&lt;span class="nb"&gt;sudo &lt;/span&gt;usermod &lt;span class="nt"&gt;-aG&lt;/span&gt; docker &lt;span class="nv"&gt;$USER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second command is to access docker without &lt;code&gt;sudo&lt;/code&gt;. You might need to logout and login again to do this.Once we have the docker engine installed, we will run the &lt;a href="https://hub.docker.com/r/gitlab/gitlab-runner/"&gt;Gitlab CI Multi Runner&lt;/a&gt; image. We will set the image to always restart if it goes down. With this implementation, we will share the docker engine in the server with the runner. For this, we will create a volume at the location, &lt;code&gt;/var/run/docker.sock&lt;/code&gt;. To do all this, use the following command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; runner1 &lt;span class="nt"&gt;--restart&lt;/span&gt; always &lt;span class="nt"&gt;-v&lt;/span&gt; /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;runner1&lt;/code&gt; is the name of this runner. You can run multiple runners to run the builds in parallel. To run more runners, run the previous command by changing the name &lt;code&gt;runner1&lt;/code&gt; to something else. Example: &lt;code&gt;docker run -d --name runner2 --restart always -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest&lt;/code&gt;, and so on.&lt;/p&gt;

&lt;p&gt;To register these runners to Gitlab service, you need to have the registration token. You can find it in the &lt;code&gt;Runners&lt;/code&gt; section of the Admin Area. Once you find this, run the following command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; runner1 gitlab-runner register
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will ask the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;gitlab-ci coordinator URL&lt;/code&gt;: It is the domain name of the gitlab service with &lt;code&gt;https://&lt;/code&gt;, Example: &lt;code&gt;https://gitlab.botleg.com&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;gitlab-ci token&lt;/code&gt;: Enter the registration token that we got from Gitlab site.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;description&lt;/code&gt;: Give a name from this runner. Example: &lt;code&gt;runner1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tags&lt;/code&gt;: Give some tags to target this runner. Gitlab provide the option to run the tests on runner with certain tags.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;run untagged builds&lt;/code&gt;: Boolean value that tell whether this runner can accept jobs without tags. Since we have only one type of runners here, we can provide &lt;code&gt;true&lt;/code&gt; for this.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;executor&lt;/code&gt;: How the jobs are executed by the runner. Here, we choose &lt;code&gt;docker&lt;/code&gt;. To more about the other executors, check &lt;a href="https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/executors/README.md"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;default Docker image&lt;/code&gt;: The docker image to run the tests in. We will user &lt;code&gt;docker:git&lt;/code&gt; for this. This image allows for &lt;code&gt;docker in docker&lt;/code&gt; and also has git inbuilt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Repeat this command for all your runners by changing the runner name. Example: &lt;code&gt;docker exec -it runner2 gitlab-runner register&lt;/code&gt;. After you do this, you will be able to see these runners in the &lt;code&gt;Runners&lt;/code&gt; section of Gitlab's Admin Area.&lt;/p&gt;

&lt;p&gt;There is however one more step we need to do. We are sharing the docker engine in the server to the runners. These runners will create &lt;code&gt;docker:git&lt;/code&gt; image to run the jobs. These jobs build and push docker images. We will use the docker engine from the server to do this. So, we need to make a docker volume of &lt;code&gt;/var/run/docker.sock&lt;/code&gt; for the &lt;code&gt;docker:git&lt;/code&gt; images created by the runners to share the docker engine. To do this, we need to modify the configuration of each runner. Run the following command to open up the configuration file of the runner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; runner1 nano /etc/gitlab-runner/config.toml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit the line &lt;code&gt;volumes = ["/cache"]&lt;/code&gt; to &lt;code&gt;volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"]&lt;/code&gt;. The file will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;concurrent &lt;span class="o"&gt;=&lt;/span&gt; 1
check_interval &lt;span class="o"&gt;=&lt;/span&gt; 0

&lt;span class="o"&gt;[[&lt;/span&gt;runners]]
  name &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"runner1"&lt;/span&gt;
  url &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://gitlab.botleg.com"&lt;/span&gt;
  token &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"9ab32be14c9d2cb67fbec7aa59304f"&lt;/span&gt;
  executor &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"docker"&lt;/span&gt;
  &lt;span class="o"&gt;[&lt;/span&gt;runners.docker]
    tls_verify &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false
    &lt;/span&gt;image &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"docker:git"&lt;/span&gt;
    privileged &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false
    &lt;/span&gt;disable_cache &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false
    &lt;/span&gt;volumes &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/cache"&lt;/span&gt;, &lt;span class="s2"&gt;"/var/run/docker.sock:/var/run/docker.sock"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;[&lt;/span&gt;runners.cache]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Repeat this step for other runners as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Container Registry
&lt;/h2&gt;

&lt;p&gt;The final thing we need to setup is the &lt;a href="https://docs.gitlab.com/ce/user/project/container_registry.html"&gt;Container Registry&lt;/a&gt;. We will create a new sub-domain for registry, like &lt;code&gt;registry.botleg.com&lt;/code&gt; and secure it using HTTPS. So, first thing would be to create a new &lt;code&gt;A&lt;/code&gt; record with the DNS service. In this case, the name will be &lt;code&gt;registry&lt;/code&gt; and give the public IP of the VM. Now, our server has &lt;code&gt;nginx&lt;/code&gt; webserver. If the request is for &lt;code&gt;gitlab&lt;/code&gt; subdomain, it will redirect to gitlab and if the request is for &lt;code&gt;registry&lt;/code&gt;, it will redirect to registry.&lt;/p&gt;

&lt;p&gt;We also need to create new SSL certificate for this &lt;code&gt;registry&lt;/code&gt; sub-domain. As before, we use Let's Encrypt for this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;gitlab-ctl stop
&lt;span class="nb"&gt;sudo &lt;/span&gt;letsencrypt certonly &lt;span class="nt"&gt;--standalone&lt;/span&gt; &lt;span class="nt"&gt;--agree-tos&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;gitlab-ctl start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Provide the domain for the registry when prompted. This will create the new SSL certificates, which can be found in the &lt;code&gt;/etc/letsencrypt/live&lt;/code&gt; folder. Update the gitlab configuration file &lt;code&gt;/etc/gitlab/gitlab.rb&lt;/code&gt; to add the following lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;registry_external_url &lt;span class="s1"&gt;'https://&amp;lt;domain name&amp;gt;'&lt;/span&gt;
registry_nginx[&lt;span class="s1"&gt;'ssl_certificate'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"/etc/letsencrypt/live/&amp;lt;domain name&amp;gt;/fullchain.pem"&lt;/span&gt;
registry_nginx[&lt;span class="s1"&gt;'ssl_certificate_key'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"/etc/letsencrypt/live/&amp;lt;domain name&amp;gt;/privkey.pem"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;&amp;lt;domain name&amp;gt;&lt;/code&gt; with the domain registered for the registry. This will enable registry with HTTPS enabled. To update the changes, use the &lt;code&gt;sudo gitlab-ctl reconfigure&lt;/code&gt; command. In the Admin area you can see that the Container Registry is enabled. To test this out, try logging into the registry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker login &amp;lt;registry domain&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;&amp;lt;registry domain&amp;gt;&lt;/code&gt; with the domain registered for the registry and provide the Gitlab username and password. You can see the &lt;code&gt;Login Succeeded&lt;/code&gt; message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing Setup
&lt;/h2&gt;

&lt;p&gt;We have now setup the Gitlab for the docker based development. To test this implementation, we will push a git repository to Gitlab and see the working of Gitlab CI and Container Registry. I have made a simple docker based project which can be found &lt;a href="https://github.com/botleg/gitlab-nginx"&gt;here&lt;/a&gt;. This just contains a &lt;code&gt;Dockerfile&lt;/code&gt; that installs &lt;code&gt;nginx&lt;/code&gt; websever, which serves the &lt;code&gt;index.html&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;The file that we are interested about is &lt;code&gt;.gitlab-ci.yml&lt;/code&gt;. This contains all the information on how to build and deploy this project. To know more about this file, check &lt;a href="https://docs.gitlab.com/ce/ci/yaml/"&gt;here&lt;/a&gt;. The file for this project looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;image: docker:git

stages:
- build
- publish

build:
  stage: build
  script:
    - docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nv"&gt;$REGISTRY_HOST&lt;/span&gt;/&lt;span class="nv"&gt;$IMAGE_NAME&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;

registry:
  stage: publish
  script:
    - docker login &lt;span class="nt"&gt;-u&lt;/span&gt; gitlab-ci-token &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;$CI_BUILD_TOKEN&lt;/span&gt; &lt;span class="nv"&gt;$REGISTRY_HOST&lt;/span&gt;
    - docker push &lt;span class="nv"&gt;$REGISTRY_HOST&lt;/span&gt;/&lt;span class="nv"&gt;$IMAGE_NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the job is docker based, we will specify the image to run this test on. The image, &lt;code&gt;docker:git&lt;/code&gt; contains &lt;code&gt;docker&lt;/code&gt; and &lt;code&gt;git&lt;/code&gt; inside. We can split the entire process into multiple stages and each stage contains multiple jobs. Each job is a stage will be done in parallel and each stage will be triggered only if all jobs in the previous stage is successful.&lt;/p&gt;

&lt;p&gt;Here, we have two stages, &lt;code&gt;build&lt;/code&gt; and &lt;code&gt;publish&lt;/code&gt;. The job &lt;code&gt;build&lt;/code&gt; is in the &lt;code&gt;build&lt;/code&gt; stage and job &lt;code&gt;registry&lt;/code&gt; in the &lt;code&gt;publish&lt;/code&gt; stage. So, &lt;code&gt;registry&lt;/code&gt; job will be done only if the &lt;code&gt;build&lt;/code&gt; job is successful. The &lt;code&gt;build&lt;/code&gt; job contains a bash command to build the docker image. We can use variables in this script by prepending it with &lt;code&gt;$&lt;/code&gt;. The values for this variables can be given from the Gitlab UI. The image name will be &lt;code&gt;$REGISTRY_HOST/$IMAGE_NAME&lt;/code&gt;. Here, &lt;code&gt;$REGISTRY_HOST&lt;/code&gt; is the registry domain name and &lt;code&gt;$IMAGE_NAME&lt;/code&gt; will be the repository name.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;registry&lt;/code&gt; job, we push this image to the container registry. To do this, we have to login to the registry. Gitlab has a temporary user named &lt;code&gt;gitlab-ci-token&lt;/code&gt; with password in the variable &lt;code&gt;$CI_BUILD_TOKEN&lt;/code&gt; for this purpose. Once with login to the registry at &lt;code&gt;$REGISTRY_HOST&lt;/code&gt;, we can push the docker image.&lt;/p&gt;

&lt;p&gt;To test this out, create a new public project in Gitlab by importing from Github. In the settings, choose &lt;code&gt;Variables&lt;/code&gt; and add the following two variables.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;REGISTRY_HOST&lt;/code&gt;: The domain registered for container registry. Example: &lt;code&gt;registry.botleg.com&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IMAGE_NAME&lt;/code&gt;: The repository name for the project. Example: &lt;code&gt;botleg/gitlab-nginx&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the repository is imported, goto the &lt;code&gt;Pipelines&lt;/code&gt; tab and click the &lt;code&gt;Run Pipeline&lt;/code&gt; button to trigger the build. You can see the jobs running and image being pushed to the registry. Once the build pipeline is complete, you can see the docker image in the &lt;code&gt;Registry&lt;/code&gt; section of the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Certificate Renewal
&lt;/h2&gt;

&lt;p&gt;The certificates generated by Let's Encrypt will get expired in 90 days. So, we have to automate the renewal of these certificates. Let's Encrypt provide a command &lt;code&gt;letsencrypt renew&lt;/code&gt; to do just this. This command will check if the certificates are about to be expired and do the renewal for those. For the renewal to happen, we need to stop the Gitlab service temporarily.&lt;/p&gt;

&lt;p&gt;The following commands will do the renewal of the certificates,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gitlab-ctl stop
letsencrypt renew
gitlab-ctl start
gitlab-ctl reconfigure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to add this as a cron job for the &lt;code&gt;root&lt;/code&gt; user. Open the crontab for &lt;code&gt;root&lt;/code&gt; user with the command &lt;code&gt;sudo crontab -e&lt;/code&gt; and paste the following line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0 7 1 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;gitlab-ctl stop &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; letsencrypt renew &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; gitlab-ctl start &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; gitlab-ctl reconfigure&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /var/log/report.log 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will cause this task to run at 7am on the 1st of every month and log the output to the file &lt;code&gt;/var/log/report.log&lt;/code&gt;. Now, we have completely setup Gitlab for the docker based development and also tested it with a git repo.&lt;/p&gt;

</description>
      <category>gitlab</category>
      <category>letsencrypt</category>
      <category>docker</category>
      <category>digitalocean</category>
    </item>
  </channel>
</rss>
