Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thomaskanzig/9396c189225cfc11f810064e1ded3f1e to your computer and use it in GitHub Desktop.
Save thomaskanzig/9396c189225cfc11f810064e1ded3f1e to your computer and use it in GitHub Desktop.
Configure GitLab CI Runner with Docker executor using AWS EC2

Configure GitLab CI Runner with Docker executor using AWS EC2

In this tutorial you will see how you can create you gitlab-ci.yml file in your repository and execute jobs on gitlab pipelines.

1. Create a VM on AWS EC2

  • In AWS, create an instance and choose the Amazon Linux 2 AMI machine.
  • Create a new key pair with the name similar like gitlab-runner. Isn't needed to be the same, just a name so you can identfy the purpose.
    • The key pair type can be ED25519

2. Install dependencies (Git, Docker)

  • Login to the server machine with ssh ec2-user@... via terminal;
  • Update dependencies:
sudo yum -y update
  • Install Git:
sudo yum install -y git

Install Docker:

sudo amazon-linux-extras install -y docker
  • Add the ec2-user user to the docker group:
sudo usermod -aG docker ec2-user
  • Ensure the Docker daemon starts automatically:
sudo systemctl enable docker
  • Restart your EC2 instance:
sudo reboot

You will desconnect to the server. Please reconnect!

  • After reconnect, make sure docker was connected automatically. To check that execute this command:
docker run hello-world

You will see something like this:

image

  • Download the GitLab Runner installation package:
curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm"
  • Install the GitLab Runner:
sudo rpm -i gitlab-runner_amd64.rpm

At the end of the installation, you will see something like this:

image

  • In group or project repository disable shared runners:
    Settings > CI/CD > Runners

image

  • Register the GitLab Runner:
sudo gitlab-runner register

Now, you must answer some questions. The URL and registration token, you can find them by in Settings > CI/CD > Runners:

image

The other questions, please check here to fill the same answers:

image

  • Check if gitlab runner is running properly:
sudo gitlab-runner status

Output:

gitlab-runner: Service is running

In Settings > CI/CD > Runners, check if Available runners is displaying:

image

3. Test the GitLab Runner

Now, we can add our first gitlab-ci.yml configuration. Below we have a quickly sample script to put in the root folder of our repository project and we can check if it's everything works.

stages:
  - build
  - test
  - deploy

Build:
  stage: build
  script:
    - echo "Building is running."

Test:
  stage: test
  script:
    - echo "Testing is running."
    
Deploy:
  stage: deploy
  script:
    - echo "Deploying is running."  

4. Enable the privileged mode

We need to give docker some priviliges to avoid future issues. Go to the

  • Login to the server machine with ssh ec2-user@... via terminal.
  • Edit the config.toml file:
sudo vim /etc/gitlab-runner/config.toml

Inside of config.toml file please replace:

privileged = false

to:

privileged = true
  • Restart gitlab runner and check the status if it's everything works:
sudo gitlab-runner restart
sudo gitlab-runner status

Output should be:

gitlab-runner: Service is running

5. Conclusion

That's it guys.
I hope this helps you to unterstand how this works and you can finally build your first pipeline in your project.

6. Additional

You can follow all steps on this YouTube Video as well.

Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment