iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🥫

[Docker] Using Local AWS Credentials in Containers

に公開

How to Access AWS from a Docker Container Using Local Credentials

For example, let's say you want to containerize a batch process that performs S3 operations and run it on ECS.
First, you develop locally, create a Dockerfile, and then create a compose.yaml like the following.

compose.yaml
services:
  batch_aws:
    build: .

When you run docker compose up in your local environment, it will error out because there are no AWS credentials, and it cannot access AWS resources. (While embedding credentials inside the image might prevent errors, it is very bad for security.)

So, what is the best way to handle this?
One solution is to bring the local AWS credentials into the container and specify the profile via an environment variable, which allows access to AWS.

compose.yaml
services:
  batch_aws:
    build: .
    environment:
      - AWS_PROFILE={AWS profile name}    
    volumes:
      - ~/.aws/credentials:/root/.aws/credentials:ro
      - ~/.aws/config:/root/.aws/config:ro

Up to this point, you can figure this out with a little bit of research.

However, wait a moment. This makes compose.yaml usable only locally. Rewriting it only during development is tedious, and there is a risk of making mistakes. You could consider preparing separate compose.yaml files for production and development, but that is not ideal because the common parts would require duplicate management.

So, what should you do?

Let's Override Docker Compose

Let's solve it with an override. Compose files can be overwritten.
First, revert the compose.yaml to its original state.

compose.yaml
services:
  batch_aws:
    build: .

Create a compose.local.yaml and write only the additional configuration parts.

compose.local.yaml
services:
  batch_aws:
    environment:
      - AWS_PROFILE={AWS profile name}    
    volumes:
      - ~/.aws/credentials:/root/.aws/credentials:ro
      - ~/.aws/config:/root/.aws/config:ro

With this:

  • When you want to use local AWS credentials:
    • docker compose up -f compose.yaml -f compose.local.yaml
  • In other cases (such as the production environment on AWS):
    • docker compose up

You no longer need to rewrite compose.yaml every time.

Making It Switchable with a .env File

Now for the main topic.

While the previous method isn't bad, my personal opinion is that having different startup commands for production and local environments is confusing. Also, since it's a difference due to the environment, I'd like to switch it using environment variables.
Since .env files are often used to manage environment variables during app development, I want to use .env to switch the Docker environment as well.

Therefore, we will rewrite compose.yaml and compose.local.yaml so they can be switched by reading environment variables from .env.

compose.yaml
services:
  batch_aws:
    build: .
    env_file: .env
compose.local.yaml
services:
  batch_aws:
    volumes:
      - ~/.aws/credentials:/root/.aws/credentials:ro
      - ~/.aws/config:/root/.aws/config:ro

Prepare the .env file.

.env
AWS_PROFILE={AWS profile name}
COMPOSE_FILE=compose.yaml:compose.local.yaml

In this state, running docker compose up will cause compose.yaml to be overridden by compose.local.yaml, and it will run with local settings.

Empty the .env file.

.env

In this state, running docker compose up will use only compose.yaml, so it will run with production settings.

That's all.

Discussion