Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

How to Run AWS CLI in Docker

2 min read

docker cli

This document provides a step-by-step guide on how to run the AWS Command Line
Interface (CLI) within a Docker container. The AWS CLI is a powerful tool that allows users to
interact with AWS services from the command line. By using Docker, you can create a
consistent environment for running the AWS CLI without needing to install it directly on your
local machine

Prerequisite

Prerequisites

Before you begin, ensure you have the following:

  • Docker must be installed on your machine.
  • AWS account with appropriate permissions to access the services you intend to
    use.
  • AWS credentials configured (Access Key ID and Secret Access Key).

Step 1: Pull the AWS CLI Docker Image

You can start by pulling the official AWS CLI Docker image from Docker Hub. Open your
terminal and run the following command:



docker pull amazon/aws-cli

  

This command downloads the latest version of the AWS CLI image.

Step 2: Run the AWS CLI in Docker

Once the image is downloaded, you can run the AWS CLI commands using Docker. You need
to pass your AWS credentials as environment variables. This is how to do it:


docker run --rm -it \
-e AWS_ACCESS_KEY_ID=your_access_key_id \
-e AWS_SECRET_ACCESS_KEY=your_secret_access_key \
amazon/aws-cli 
  

Prerequisites

Replace command with the AWS CLI command you want to execute. For example, to list
your S3 buckets, you would run:



docker run --rm -it \
-e AWS_ACCESS_KEY_ID=your_access_key_id \
-e AWS_SECRET_ACCESS_KEY=your_secret_access_key \
amazon/aws-cli s3 ls

Step 3: Using a Configuration File (Optional)

If you prefer to use a configuration file instead of passing credentials as environment
variables, you can mount your AWS credentials and config files into the Docker container.
First, ensure your AWS credentials are stored in <~/.aws/credentials>
and your configuration in <~/.aws/config>.

Prerequisites

You can run the AWS CLI with the following command:


docker run --rm -it \
-v ~/.aws:/root/.aws \
amazon/aws-cli command

This command mounts your local <.aws> directory into the container, allowing the AWS CLI to use your existing configuration.

Step 4: Additional Options

You can also specify the region and output format by adding additional environment
variables or command-line options. For example:


docker run --rm -it \
-e AWS_ACCESS_KEY_ID=your_access_key_id \
-e AWS_SECRET_ACCESS_KEY=your_secret_access_key \
-e AWS_DEFAULT_REGION=us-east-1 \
amazon/aws-cli s3 ls --output json

Conclusion

Running the AWS CLI in Docker provides a flexible and isolated environment for managing
AWS resources. By following the steps outlined in this document, you can easily execute
AWS CLI commands without the need for local installation. This approach is particularly useful
for maintaining consistency across different development environments.

References

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index