Zha*_* Yi 5 docker docker-compose
我正在创建一个需要一些环境变量的 docker compose 文件。其中一个环境变量来自 aws ssm 参数。因此,当我构建 docker 映像并将该值作为环境变量之一时,我需要从 aws ssm 查询该值。我怎样才能在 docker compose 文件中做到这一点?
version: "2.3"
services:
base:
build:
context: .
args:
- PYTHON_ENV=developmen
- API_KEY= # find the value from ssm
Run Code Online (Sandbox Code Playgroud)
没有简单的方法可以ARGs从子 shell 处理 docker-compose 文件。但你可以这样做docker build命令和带有键值的 docker-compose 来完成此操作。
使用 docker-compose 命令:
MY_KEY=$(aws ssm get-parameter --name "test" --output text --query Parameter.Value) docker-compose build --no-cache
Run Code Online (Sandbox Code Playgroud)
docker 撰写
version: "2.3"
services:
base:
build:
context: .
args:
- PYTHON_ENV=developmen
- API_KEY=${MY_KEY}
Run Code Online (Sandbox Code Playgroud)
在 Dockerfile 中定义 ARG,并在构建时运行 subshell 以获取 SSM 参数值。
FROM alpine
ARG API_KEY=default
ENV API_KEY="$API_KEY"
RUN echo "API_KEY is : $API_KEY"
Run Code Online (Sandbox Code Playgroud)
在构建过程中使用获取值aws-cli
docker build --no-cache --build-arg API_KEY="$(aws ssm get-parameter --name "test" --output text --query Parameter.Value)" -t myimage .
Run Code Online (Sandbox Code Playgroud)
使用 docker-compose 您还可以尝试使用系统环境变量。
version: "2.3"
services:
base:
build:
context: .
args:
- PYTHON_ENV=developmen
- API_KEY=${MY_KEY}
Run Code Online (Sandbox Code Playgroud)
在 docker-compose 之前将其导出为 ENV。
export MY_KEY=$(aws ssm get-parameter --name "test" --output text --query Parameter.Value) && docker-compose build --no-cache
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6639 次 |
| 最近记录: |