Docker compose无法解析容器内配置文件的环境变量。

huangapple go评论80阅读模式
英文:

Docker compose not parsing env variables for the config file inside the container

问题

我们正在本地尝试一些Docker容器。出于安全目的,用户和密码被用作配置文件中的环境变量。配置文件作为卷在docker-compose中复制给其中一个API。在docker-compose up之后,在容器内部,我们仍然看到的是变量名而不是环境变量的值。

容器内部复制的配置文件作为卷:

dbconfig:

dbuser: ${USER}
dbpass: ${PASSWORD}
dbname: 
dbdrivername: 
tablename

docker-compose.yaml:

services:

api:
    image: ${API_IMAGE:api}:${VERSION:-latest}
    ports:
        - 8080:8080
    environment:
     - "USER=${USER}"
     - "PASSWORD=${PASSWORD}"
    volumes:
        - ./conf/config.yaml:/etc/api.yaml
    command: ["-config", "/etc/api.yaml"]

Config.yaml:

dbconfig:

dbuser: ${USER}
dbpass: ${PASSWORD}
dbname: 
dbdrivername: 
tablename

请帮助我们解决这个错误,因为我们刚刚开始采用Docker进行测试。

英文:

We are trying some docker containers locally. For security purposes, user and password are used as env variables in the config file. The config file is copied as the volume in the docker-compose for one of the APIs. After docker-compose up, inside the container, we are still seeing the variable name and not the env variable value.

Config file inside the container copied as volume:

dbconfig:

dbuser: ${USER}
dbpass: ${PASSWORD}
dbname: 
dbdrivername: 
tablename

docker-compose.yaml:

services:

api:
    image: ${API_IMAGE:api}:${VERSION:-latest}
    ports:
        - 8080:8080
    environment:
     - "USER=${USER}"
     - "PASSWORD=${PASSWORD}"
    volumes:
        - ./conf/config.yaml:/etc/api.yaml
    command: ["-config", "/etc/api.yaml"]

Config.yaml:

dbconfig:

dbuser: ${USER}
dbpass: ${PASSWORD}
dbname: 
dbdrivername: 
tablename

Please help us get rid of this error as we are newly adopting docker testing

答案1

得分: 1

在这里提到的解决方案已经修复了问题。https://stackoverflow.com/questions/71916469/how-to-run-2-different-commands-from-docker-compose-command
我们在入口脚本中添加了sed命令,该命令在配置文件中搜索环境变量并将其替换为相应的值。环境变量是从docker-compose传递给服务的。

sed \
-e "s/USER/${USER}/g" \
-e "s/PASSWORD/${PASSWORD}/g" \
-i /etc/api.yaml
英文:

Issue fixed with the solution mentioned here. https://stackoverflow.com/questions/71916469/how-to-run-2-different-commands-from-docker-compose-command
We added the sed command in the entry point script which searches for the env variable inside the config and replaces it with the value. Env variables are passed from docker-compose for the service

sed \ 
-e "s/USER/${USER}/g" \
 -e "s/PASSWORD/${PASSWORD}/g" \ -i /etc/api.yaml

huangapple
  • 本文由 发表于 2022年4月19日 02:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/71915441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定