Docker イメージをビルド環境として使用する
Bitbucket Pipelines では、Docker コンテナでビルドを実行します。これらのコンテナでは、ビルド環境を定義する Docker イメージを実行します。Bitbucket が提供する 既定のイメージを使用するか、またはカスタムイメージを取得できます。
We support public and private Docker images including those hosted on Docker Hub, AWS, GCP, and self-hosted registries accessible on the internet. (Bitbucket Pipelines cannot currently access Docker images that cannot be accessed via the internet.)
概要
既定のビルド環境
ビルド環境として使用する Docker イメージを指定しない場合、Bitbucket Pipelines はアトラシアンが一般的なツールで構築した初期設定のものを使用します。
The default image is atlassian/default-image:latest. You can also use a specific version if the tools available in the default image do not satisfy your requirements.
名前 | プラットフォーム | すぐに使用可能なアプリ |
---|---|---|
atlassian/default-image:1 atlassian/default-image:latest | Ubuntu 14.04 |
|
atlassian/default-image:2 | Ubuntu 16.04 |
|
atlassian/default-image:3
| ubuntu 20.04 LTS |
|
atlassian/default-image:4
| ubuntu 22.04 LTS |
|
atlassian/default-image:5 推奨 | ubuntu 24.04 LTS |
|
ベスト プラクティスのためのヒント: Pipelines の実行を確認したら、デフォルト イメージ以外に使用できるイメージを見つけることをおすすめします。デフォルト イメージは、利用するユーザーのための互換性を最大限に確保することを意図しており、更新頻度は高くありません。また、多数のツールが含まれているため、環境によっては不要な場合が考えられます。つまり、自身で見つけたサイズの小さいビルド イメージのほうが、バグの修正を迅速に反映し、ビルドもすばやく行える可能性があります。
カスタムのビルド環境
既定の環境がニーズに合わない場合、Docker Hub で利用可能なイメージに役立つものがないかどうかを確認することをおすすめします。Docker Hub で利用可能な公式イメージで、Atlassian が推奨し、セットアップ中に Pipelines で構成しているものに、次のような例があります。
JavaScript: https://hub.docker.com/_/node/
Java (Maven): https://hub.docker.com/_/maven/
Python: https://hub.docker.com/_/python/
.NET Core: https://hub.docker.com/r/microsoft/dotnet/
ビルド イメージとしてこれらのイメージや Docker Hub 上の別のパブリック イメージを使用する場合、後述のパブリック ビルド イメージの使用のガイドをご参照ください。
To use an existing Docker image from your own registry, see the guide for Using private build images below.
To build your own Docker images, see the guide for Creating a custom build environment below.
パブリック ビルド イメージの使用
Docker Hub、別のレジストリ、またはセルフホスト型のレジストリにホストされる既存のパブリック イメージを使用できます。インターネット経由でアクセスできるイメージのみを使用できます。
例では、次のようになっています。
アカウント名は、イメージを所有するアカウントの名前です。
ユーザー名、パスワード、およびメール アドレスは、レジストリに対する個人の認証情報です。
Docker Hub でホストされるパブリック イメージ
関連する dockerhub ページに一覧表示されている pull
コマンド (1) からイメージ名を見つけることができます。
タグを指定しない場合、Docker では latest
バージョン タグを使用します。
image: openjdk
Docker Hub のイメージ バージョンとアカウントの両方を指定した場合の例は次のようになります。
image: account-name/openjdk:8
Docker Hub の外部でホストされるパブリック イメージ
Docker 以外が提供している、プライベート レジストリでホストされているイメージの場合、URL にイメージ名を含めます。
image: docker.someprovider.com/account-name/openjdk:8
プライベート ビルド イメージの使用
セルフホスト型のレジストリや、Docker Hub、Amazon ECR、Google GCR のプライベート イメージなどの、プライベートな Docker レジストリを認証する場合、YAML ファイル内でイメージ構成の一部としてユーザー名とパスワードを提供する必要があります。プロバイダによってはメール アドレスが要求される場合があるため。これを任意で含めることもできます。
Docker Hub でホストされるプライベート イメージ
セキュアな変数を使用してユーザー名やパスワードの変数を構成し、それらを次のように image の YAML 構成に含めることができます。
image:
name: account-name/openjdk:8
username: $DOCKER_HUB_USERNAME
password: $DOCKER_HUB_PASSWORD
email: $DOCKER_HUB_EMAIL
AWS ECR (EC2 コンテナ レジストリ) でホストされるプライベート イメージ
If the image is hosted by ECR, you can provide the access key and secret key via secure variables under an additional aws
section in your YAML image configuration:
image:
name: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/openjdk:8
aws:
access-key: $AWS_ACCESS_KEY
secret-key: $AWS_SECRET_KEY
Alternatively, you can avoid storing AWS_ACCESS_KEY
and AWS_SECRET_KEY
in Bitbucket and make use of OpenID Connect functionality to allow your workspace builds to access your image only.
image:
name: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/openjdk:8
aws:
oidc-role: arn:aws:iam::<aws_account_id>:role/<your_role_name>
Bitbucket OpenID Connect と統合するように AWS アカウントを設定する方法に関するステップ バイ ステップの手順については、OpenID Connect を使用して、Pipelines の AWS ECR イメージを使用するをご確認ください。
Google Container Registry (GCR) でホストされるプライベート イメージ
GCR のイメージをプルするには、GCP の管理コンソールで、Pipelines 用に "Viewer" アクセスを持つサービス アカウントを構成する必要があります。
生成された JSON 形式のプライベート キーをダウンロードし、Pipelines のセキュアな変数にコピー アンド ペーストします。構成した変数は次の例のように、YAML image 構成の password フィールドで直接使用できます。
image:
name: <region>.gcr.io/<project>/image:latest
username: _json_key
password: '$GCR_JSON_KEY'
その他のレジストリでホストされるイメージ
You can use secure variables to configure username
and password
variables, then add them to the image YAML configuration as shown below:
image:
name: docker.your-company-name.com/account-name/openjdk:8
username: $USERNAME
password: $PASSWORD
email: $EMAIL
イメージのダイジェストの保存
確実に同じイメージを取得するには、イメージのハッシュ値またはダイジェスト値を参照します。ダイジェストは暗号化されたハッシュ値であり、イメージの内容が変更されるとダイジェストも変更されます。イメージのダイジェストを保存することで、変更が行われていない同じイメージを確実に参照できます。
イメージのダイジェストの使用
image:
name: ubuntu@sha256:a0ee7647e24c8494f1cf6b94f1a3cd127f423268293c25d924fbe18fd82db5a4
イメージのダイジェストの検索
> docker inspect --format='{{.RepoDigests}}' ubuntu
[ubuntu@sha256:a0ee7647e24c8494f1cf6b94f1a3cd127f423268293c25d924fbe18fd82db5a4]
The Docker inspect command will return an error if the image is not available locally. Be sure to pull the image before running the command.
既定ユーザーのオーバーライド
An image's default user can be overridden by specifying a user UID as the run-as-user
. The specified user UID must be an existing user in the image with a valid home directory.
image:
name: atlassian/default-image:3
run-as-user: 1000
カスタム ビルド環境の作成
You can build your own Docker file, create a docker image out of it, push it to a registry of your choice, and use it as your build environment. This way, you can keep your build environment lightweight by only including the tools that you need in your build.
リソース
Docker をローカルにインストール: https://docs.docker.com/engine/installation/
独自のDocker イメージを作成します https://docs.docker.com/build/building/base-images/
Docker イメージを Docker Hub に公開します https://docs.docker.com/get-started/workshop/04_sharing_app/
独自のレジストリを使用する方法: https://www.docker.com/blog/how-to-use-your-own-registry-2/
この内容はお役に立ちましたか?