2024年11月28日星期四

🐳 GitLab Runner and Docker Daemon Usage Guide

🔧 GitLab Runner Executes CI/CD Jobs

GitLab Runner is the core tool for executing CI/CD jobs. When building or running Docker containers, there are two ways to interact with the Docker daemon:

  1. Shared Docker Daemon on the Host: The Runner directly uses the host's Docker service by mounting the host's docker.sock file.
  2. Using Docker-in-Docker (dind) Service: Runs a standalone Docker daemon container within the job.

🚀 Docker Daemon Configuration Methods

1. Shared Host Docker Daemon

If the Runner job needs to directly use the host's Docker daemon:

  • Mount /var/run/docker.sock to the Runner container.
yaml
---
version: "3.8" services: gitlab-runner: image: gitlab/gitlab-runner:latest volumes: - /var/run/docker.sock:/var/run/docker.sock # Map host Docker daemon - /path/to/config:/etc/gitlab-runner # Runner config directory
  • Also, the executor docker set in gitlab-runner's config.toml needs to mount /var/run/docker.sock.
yaml
---
[runners.docker] tls_verify = false image = "registry.dev.mbpsmartec.co.jp/ant-mvn:ubuntu" privileged = false disable_entrypoint_overwrite = false oom_kill_disable = false disable_cache = false volumes = ["/cache","/var/run/docker.sock:/var/run/docker.sock"] pull_policy = ["if-not-present"] shm_size = 0 network_mtu = 0
  • No need to set services in .gitlab-ci.yml.
  • Example Configuration:

.gitlab-ci.yml

yaml
コピー
image: docker:cli # Use Docker CLI image services: [] # No additional Docker services needed variables: DOCKER_TLS_CERTDIR: "" # Disable TLS configuration before_script: - docker info build: stage: build script: - docker build -t my-image . - docker push my-image

2. Using Docker-in-Docker (dind) Mode

If a standalone Docker daemon is needed:

  • Add docker:20.10-dind service in .gitlab-ci.yml.
  • Set the DOCKER_HOST variable to tcp://docker:2375.
  • Enable privileged mode to support dind. .gitlab-ci.yml
yaml
---
image: docker:cli # Main container runs Docker commands services: - docker:20.10-dind # Provides standalone Docker daemon variables: DOCKER_HOST: tcp://docker:2375 # Connect to dind daemon DOCKER_TLS_CERTDIR: "" # Disable TLS configuration before_script: - docker info build: stage: build script: - docker build -t my-image . - docker push my-image

docker-compose.yaml

yaml
---
version: "3.8" services: gitlab-runner: image: gitlab/gitlab-runner:latest privileged: true # Must enable to support dind volumes: - /path/to/config:/etc/gitlab-runner networks: - gitlab-network docker: image: docker:20.10-dind privileged: true container_name: docker environment: DOCKER_TLS_CERTDIR: "" # Disable TLS networks: - gitlab-network networks: gitlab-network:

❗ Troubleshooting and Solutions

1. Error: dial tcp: lookup docker on 10.0.0.2:53: no such host

Cause:

  • The DOCKER_HOST=tcp://docker:2375 is configured in .gitlab-ci.yml, but the docker service is not started correctly or there are network configuration issues. Solution:
  1. Ensure that docker:20.10-dind is included in the services of .gitlab-ci.yml.
  2. Ensure that the same network is used in docker-compose.yaml.
  3. Or use docker:20.10-dind in services, ensuring the service name matches the configuration.

2. Error: error during connect: Head "http://docker:2375/_ping"

Cause:

  • DOCKER_HOST=tcp://docker:2375 is configured, but the dind service is not enabled, or the Docker daemon is not running. Solution:
  1. If you want to use the host Docker:
    • Remove DOCKER_HOST from .gitlab-ci.yml.
    • Ensure docker.sock is correctly mapped.
  2. If you want to use the dind service:
    • Add services: - docker:20.10-dind in .gitlab-ci.yml.
    • Enable privileged mode.

3. Error: Cannot connect to the Docker daemon

Cause:

  • /var/run/docker.sock is not mounted or DOCKER_HOST is not set correctly. Solution:
  1. If using host Docker:
    • Ensure the docker.sock file is correctly mounted to the Runner container.
    • Check that the mount paths are consistent.
  2. If using dind:
    • Ensure the docker:20.10-dind service is started and accessible.

✅ Summary

  • Shared Host Docker: Mount /var/run/docker.sock, no need to configure services.
  • Using Docker-in-Docker: Add services: docker:20.10-dind and configure DOCKER_HOST correctly.
  • When encountering issues, check network, service, and Runner configurations based on error messages.

📝 Detailed Explanation of image and services in .gitlab-ci.yml

🌟 1. Difference Between image and services

image

  • Definition: Specifies the primary container environment for running CI/CD jobs.
  • Function: Provides the base image for running the job; all commands in script are executed in this image.
  • Example:
    yaml
    ---
    image: python:3.9 script: - python --version
    In the above example, the job runs in a container based on the python:3.9 image, executing the commands in the script.

services

  • Definition: Provides auxiliary container services for the job.
  • Function: Runs additional service containers that share the network with the main container. Common services include databases, caching services, or Docker daemons.
  • Example:
    yaml
    コピー
    image: ruby:3.0 services: - redis:6.0 script: - ruby app.rb
    In the above example, the main container running the job is ruby:3.0, while the services start a redis:6.0 container as an auxiliary service, allowing the main container to access Redis via the redis hostname.

Comparison Summary

Featureimageservices
FunctionDefines the main runtime environmentProvides auxiliary services
Container CountSingle containerCan start multiple auxiliary service containers
Network ConfigurationJob containerShares network with job container, accessed via service name
Example UsageProgramming languages, tools, or compilation environmentsDatabases, caching services, or Docker daemons

🌟 2. Difference Between docker:cli, docker:20.10, and docker:20.10-dind

docker:cli

  • Definition: A lightweight image providing Docker CLI tools.
  • Function: Used to interact with an existing Docker daemon (e.g., the host's Docker daemon).
  • Characteristics
    • Does not include the Docker daemon.
    • Needs to connect to an external daemon via DOCKER_HOST.
  • Example:
    yaml
    ---
    image: docker:cli script: - docker info

docker:20.10

  • Definition: A complete image that includes both Docker CLI and the daemon.
  • Function: Used in situations where a full Docker environment is required, such as testing containerized applications.
  • Characteristics
    • Includes the Docker daemon but does not start by default.
    • Can be used to run simple Docker commands.
  • Example:
    yaml
    ---
    image: docker:20.10 script: - dockerd & # Start Docker daemon - docker info

docker:20.10-dind

  • Definition: A Docker-in-Docker image designed to run a standalone Docker daemon.
  • Function: Provides a fully isolated Docker daemon environment for building, running, or testing containers.
  • Characteristics
    • Requires privileged mode support.
    • Can be configured as services, sharing the network with the main container.
  • Example:
    yaml
    ---
    image: docker:cli services: - docker:20.10-dind variables: DOCKER_HOST: tcp://docker:2375 script: - docker info

Comparison Summary

Featuredocker:clidocker:20.10docker:20.10-dind
Included ComponentsDocker CLIDocker CLI + DaemonStandalone Docker Daemon
Applicable ScenariosUse host DockerSimple Docker testingStandalone Docker daemon environment
Daemon RunningNo daemon requiredOptional manual startAutomatically runs, requires privileged
Configuration ComplexityLowMediumHigh


🐳 GitLab Runner と Docker デーモン使用ガイド

🔧 GitLab Runner が CI/CD ジョブを実行

GitLab Runner は CI/CD ジョブを実行するためのコアツールです。Docker コンテナを構築または実行する必要がある場合、Docker デーモンとやり取りするための方法は2つあります。

  1. ホスト上の共有 Docker デーモン:Runner はホストの docker.sock ファイルをマウントすることによって、ホストの Docker サービスを直接使用します。
  2. Docker-in-Docker (dind) サービスを使用:ジョブ内で独立した Docker デーモンコンテナを実行します。

🚀 Docker デーモン構成方法

1. ホスト Docker デーモンを共有

Runner ジョブがホストの Docker デーモンを直接使用する必要がある場合:

  • /var/run/docker.sock を Runner コンテナにマウントします。
yaml
---
version: "3.8" services: gitlab-runner: image: gitlab/gitlab-runner:latest volumes: - /var/run/docker.sock:/var/run/docker.sock # ホスト Docker デーモンをマッピング - /path/to/config:/etc/gitlab-runner # Runner 設定ディレクトリ
  • また、gitlab-runner の config.toml に設定された executor docker も /var/run/docker.sock をマウントする必要があります。
yaml
---
[runners.docker] tls_verify = false image = "registry.dev.mbpsmartec.co.jp/ant-mvn:ubuntu" privileged = false disable_entrypoint_overwrite = false oom_kill_disable = false disable_cache = false volumes = ["/cache","/var/run/docker.sock:/var/run/docker.sock"] pull_policy = ["if-not-present"] shm_size = 0 network_mtu = 0
  • .gitlab-ci.ymlservices を設定する必要はありません。
  • 例の構成

.gitlab-ci.yml

yaml
---
image: docker:cli # Docker CLI イメージを使用 services: [] # 追加の Docker サービスは不要 variables: DOCKER_TLS_CERTDIR: "" # TLS 設定を無効化 before_script: - docker info build: stage: build script: - docker build -t my-image . - docker push my-image

2. Docker-in-Docker (dind) モードを使用

独立した Docker デーモンが必要な場合:

  • .gitlab-ci.yml に docker:20.10-dind サービスを追加します。
  • DOCKER_HOST 変数を tcp://docker:2375 に設定します。
  • dind をサポートするために特権モードを有効にします。
yaml
---
image: docker:cli # 主コンテナが Docker コマンドを実行 services: - docker:20.10-dind # 独立した Docker デーモンを提供 variables: DOCKER_HOST: tcp://docker:2375 # dind デーモンに接続 DOCKER_TLS_CERTDIR: "" # TLS 設定を無効化 before_script: - docker info build: stage: build script: - docker build -t my-image . - docker push my-image

docker-compose.yaml

yaml
コピー
version: "3.8" services: gitlab-runner: image: gitlab/gitlab-runner:latest privileged: true # dind をサポートするために必須 volumes: - /path/to/config:/etc/gitlab-runner networks: - gitlab-network docker: image: docker:20.10-dind privileged: true container_name: docker environment: DOCKER_TLS_CERTDIR: "" # TLS を無効化 networks: - gitlab-network networks: gitlab-network:

❗ 異常排査と解決方法

1. エラー:dial tcp: lookup docker on 10.0.0.2:53: no such host

原因:

  • .gitlab-ci.yml に DOCKER_HOST=tcp://docker:2375 が設定されていますが、docker サービスが正しく起動していないか、ネットワーク構成に問題があります。 解決方法:
  1. .gitlab-ci.yml の services に docker:20.10-dind が含まれていることを確認します。
  2. docker-compose.yaml で同じネットワークが使用されていることを確認します。
  3. または、services に docker:20.10-dind を使用し、サービス名が構成と一致していることを確認します。

2. エラー:error during connect: Head "http://docker:2375/_ping"

原因:

  • DOCKER_HOST=tcp://docker:2375 が設定されていますが、dind サービスが有効になっていないか、Docker デーモンが実行されていません。 解決方法:
  1. ホスト Docker を使用したい場合:
    • .gitlab-ci.yml から DOCKER_HOST を削除します。
    • docker.sock が正しくマッピングされていることを確認します。
  2. dind サービスを使用したい場合:
    • .gitlab-ci.yml に services: - docker:20.10-dind を追加します。
    • 特権モードを有効にします。

3. エラー:Cannot connect to the Docker daemon

原因:

  • /var/run/docker.sock がマウントされていないか、DOCKER_HOST が正しく設定されていません。 解決方法:
  1. ホスト Docker を使用する場合:
    • docker.sock ファイルが Runner コンテナに正しくマウントされていることを確認します。
    • マウントパスが一致していることを確認します。
  2. dind を使用する場合:
    • docker:20.10-dind サービスが起動していてアクセス可能であることを確認します。

✅ まとめ

  • ホスト Docker を共有:/var/run/docker.sock をマウントし、services を構成する必要はありません。
  • Docker-in-Docker を使用:services: docker:20.10-dind を追加し、DOCKER_HOST を正しく構成します。
  • 問題が発生した場合は、エラーメッセージに基づいてネットワーク、サービス、Runner の構成を確認します。

📝 .gitlab-ci.ymlimageservices の詳細説明

🌟 1. imageservices の違い

image

  • 定義:CI/CD ジョブを実行するための主要なコンテナ環境を指定します。
  • 作用:ジョブを実行するための基本イメージを提供し、script 内のすべてのコマンドがこのイメージ内で実行されます。
yaml
---
image: python:3.9 script: - python --version

上記の例では、ジョブが python:3.9 イメージに基づいてコンテナを起動し、スクリプト内のコマンドを実行します。

services

  • 定義:ジョブの実行に補助的なコンテナサービスを提供します。
  • 作用:追加のサービスコンテナを実行し、主要なコンテナとネットワークを共有します。一般的なサービスにはデータベース、キャッシュサービス、または Docker デーモンが含まれます。
yaml
---
image: ruby:3.0 services: - redis:6.0 script: - ruby app.rb

上記の例では、ジョブを実行する主コンテナは ruby:3.0 であり、services は補助サービスとして redis:6.0 コンテナを起動し、主コンテナは redis ホスト名を介して Redis サービスにアクセスできます。

比較のまとめ

特徴imageservices
作用主要な実行環境を定義補助サービスを提供
コンテナ数単一コンテナ複数の補助サービスコンテナを起動可能
ネットワーク構成ジョブコンテナジョブコンテナとネットワークを共有し、サービス名でアクセス
使用例プログラミング言語、ツール、またはコンパイル環境データベース、キャッシュサービス、または Docker デーモン

🌟 2. docker:cli、docker:20.10、および docker:20.10-dind の違い

docker:cli

  • 定義:Docker CLI ツールを提供する軽量イメージです。
  • 作用:既存の Docker デーモン(例えばホストの Docker デーモン)と対話するために使用されます。
  • 特徴
    • Docker デーモンは含まれていません。
    • DOCKER_HOST を介して外部デーモンに接続する必要があります。
yaml
---
image: docker:cli script: - docker info

docker:20.10

  • 定義:Docker CLI とデーモンを含む完全なイメージです。
  • 作用:完全な Docker 環境が必要な場合に使用されます。例えば、コンテナ化されたアプリケーションのテストなどです。
  • 特徴
    • Docker デーモンを含みますが、デフォルトでは起動しません
    • 簡単な Docker コマンドを実行するために使用できます。
yaml
---
image: docker:20.10 script: - dockerd & # Docker デーモンを起動 - docker info

docker:20.10-dind

  • 定義:独立した Docker デーモンを実行するために設計された Docker-in-Docker イメージです。
  • 作用:コンテナを構築、実行、またはテストするための完全に隔離された Docker デーモン環境を提供します。
  • 特徴
    • 特権モードのサポートが必要です。
    • services として構成でき、主コンテナとネットワークを共有します。
yaml
---
image: docker:cli services: - docker:20.10-dind variables: DOCKER_HOST: tcp://docker:2375 script: - docker info

比較のまとめ

特徴docker:clidocker:20.10docker:20.10-dind
含まれるコンポーネントDocker CLIDocker CLI + デーモン独立した Docker デーモン
適用シナリオホスト Docker を使用簡単な Docker テスト独立した Docker デーモン環境
デーモンの実行デーモンは不要手動での起動が可能自動的に実行、特権が必要
設定の複雑さ