Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install AWS CLI in docker container based on image "python:2.7"

Tags:

docker

aws-cli

I have a dockerfile that looks like this:

FROM python:2.7
RUN pip install awscli --upgrade --user

Once the docker image is built from this dockerfile, I run it. But when I get into the container and try to run the AWS CLI, it can't find it, because it is not in the PATH environment variable:

$ docker exec -ti ec4934370e37 /bin/bash

root@ec4934370e37:~# aws
bash: aws: command not found

root@ec4934370e37:/# find / -name aws
/root/.local/bin/aws

root@ec4934370e37:/# /root/.local/bin/aws --version
aws-cli/1.15.81 Python/2.7.15 Linux/4.9.87-linuxkit-aufs botocore/1.10.80

root@ec4934370e37:/# env | grep PATH
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

What is the best/easiest/least-hacky way to make sure that the AWSCLI is usable by being included in the PATH variable? Can this be done from inside the dockerfile itself?

like image 904
Saqib Ali Avatar asked Aug 20 '18 01:08

Saqib Ali


People also ask

Can we install AWS CLI using pip?

You can install version 1 of the AWS Command Line Interface (AWS CLI) on Windows by using a standalone installer (recommended) or pip , which is a package manager for Python.

Which Python is AWS CLI using?

Prerequisites. Before you can install the AWS CLI version 1 on macOS, be sure you have Python 3.6 or later installed. For installation instructions, see the Downloading Python page in Python's Beginner Guide.

How do I use AWS docker command line?

To run the AWS CLI version 2 Docker image, use the docker run command. This is how the command functions: docker run --rm -it amazon/aws-cli – The equivalent of the aws executable. Each time you run this command, Docker spins up a container of your downloaded amazon/aws-cli image, and executes your aws command.

What is pip install AWS CLI?

or for your user: $ python -m pip install --user awscli. If you have the aws-cli package installed and want to upgrade to the latest version, you can run: $ python -m pip install --upgrade awscli. This will install the aws-cli package as well as all dependencies.


2 Answers

You have two options here.

The first is to explicitly put the root user's local bin on the PATH with something like

ENV PATH=/root/.local/bin:$PATH

The second is to drop the --user argument on your pip install. By default, pip should write to /usr/local/bin which will be on your PATH already

like image 162
Anthony Sottile Avatar answered Nov 15 '22 10:11

Anthony Sottile


In case you just need aws on your CI server:

FROM python:alpine

ARG CLI_VERSION=1.16.310

RUN pip install --no-cache-dir awscli==$CLI_VERSION && \
    mkdir /root/.aws

VOLUME /root/.aws/

ENTRYPOINT ["/usr/local/bin/aws"]

Build and tag as aws:

docker build -t aws .

Which can be used in this way:

docker run --rm -v /root/aws-cli/.aws:/root/.aws -ti aws ecr get-login --no-include-email --region us-west-2

Or:

docker run --rm -e  AWS_ACCESS_KEY_ID=AKIABCDEFGHIJKLMONP -e "AWS_SECRET_ACCESS_KEY=abcdefghijklmnopqrstvwxyz" -ti aws ecr get-login --no-include-email --region us-west-2
like image 40
Oleg Neumyvakin Avatar answered Nov 15 '22 11:11

Oleg Neumyvakin