简体   繁体   English

如何从本地文件读取值到Docker-compose环境变量?

[英]How to read values from local file into Docker-compose environment variables?

I'm trying to inject my AWS Credentials from my local ~/.aws/credentials file into a Docker container by setting environment variables in my docker-compose.yml file. 我正在尝试通过在docker-compose.yml文件中设置环境变量,将我的AWS凭证从我的本地~/.aws/credentials文件注入Docker容器。

But I don't know how to read the credentials from the local file into the the docker-compose file. 但我不知道如何将本地文件中的凭据读入docker-compose文件。 How can I do it?? 我该怎么做??

Here is what my AWS credentials file looks like: 以下是我的AWS凭证文件的样子:

$ cat ~/.aws/credentials
[default]
aws_access_key_id = AK_FAKE_KEY_88RD3PNY
aws_secret_access_key = BividQsWW_FAKE_KEY_MuB5VAAsQNJtSxQQyDY2C

Here is what my the relevant part of my Docker compose file looks like: 以下是我的Docker撰写文件的相关部分:

  my_service:
    build: .
    image: my_image
    environment:
         - AWS_ACCESS_KEY_ID=<What should I put here?>
         - AWS_SECRET_ACCESS_KEY=<What should I put here?>

Does it need to be from your credentials file? 它需要来自您的凭证文件吗?

You could create ~/aws_env_creds containing 你可以创建包含的~/aws_env_creds

AWS_ACCESS_KEY_ID=AK_FAKE_KEY_88RD3PNY
AWS_SECRET_ACCESS_KEY=BividQsWW_FAKE_KEY_MuB5VAAsQNJtSxQQyDY2C

And then 然后

my_service:
  build: .
  image: my_image
  env_file:
    - ~/aws_env_creds

IMHO, generally speaking, it's not a good idea to hard-code home directory in docker-compose.yml , it's not going to be portable for others. 恕我直言,一般来说,在docker-compose.yml主目录进行硬编码并不是一个好主意,它对其他人来说不是可移植的。 I'll not object to having ~/.aws/credentials though since it's a convention that everyone will follow 我不反对拥有〜/ .aws /凭证,因为这是一个每个人都会遵循的惯例

my_service:
    build: .
    image: my_image
    environment:
         - AWS_ACCESS_KEY_ID
         - AWS_SECRET_ACCESS_KEY
    volumes:
         - ~/.aws:/root/.aws:ro

This docker-compose.yml will be flexible enough that it depends on the setting of the machine, either the credential file or the environment variables will be used. 这个docker-compose.yml将足够灵活,它取决于机器的设置,将使用凭证文件或环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM