Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker - cannot find aws credentials in container although they exist

Running the following docker command on mac works and on linux, running ubuntu cannot find the aws cli credentials. It returns the following message: Unable to locate credentials Completed 1 part(s) with ... file(s) remaining

The command which runs an image and mounts a data volume and then copies a file from and s3 bucket, and starts the bash shell in the docker container.

sudo docker run -it --rm -v ~/.aws:/root/.aws username/docker-image sh -c 'aws s3 cp s3://bucketname/filename.tar.gz /home/emailer && cd /home/emailer && tar zxvf filename.tar.gz && /bin/bash'

What am I missing here?

This is my Dockerfile:

FROM ubuntu:latest

#install node and npm
RUN apt-get update && \
    apt-get -y install curl && \
    curl -sL https://deb.nodesource.com/setup | sudo bash - && \
    apt-get -y install python build-essential nodejs

#install and set-up aws-cli
RUN sudo apt-get -y install \
    git \
    nano \
    unzip && \
    curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" && \
    unzip awscli-bundle.zip

RUN sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

# Provides cached layer for node_modules
ADD package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /home/emailer && cp -a /tmp/node_modules /home/emailer/
like image 687
hyprstack Avatar asked Jun 26 '15 13:06

hyprstack


2 Answers

You can use environment variable instead of copying ~/.aws/credentials and config file into container for aws-cli

docker run \ -e AWS_ACCESS_KEY_ID=AXXXXXXXXXXXXE \ -e AWS_SECRET_ACCESS_KEY=wXXXXXXXXXXXXY \ -e AWS_DEFAULT_REGION=us-west-2 \ <img>

Ref: AWS CLI Doc

like image 74
Tony Lee Avatar answered Sep 17 '22 23:09

Tony Lee


Mounting $HOME/.aws/ into the container should work. Make sure to mount it as read-only.

It is also worth mentioning, if you have several profiles in your ~/.aws/config -- you must also provide the AWS_PROFILE=somethingsomething environment variable. E.g. via docker run -e AWS_PROFILE=xxx ... otherwise you'll get the same error message (unable to locate credentials).

Update: Added example of the mount command

docker run -v ~/.aws:/root/.aws …
like image 44
Bastian Venthur Avatar answered Sep 19 '22 23:09

Bastian Venthur