英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论