shell 脚本和终端之间 运行 命令后的不同结果

Different result after run command between shell script and terminal

情况

使用 shell 脚本将 docker 图像(Nodejs 应用程序)推送到 ECR

命令

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

文件shell脚本

#!/bin/sh

docker build -t abc/abc-api ../../abc/abc-api

docker build -t abc/abc-fe ../../abc/abc-fe

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

docker tag abc/abc-api xxxxxxxxxxx.dkr.ecr.ap-southeast-1.amazonaws.com/abc:api-latest

docker push xxxxxxxxxxx.dkr.ecr.ap-southeast-1.amazonaws.com/abc:api-latest

docker tag abc/abc-fe xxxxxxxxxxx.dkr.ecr.ap-southeast-1.amazonaws.com/abc:fe-latest

docker push xxxxxxxxxxx.dkr.ecr.ap-southeast-1.amazonaws.com/abc:fe-latest

当前

Unable to locate credentials. You can configure credentials by running "aws configure".
Error: Cannot perform an interactive login from a non TTY device
WARNING! Your password will be stored unencrypted in /home/***/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

我想要的

Shell脚本登录成功

环境

根据评论。

脚本不起作用的原因是它在根用户下执行。 root 用户没有定义 aws 配置文件,这解释了为什么脚本出错并显示“您可以通过 运行ning“aws configure”消息配置凭据。

要纠正这个问题,有一些可能性:

  1. 不要运行 root 用户下的脚本。 运行 它作为您自己的用户,因为您有自己的 aws 配置文件配置(首选)。
  2. 为 root 用户创建新的 aws 配置文件。