CI/CD via GitLab and a Runner in Containers?

What I learned about configuring a GitLab Server with a Runner using Docker
Containers
It is important to note  this document is for Proof of Concept testing. IT IS
NOT  meant as a go to for Production environments as the use of SSL (HTTPS) is
not spoken of in this article.

Containers
I fully support Containers and containerization of services. I agree with the
portability viewpoint, but for me it is more of keeping a "clean" system. The
fewer services installed the fewer conflicts to work out and the fewer updates I
must worry about conflicting with installed services and so on. Next there is
the issue of bundled configurations minimizing conflicts of packages and updates
allowing less of an oppotunity of downtime while forced to troubleshoot a
package update. Although building containers on the fly does not protect you
from this. To truly appreciate this means of minimizing conflicts it would be
best to consider hosting your own Docker Hub or publish your containers
publicly.

What I tried
Initially I was trying to run a Container on my notebook with a Runner in an AWS
EC2. This will NOT work as the runner must be able to communicate with the
GitLab Server via an IP or FQDN. Next I tried running both the GitLab CE Server
and a Runner in the same EC2. In doing so I found a t2.micro is insufficient for
this purpose, although a t2.large is capbable of handling a small learning
configuration.

As I support IaC (Infrastructure as Code)
I built a Terraform Module that creates the AWS Environment and necessary
resources:
Terraform code used to create the AWS Environment is available here: GitHub
[URL].

Once you have your VM running with Docker installed, this User Data will
establish your GitLab-CE Container. Once completed you can access your new
GitLab-CE environment via http://your_domain.tld/. Be sure to append the URL
with your custom port number if you elect to use a port other than 80.

#!/bin/bash

apt-get update -y
apt-get install -y awscli unzip

Install Docker

curl -sSL https://get.docker.com/ | sh
systemctl enable docker; systemctl start docker

######### GITLAB SERVER
HOSTNAME=$(curl http://169.254.169.254/latest/meta-data/public-hostname)
docker run --detach
--hostname "$HOSTNAME"
--publish 80:80 --publish 2289:22
--name gitlab
--restart always
--volume ~/Library/Docker/gitlab/config:/etc/gitlab
--volume ~/Library/Docker/gitlab/logs:/var/log/gitlab
--volume ~/Library/Docker/gitlab/data:/var/opt/gitlab
gitlab/gitlab-ce:latest

######### GITLAB RUNNER
mkdir -p /srv/gitlab-runner/config
docker run -d --name gitlab-runner --restart always
-v /srv/gitlab-runner/config:/etc/gitlab-runner
-v /var/run/docker.sock:/var/run/docker.sock
gitlab/gitlab-runner:latest

This next step is not automated as I did not take time to figure out if an API
exists to extract this info, et al.
Following the deployment of the EC2, you will need to associate the Runner with
the GitLab Server:

HOSTNAME=$(curl http://169.254.169.254/latest/meta-data/public-hostname)
TOKEN=%% get from your gitlab server %%
docker run --rm -t -i -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
--non-interactive
--executor "docker"
--docker-image ubuntu:18.04
--url "%% URL TO YOUR SERVER%%"
--registration-token "${TOKEN}"
--description "%% YOUR RUNNER DESCRIPTION%%"
--tag-list "test,AWS"
--run-untagged
--locked="false"

  1. Be sure to set your --executor  accordingly docs
    [https://docs.gitlab.com/runner/executors/README.html]. I chose Docker.
  2. Set the --docker-image  you will use each time your runner executes. Be sure
    to plan this accordingly. If you chose to use Docker for your Runners and
    you set Alpine here as the distro you will need to make sure your runners
    are configured to use Alpine packages, etc.
  3. Set the --url  to point to your server. E.g. http://gitlab.mydomain.com.
    This is documented in your GitLab-CE Server. Login to your GitLab-CE server.
    On the lower left click: Settings | CI/CD | Runners. Scroll down "Specific
    Runners" and you will find the URL.
  4. Set the --registration-token. You will need to access your GitLab-CE Server
    to obtain this. Login to your GitLab-CE server. On the lower left click:
    Settings | CI/CD | Runners. Scroll down "Specific Runners" and you will find
    the registration token shown below the URL.
  5. Set the --description  to something meaningful. This will help you
    distinguish this runner from your other runners as you create them in the
    future.
  6. Set the --tag-list  with tags that will be used to kick off this runner
    process.