GitLab-CIのRunnerでDockerのローカルイメージを使う

Runnerの設定がデフォルトのままだと、ジョブが失敗してしまいました。

Runnerを新しく作るたびにこの設定を忘れていてハマるのでメモを残しておきます。

## RunnerにDockerを使わせる

ローカル環境にこんなDockerイメージがあるとして、

```
$ docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
android                             sdk28               ************        2 weeks ago         4.7GB
```

Dockerを使うRunnerを新規作成します。

```
$ gitlab-runner register
WARNING: Running in user-mode.                     
WARNING: Use sudo for system-mode:                 
WARNING: $ sudo gitlab-runner...                   
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.examle.com/
Please enter the gitlab-ci token for this runner:
TOKEN_OF_SOME_REPOSITORY
Please enter the gitlab-ci description for this runner:
NAME_OF_SDK28_RUNNER
Please enter the gitlab-ci tags for this runner (comma separated):
TAG_OF_SDK28_RUNNER
Registering runner... succeeded                     runner=********
Please enter the executor: parallels, docker+machine, docker-ssh+machine, kubernetes, docker, docker-ssh, shell, ssh, virtualbox:
docker
Please enter the default Docker image (e.g. ruby:2.1):
android:sdk28
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
```

こんな感じでCI用のYAMLを書いて...

```yaml:.gitlab-ci.yml
image: android:sdk28  # <-- これ

stages:
  - sample_stage

sample_job:
  stage: sample_stage
  script:
    - echo "SAMPLE"
  tags:
    - TAG_OF_SDK28_RUNNER
```

GitLabにpushします。

```
$ git push
```

すると...

```
Running with gitlab-runner 11.3.1~beta.4.g0aa5179e (0aa5179e)
  on NAME_OF_SDK28_RUNNER 32b12b55
Using Docker executor with image android:sdk28 ...
Pulling docker image android:sdk28 ...
ERROR: Job failed: Error response from daemon: pull access denied for android, repository does not exist or may require 'docker login' (executor_docker.go:168:2s)
```

ダメでした :exclamation: :confounded: 

どうやら [Docker Hub](https://hub.docker.com/) からコンテナイメージをダウンロードしようとしているみたいです。

## Runnerに"ローカルの"Dockerイメージを使わせる

そこで、Runnerの設定ファイルに`pull_policy = "if-not-present"`というのを追加します。

```toml:~/.gitlab-runner/config.toml
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "NAME_OF_SDK28_RUNNER"
  url = "https://gitlab.example.com/"
  token = "******************************"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "android:sdk28"
    privileged = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    pull_policy = "if-not-present"  # <-- これ
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
```

そしてジョブをリトライしてみると...

```
Running with gitlab-runner 11.3.1~beta.4.g0aa5179e (0aa5179e)
  on NAME_OF_SDK28_RUNNER 32b12b55
Using Docker executor with image android:sdk28 ...
Using locally found image version due to if-not-present pull policy
Using docker image sha256:e725ea60f228e9cddff3f3fde1b92bcc9dec4fdf4484680700bcc1cc7412d95d for android:sdk28 ...
Running on runner-32b12b55-project-225-concurrent-0 via localhost.local...
Cloning repository...
Cloning into '/builds/foo/ci-sample'...
Checking out 78082e84 as master...
Skipping Git submodules setup
$ echo "SAMPLE"
SAMPLE
Job succeeded
```

OKになりました :star: 

## ここに書いてありました

Using a private container registry:
https://docs.gitlab.com/runner/configuration/advanced-configuration.html#using-a-private-container-registry

もっと目につく所に書いておいて欲しいですね :droplet: