LoginSignup
17
10

More than 3 years have passed since last update.

CircleCIでDocker Hubの制限を回避するための設定

Last updated at Posted at 2020-10-09

問題

Docker Hubは、無料アカウントと認証なしユーザーで、11月からpull数に制限がかかります。CircleCI上で認証無しで運用しているとIPアドレスベースの制限がかかり、CircleCIのIPアドレスはユーザーと比較して多くないので影響が懸念されます。

CircleCIチームはDocker社と会話してる模様なので、今後もしかしたらユーザーの対応は不要となるかも知れません。しかしCI/CD環境が止まれば影響は大きいので回避策を押さえておきましょう。

解決策

ひとまずDocker Hubのユーザー認証を噛ませれば、IPアドレスでは判断されなくなるので無料アカウントでも200 pulls/6hまでは利用できます。これ以上になるならDocker Hubへの課金が必要ですが、どのみちCircleCI上でユーザー認証を行う必要があるので同様の作業が発生します。

作業手順

以下の作業でCircleCI上でのDocker Hubのユーザー認証を行います。
config.yamlの全体は↓こちら。

CircleCIのContextにDocker Hubのユーザー名とパスワードを設定

Contextへの登録手順はすぐ出てくるので割愛します。
image.png

Dockerを利用するJobにContextを追加

2020/09/15から複数のContextがサポートされた模様! これは嬉しい。

  staging_steps: &staging_steps
    <<: *only_master
    context:
      - serverless_staging
      - docker-hub-credencials
  production_steps: &production_steps
    <<: *only_master
    context:
      - serverless_production
      - docker-hub-credencials

Dockerを利用している箇所に認証を加える

Docker executorや、Machine executor内でDockerをpullしている箇所に設定した認証情報を加えてゆきます。
以下はDocker executorの例。

参照用の項目を作成

references:
  docker_hub_authentication: &docker_hub_authentication
    auth:
      username: $DOCKERHUB_USER
      password: $DOCKERHUB_PASSWORD

imageをpullしている箇所全てに適用

以下はexecutorsにまとめられている箇所に適用している例。

executors:
  python3_6_1:
    working_directory: ~/repo
    docker:
      - image: circleci/python:3.6.1
        <<: *docker_hub_authentication
  test:
    working_directory: ~/repo
    docker:
      - image: circleci/python:3.6.1
        <<: *docker_hub_authentication
      - image: bluszcz/bflocalstack-dynamodb-s3
        <<: *docker_hub_authentication
      - image: alismedia/dynamodb-local
        <<: *docker_hub_authentication
        environment:
          MAX_HEAP_SIZE: 4096m
          HEAP_NEWSIZE: 3072m
      # Docker Hubではないので認証情報不要
      - image: docker.elastic.co/elasticsearch/elasticsearch:6.2.0
        environment:
          discovery.type: single-node

以上の設定を行って、変更前と同じようにビルドできれば完了です。

エラー

Docker Hubではない箇所に誤って認証情報を追加すると以下のようなエラーが出ます。

Error response from daemon: Get https://docker.elastic.co/v2/elasticsearch/elasticsearch/manifests/6.2.0: unauthorized: authentication required

以下は謎のエラー。今回初めて見たもの。

Allocating a remote Docker Engine
Requesting version: 18.09.3

Got error while creating host: failed to create host: rpc error: code = Unknown desc = failed to create VM: Put "https://vm-service.infra.circleci.com/tasks/5f7ffcbaa7b0aa6a8ec29c79-0-build%2F59C7C9D1": EOF
We had an unexpected error preparing a VM for this build, potentially due to our infrastructure or cloud provider.  Please retry the build in a few minutes

指示通りにRerunしたら解決しました。

17
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
10