猫でもわかるWebプログラミングと副業

本業エンジニアリングマネージャー。副業Webエンジニア。Web開発のヒントや、副業、日常生活のことを書きます。

AWS CDK で構築した AWS CodeBuild の docker build で NO_PUBKEY エラー

発生した現象

AWS CDK で CodeBuild のプロジェクトを作成し、Docker のビルドを行ったところ、

Dockerfile の RUN apt-get update で以下のようなエラーが発生しビルドに失敗してしまった。

Err:1 http://security.ubuntu.com/ubuntu jammy-security InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C

...(中略)...

W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: GPG error: http://security.ubuntu.com/ubuntu jammy-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
E: The repository 'http://security.ubuntu.com/ubuntu jammy-security InRelease' is not signed.

結論

CDK で AWS CodeBuild を構築するときに、 buildImage を指定したら解決しました。

buildImage とは、 docker build をどんな環境で行うか指定する設定なのですが、 CDK で buildImage を指定せずに CodeBuild を構築すると、standard:1.0 という環境になってしまうようです。

standard:1.0 では環境が古すぎて Docker のビルドが正常に行えないので、適切な環境を指定します。僕の場合は、これを書いている時点の最新版である standard:7.0 というのを指定したら解消しました。

const codeBuild = new codebuild.PipelineProject(this, "app_build", {
  projectName: "app-build",
  environment: { 
    // Docker をビルドするためには priviledged を true にする必要がある
    privileged: true,
    // buildImage を指定しないと standard:1.0 になってしまうため明示的に指定する
    buildImage: codebuild.LinuxBuildImage.STANDARD_7_0,
  },
  role: codeBuildRole,
});

詳細

エラーが出た CodeBuild を構築した AWS CDK のコードは以下

const codeBuild = new codebuild.PipelineProject(this, "app_build", {
  projectName: "app-build",
  environment: { 
    // Docker をビルドするためには priviledged を true にする必要がある
    privileged: true,
  },
  role: codeBuildRole,
});

これで CodeBuild プロジェクトを作成すると、standard:1.0 という環境で作成されます。

CodeBuild のページからプロジェクトを選択し、ビルドの詳細 > 環境を見ると書いてあります。(↓の写真は standard:7.0 に修正したあとのものです)

今回ビルドした Dockerfile

FROM tomcat:10.1.8
RUN apt-get update -y && apt-get install -y maven

# これ以下は関係無いので略

buildspec.yml は以下の通り。docker build が成功するかどうかのテストをしていたので、ECR に push するステップはまだありません。

version: 0.2
phases:
  build:
    commands:
      - docker build -t my-app .

すると発生したエラーが以下

(前略)

Step 2/6 : RUN apt-get update -y && apt-get install -y maven
 ---> Running in d3eab0495ec6
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
Err:1 http://security.ubuntu.com/ubuntu jammy-security InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
Err:2 http://archive.ubuntu.com/ubuntu jammy InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Err:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [108 kB]
Err:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
Reading package lists...
[91mW: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: GPG error: http://security.ubuntu.com/ubuntu jammy-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
E: The repository 'http://security.ubuntu.com/ubuntu jammy-security InRelease' is not signed.
W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: GPG error: http://archive.ubuntu.com/ubuntu jammy InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
E: The repository 'http://archive.ubuntu.com/ubuntu jammy InRelease' is not signed.
W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: GPG error: http://archive.ubuntu.com/ubuntu jammy-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
E: The repository 'http://archive.ubuntu.com/ubuntu jammy-updates InRelease' is not signed.
W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key.
W: GPG error: http://archive.ubuntu.com/ubuntu jammy-backports InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C
E: The repository 'http://archive.ubuntu.com/ubuntu jammy-backports InRelease' is not signed.
E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true'
E: Sub-process returned an error code
[0mThe command '/bin/sh -c apt-get update -y && apt-get install -y maven' returned a non-zero code: 100

エラーでググっても、GPG の key が足りてないから key を更新して、みたいなのが出てくるんですが、色々試してもあまりうまくいかず...

設定とかを見直してみると、ビルドイメージがなんか古くない?ということに気づき、以下で解決しました。

const codeBuild = new codebuild.PipelineProject(this, "app_build", {
  projectName: "app-build",
  environment: { 
    // Docker をビルドするためには priviledged を true にする必要がある
    privileged: true,
    // buildImage を指定しないと standard:1.0 になってしまうため明示的に指定する
    buildImage: codebuild.LinuxBuildImage.STANDARD_7_0,
  },
  role: codeBuildRole,
});

しかし、デフォルトでなぜ standard1.0 になるのか謎です... AWS コンソールから作成すると、standard:1.0 は古すぎて選択肢に出てきません(5.0, 6.0, 7.0 の3つしか選べない)

せめて必須パラメータにしてくれれば...