前提・実現したいこと
Apache+Git(Smart HTTP)を導入したDockerコンテナにvolumeとして/var/run/docker.sockをマウントし、Git hookでmasterへのpush時にアップロードされたbuild.shに従ってDockerイメージを自動的にビルドするシステムを作っています。
発生している問題・エラーメッセージ
コンテナのシェルから直接build.shを実行する場合は普通にイメージのビルドが行われるのですが、git hookからbuild.shが実行された場合はDockerのソケットファイルのパーミッションが拒否されてしまいます。
$ git push origin master Username for 'http://localhost:8080': boke0 Password for 'http://boke0@localhost:8080': Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 264 bytes | 264.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/build?buildargs=%7B%7D&buildbinds=null&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=hoge%3Adev&ulimits=null: dial unix /var/run/docker.sock: connect: permission denied To http://localhost:8080/git/hoge.git 4805741..dcfd059 master -> master
該当のソースコード
build.sh
bash
1#!/bin/bash 2 3if [ "$1"=="dev" ]; then 4 tag="dev" 5else 6 tag="1.0" 7fi 8docker build . -t hoge:$tag 9
post-receive
PHP
1#!/usr/bin/php -q 2<?php 3 4$git=explode("/",$_SERVER["REQUEST_URI"])[2]; 5$id=explode(".",$git)[0]; 6 7$ref=explode(" ",trim(fgets(STDIN)))[2]; 8if($ref=="refs/heads/master"){ 9 @exec("rm -Rf /var/www/resource/docker/{$id}"); 10 exec("git clone /var/www/resource/git/{$id}.git /var/www/resource/docker/{$id}"); 11 chmod("/var/www/resource/docker/{$id}/build.sh",0777); 12 chdir("/var/www/resource/docker/{$id}"); 13 exec("./build.sh"); 14}
コンテナ内のapacheユーザーとrootユーザーのメイングループはホストのDockerグループと同じIDのグループにしているのですが、何が原因でしょうか
2019年4月22日22時25分追記
apacheユーザーのシェルにbashで入ってDockerコマンドを実行したところ、パーミッションは拒否されず、実行できました。そこで、Git hookからid
の実行結果を出力してみると以下のようになりました。
remote: uid=48(apache) gid=48(apache) groups=48(apache) remote: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/build?buildargs=%7B%7D&buildbinds=null&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=hoge%3Adev&ulimits=null: dial unix /var/run/docker.sock: connect: permission denied
どうやら、Git hookのPHPのexecからコマンドを打つとdockerグループとして認識されていないようです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。