CodeBuildでDocker in Dockerする(CloudFormation)


やり方

CodeBuild Project > Environment > PrivilegedModeをTrueにする

PrivilegedMode:
プロジェクトで Docker イメージを実行する方法を示します。true を指定して、Docker コンテナ内の Docker デーモンを実行できるようにします。
AWS CodeBuild プロジェクト環境

Before

  CodeBuildProject:
    Description: Creating AWS CodeBuild project
    Type: AWS::CodeBuild::Project
    Properties:
      Artifacts:
        Type: CODEPIPELINE
      Description: !Sub Building stage for ${Branch}.
      Environment:
        ComputeType: BUILD_GENERAL1_SMALL
        EnvironmentVariables:
          - Name: Branch
            Value: !Ref Branch
        Image: !Ref CodeBuildImage
        Type: LINUX_CONTAINER
      Name: !Sub ${ServiceName}-${Branch}-build
      ServiceRole: !Ref CodeBuildRole
      Source:
        Type: CODEPIPELINE
      TimeoutInMinutes: 5

After

  CodeBuildProject:
    Description: Creating AWS CodeBuild project
    Type: AWS::CodeBuild::Project
    Properties:
      Artifacts:
        Type: CODEPIPELINE
      Description: !Sub Building stage for ${Branch}.
      Environment:
        ComputeType: BUILD_GENERAL1_SMALL
        EnvironmentVariables:
          - Name: Branch
            Value: !Ref Branch
        Image: !Ref CodeBuildImage
        Type: LINUX_CONTAINER
        PrivilegedMode: True
      Name: !Sub ${ServiceName}-${Branch}-build
      ServiceRole: !Ref CodeBuildRole
      Source:
        Type: CODEPIPELINE
      TimeoutInMinutes: 5

エラー

上記の指定をしないと↓のエラーが出る。

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

備考