6
1

More than 1 year has passed since last update.

ECRにログインやDockerをpushするときにエラー+複数ユーザーで使用したいとき(Error: Cannot perform an interactive login from a non TTY device)

Last updated at Posted at 2022-11-18

EC2サーバーでECRにログインするときのエラー

背景

Amazon ECRを使う場面でのことです。
ECRでは「プッシュコマンドの表示」があり、それ通りに行うことでレポジトリ内にあるimageをpushできます。

image.png

ある日新しいEC2サーバーでECRを使うため、新しいEC2にAWS CLIとDockerをインストールしました。図にしたのが以下です。

image.png

この状態で、Dockerイメージを構築する前段階の処理(認証トークンを取得+レジストリに対して Docker クライアントを認証)をするために下記赤枠コマンドをそのまま実行したところ、エラーが発生しました。

2022-11-11_16h07_26.png

aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 000000000000.dkr.ecr.ap-northeast-1.amazonaws.com

※0000…のところはアカウントID

エラー内容

An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation: User: arn:aws:sts::000000000000:assumed-role/ec2-role/i-0a241ere75e550e3dee4 is not authorized to perform: ecr:GetAuthorizationToken on resource: * because no identity-based policy allows the ecr:GetAuthorizationToken action
Error: Cannot perform an interactive login from a non TTY device

要因

aws のプロファイル情報が紐づけされていないため

解決策

AWSのドキュメントにのっていました。

以下のコードでプロファイルを設定できます。

aws configure

Access Key IDやSecret Access KeyはIAMから取得できます。

設定できたか確認

以下のコードで確認できます

aws configure list

2022-11-11_16h17_39.png

冒頭にあったECRのプッシュコマンドを実行すると無事成功。

2022-11-11_16h21_55.png

複数ユーザー(別ユーザー)が使うとき

背景

既にEC2サーバーにAWS CLIの情報がある場合、上記の手順で行うと情報を上書きしてしまいます。
そのため、以下手順でprofile名を指定することで別ユーザーでも操作することに成功しました。

解決策

--profileオプションをつけた上でプロファイル情報を設定します

aws configure --profile testadduser

※「testadduser」部分はプロファイル指定したい名前にする

設定できたか確認

上記と同様ですが、--profileをつけて確認します。

aws configure list --profile testadduser

最後に冒頭にあったECRのプッシュコマンドを実行します。
ただしそのままコピペだと実行できません。
コード内で --profile test_add_user とつけることで、指定したプロファイル情報を参考にログイン試行してくれます。

aws ecr get-login-password --region ap-northeast-1 --profile test_add_user | docker login --username AWS --password-stdin 000000000000.dkr.ecr.ap-northeast-1.amazonaws.com

2022-11-11_16h32_21.png

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1