Docker タスクDocker task
このタスクは、docker レジストリサービス接続を使用して、任意のコンテナーレジストリに Docker イメージをビルドしてプッシュする場合に使用します。Use this task to build and push Docker images to any container registry using Docker registry service connection.
概要Overview
Docker タスクをスクリプトで直接使用する場合と比較して、docker タスクを使用する主な利点は次のとおりです。Following are the key benefits of using Docker task as compared to directly using docker client binary in script -
Docker registry サービス接続との統合 -このタスクにより、docker registry サービス接続を使用して任意のコンテナーレジストリに接続することが簡単になります。Integration with Docker registry service connection - The task makes it easy to use a Docker registry service connection for connecting to any container registry. ログインすると、ユーザーは、Docker タスクによって既に実行されているログインを利用して、タスクやスクリプトを実行するためのフォローアップタスクを作成できます。Once logged in, the user can author follow up tasks to execute any tasks/scripts by leveraging the login already done by the Docker task. たとえば、Docker タスクを使用して任意の Azure Container Registry にサインインし、その後のタスク/スクリプトを使用して、このレジストリにイメージをビルドしてプッシュすることができます。For example, you can use the Docker task to sign in to any Azure Container Registry and then use a subsequent task/script to build and push an image to this registry.
ラベルとして追加されたメタデータ -タスクは、追跡に関連するメタデータを次のラベルの形式で画像に追加します。Metadata added as labels - The task adds traceability-related metadata to the image in the form of the following labels -
- .com... dev. ビルドcom.azure.dev.image.build.buildnumber
- builduri (com... イメージ)com.azure.dev.image.build.builduri
- .com... dev. definitionnamecom.azure.dev.image.build.definitionname
- com.azure.dev.image.build.repository.namecom.azure.dev.image.build.repository.name
- .com......。com.azure.dev.image.build.repository.uri
- sourcebranchname (com... イメージ)com.azure.dev.image.build.sourcebranchname
- com........ build... ビルド. sourceversioncom.azure.dev.image.build.sourceversion
- .com... definitionname definitionnamecom.azure.dev.image.release.definitionname
- .com... release. releaseidcom.azure.dev.image.release.releaseid
- releaseweburl (com... イメージ)com.azure.dev.image.release.releaseweburl
- com.azure.dev.image.system のコレクションcom.azure.dev.image.system.teamfoundationcollectionuri
- com.azure.dev.image.system teamprojectcom.azure.dev.image.system.teamproject
タスクの入力Task Inputs
パラメーターParameters | 説明Description |
---|---|
command コマンドCommand |
必要使用可能な値: buildAndPush 、、 build push 、 login 、 logout (Required) Possible values: buildAndPush , build , push , login , logout 2.173.0 のバージョンで追加されました: start 、 stop Added in version 2.173.0: start , stop 既定値: buildAndPush Default value: buildAndPush |
containerRegistry コンテナー レジストリContainer registry |
OptionalDocker registry サービス接続の名前(Optional) Name of the Docker registry service connection |
repository リポジトリRepository |
Optionalコンテナーレジストリ内のリポジトリの名前。入力として指定された Docker registry サービス接続に対応します。 containerRegistry (Optional) Name of repository within the container registry corresponding to the Docker registry service connection specified as input for containerRegistry |
container コンテナーContainer |
(コマンドとの場合は必須 start stop ) 開始または停止するコンテナーリソース(Required for commands start and stop ) The container resource to start or stop |
tags TagsTags |
Optional行ごとに入力します。各行に build は、、 push または buildAndPush コマンドで使用するタグが含まれています。(Optional) Multiline input where each line contains a tag to be used in build , push or buildAndPush commands既定値: $(Build.BuildId) Default value: $(Build.BuildId) |
Dockerfile DockerfileDockerfile |
OptionalDockerfile へのパス。(Optional) Path to the Dockerfile. タスクは、見つかった 最初 の dockerfile を使用してイメージをビルドします。The task will use the first dockerfile it finds to build the image. 既定値: **/Dockerfile Default value: **/Dockerfile |
buildContext ビルドコンテキストBuild context |
Optionalビルドコンテキストへのパス(Optional) Path to the build context 既定値: ** Default value: ** |
arguments 引数Arguments |
OptionalDocker クライアントに渡される追加の引数(Optional) Additional arguments to be passed onto the docker client パラメーターの値を使用する場合、プロパティは無視されることに注意 buildAndPush command して arguments ください。Be aware that if you use value buildAndPush for the command parameter, then the arguments property will be ignored. |
addPipelineData パイプラインデータの追加Add Pipeline Data |
Optional前述のメタデータをラベルとしてイメージに追加します。(Optional) Adds the above mentioned metadata as labels to the image 指定できる値: true 、false Possible values: true , false 既定値: true Default value: true |
ログインLogin
次の YAML スニペットは、Docker registry サービス接続を使用したコンテナーレジストリのログインを示しています。Following YAML snippet showcases container registry login using a Docker registry service connection -
- task: Docker@2
displayName: Login to ACR
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection1
ビルドとプッシュBuild and Push
BuildAndPush という便利なコマンドを使用すると、イメージを1つのコマンドでコンテナーレジストリにビルドおよびプッシュすることができます。A convenience command called buildAndPush allows for build and push of images to container registry in a single command. 次の YAML スニペットは、複数のレジストリにイメージの複数のタグを作成してプッシュする例です。The following YAML snippet is an example of building and pushing multiple tags of an image to multiple registries -
steps:
- task: Docker@2
displayName: Login to ACR
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection1
- task: Docker@2
displayName: Login to Docker Hub
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection2
- task: Docker@2
displayName: Build and Push
inputs:
command: buildAndPush
repository: contosoRepository
tags: |
tag1
tag2
上のスニペットでは、イメージ contosoRepository:tag1
と contosoRepository:tag2
がビルドされ、およびに対応するコンテナーレジストリにプッシュされ dockerRegistryServiceConnection1
dockerRegistryServiceConnection2
ます。In the above snippet, the images contosoRepository:tag1
and contosoRepository:tag2
are built and pushed to the container registries corresponding to dockerRegistryServiceConnection1
and dockerRegistryServiceConnection2
.
すべての認証済みコンテナーレジストリを一度に構築してプッシュするのではなく、特定の認証済みコンテナーレジストリをビルドしてプッシュする場合は、次に示すように、と共に containerRegistry
入力を明示的に指定できます。 command: buildAndPush
If one wants to build and push to a specific authenticated container registry instead of building and pushing to all authenticated container registries at once, the containerRegistry
input can be explicitly specified along with command: buildAndPush
as shown below -
steps:
- task: Docker@2
displayName: Build and Push
inputs:
command: buildAndPush
containerRegistry: dockerRegistryServiceConnection1
repository: contosoRepository
tags: |
tag1
tag2
LogoutLogout
次の YAML スニペットは、Docker registry サービス接続を使用したコンテナーレジストリのログアウトを示しています。Following YAML snippet showcases container registry logout using a Docker registry service connection -
- task: Docker@2
displayName: Logout of ACR
inputs:
command: logout
containerRegistry: dockerRegistryServiceConnection1
開始/停止Start/stop
このタスクは、ジョブおよびサービスコンテナーの制御にも使用できます。This task can also be used to control job and service containers. この使用方法は一般的ではありませんが、固有の状況に使用されることがあります。This usage is uncommon, but occasionally used for unique circumstances.
resources:
containers:
- container: builder
image: ubuntu:18.04
steps:
- script: echo "I can run inside the container (it starts by default)"
target:
container: builder
- task: Docker@2
inputs:
command: stop
container: builder
# any task beyond this point would not be able to target the builder container
# because it's been stopped
その他のコマンドと引数Other commands and arguments
コマンドと引数の入力を使用して、次に示すように、docker クライアントバイナリを使用してビルドまたはプッシュコマンドの追加の引数を渡すことができます。The command and argument inputs can be used to pass additional arguments for build or push commands using docker client binary as shown below -
steps:
- task: Docker@2
displayName: Login to ACR
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection1
- task: Docker@2
displayName: Build
inputs:
command: build
repository: contosoRepository
tags: tag1
arguments: --secret id=mysecret,src=mysecret.txt
注意
引数の入力は、を除くすべてのコマンドに対して評価され buildAndPush
ます。The arguments input is evaluated for all commands except buildAndPush
. buildAndPush
は便利なコマンド (の build
後に続く) として push
、 arguments
このコマンドの入力は無視されます。As buildAndPush
is a convenience command (build
followed by push
), arguments
input is ignored for this command.
トラブルシューティングTroubleshooting
Docker タスクが buildAndPush コマンドに渡された引数を無視するのはなぜですか。Why does Docker task ignore arguments passed to buildAndPush command?
BuildAndPush コマンドで構成された Docker タスクは、内部で実行されるビルドおよびプッシュコマンドにあいまいになるため、渡された引数を無視します。Docker task configured with buildAndPush command ignores the arguments passed since they become ambiguous to the build and push commands that are run internally. コマンドを別のビルドおよびプッシュステップに分割し、適切な引数を渡すことができます。You can split your command into separate build and push steps and pass the suitable arguments. 例については、この stackoverflow の投稿 をご覧ください。See this stackoverflow post for example.
DockerV2 は、Docker registry サービス接続のみをサポートしており、ARM サービス接続をサポートしていません。DockerV2 only supports Docker registry service connection and not support ARM service connection. Docker タスクで認証に既存の Azure サービスプリンシパル (SPN) を使用するにはどうすればよいですか。How can I use an existing Azure service principal (SPN) for authentication in Docker task?
Docker registry サービス接続は、Azure SPN の資格情報を使用して作成できます。You can create a Docker registry service connection using your Azure SPN credentials. [レジストリの種類] から [その他] を選択し、次のように詳細を指定します。Choose the Others from Registry type and provide the details as follows:
Docker Registry: Your container registry URL (eg. https://myacr.azurecr.io)
Docker ID: Service principal client ID
Password: Service principal key
ソースを開くOpen source
このタスクは、 GitHub のオープンソースです。This task is open source on GitHub. フィードバックと投稿が歓迎されます。Feedback and contributions are welcome.