Search code examples
amazon-web-servicesshamazon-ecr

Different result after run command between shell script and terminal


Situation

Use shell script to push docker images (Nodejs app) to ECR

Command

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

File shell script

#!/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

Current

  • When run file shell script
Unable to locate credentials. You can configure credentials by running "aws configure".
Error: Cannot perform an interactive login from a non TTY device
  • When run command on terminal
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

What I want

Shell script login succeeded

Environment

  • Linux mint 20
  • aws-cli 2.0.46
  • Configured aws authentication by aws configure

Solution

  • Based on the comments.

    The reason why the script does not work is that it is executed under root user. The root user does not have aws profile defined, which explain why the script errors out with "You can configure credentials by running "aws configure" message.

    To rectify the issue, there are some posibilites:

    1. Don't run the script under root user. Run it as your own user, because you have aws profile configure for yourself (preferred).
    2. Create new aws profile for the root user.