Which is the best way to pass AWS credentials to Docker container?

前端 未结 5 918
无人及你
无人及你 2020-12-02 07:12

I am running docker-container on Amazon EC2. Currently I have added AWS Credentials to Dockerfile. Could you please let me know the best way to do this?

5条回答
  •  眼角桃花
    2020-12-02 07:44

    Yet another approach is to create temporary read-only volume in docker-compose.yaml. AWS CLI and SDK (like boto3 or AWS SDK for Java etc.) are looking for default profile in ~/.aws/credentials file.

    If you want to use other profiles, you just need also to export AWS_PROFILE variable before running docker-compose command.

    export AWS_PROFILE=some_other_profile_name

    version: '3'
    
    services:
      service-name:
        image: docker-image-name:latest
        environment:
          - AWS_PROFILE=${AWS_PROFILE}
        volumes:
          - ~/.aws/:/root/.aws:ro
    

    In this example, I used root user on docker. If you are using other user, just change /root/.aws to user home directory.

    :ro - stands for read-only docker volume

    It is very helpful when you have multiple profiles in ~/.aws/credentials file and you are also using MFA. Also helpful when you want to locally test docker-container before deploying it on ECS on which you have IAM Roles, but locally you don't.

提交回复
热议问题