Install and set up multiple Gitlab CI Runners to use Docker

GitLab CI allows you to use Docker Engine to build and test docker-based projects.

We will follow the official guide: Using Docker Build

Here we choose the first method “Use shell executor”.

1.Install GitLab Runner:

Here we install gitlab-ci-multi-runner on linux. Follow this official guide until “Register the runner”: Install using official GitLab repositories

2. Register the runner:

Using Docker Build is trying to register the runner in non-interactive way. Somehow it doesn’t work. Here we follow the registration step in Install using official GitLab repositories, installing in interactive way.

1

As we can see from the above picture, we will need to provide coordinator URL and token. They can be found in your repository at “Settings” -> “Runners”:

1

And one thing different from the screenshot, For “Pleasse enter the executor: shell, docker, docker-ssh, ssh?” We need to type in shell.

 

After registering, we should be able to see the runner in “Settings” -> “Runners”:

1

3. Install docker, set up privileges.

Just continue following Using Docker Build  “Use shell executor” section.

4. Register multiple runners and make them run builds concurrently.

The runner config file is located at:

/etc/gitlab-runner/config.toml

1

The concurrent is default to 1. Here we use 3 concurrent runners, so we set it to 3.

5. Verify whether runners are running or not:

$sudo gitlab-ci-multi-runner verify

Docker – How to create a custom database from docker mysql image

Problem:

I use the official mysql base image from Docker Hub to create mysql database. But by default it only creates one database. I want to create another database.

Solution:

mysql image actually provides a way to do this. Reference the official doc: https://github.com/docker-library/docs/tree/master/mysql  Under “Initializing a fresh instance” section.

So we just need to create a .sql file with create database command, and mount this file as volume to docker-entrypoint-initdb.d folder inside mysql container.

This is my docker-compose.yml file:

version: 2′

services:
  db:
    image: mysql   # Pull mysql image from Docker Hub
    environment:   # Set up mysql database name and password
      MYSQL_DATABASE: development
      MYSQL_ROOT_PASSWORD: password
    ports:    # Set up ports exposed for other containers to connect to
      - "3306"
    volumes:  # Mount relative path source folder on host to absolute path destination folder on docker container
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
  web:
    build: .  # Look in the current directory for a Dockerfile and build images using it
    depends_on:    # Link web container to db container
      - db
    environment:  # Set up environment variables
      MYSQL_DATABASE: development
      MYSQL_ROOT_PASSWORD: password
      RAILS_ENV: docker
    ports:
      - "3000:3000"  # Forwards the exposed port 3000 on the container to port 3000 on the host machine

 

We create init.sql file inside docker-entrypoint-initdb.d folder, and use “volumes” to mount this folder to docker-entrypoint-initdb.d inside mysql container, so that mysql service will have access to all the files in docker-entrypoint-initdb.d folder on host. The content of init.sql file:

CREATE DATABASE test;

After we run

$docker-compose up

It will boot up db and web service. The db service will be a mysql container, which have two databases: develop and test.