简体   繁体   English

docker-compose mongodb访问/docker-entrypoint-initdb.d/脚本中的env变量

[英]docker-compose mongodb access env variables in /docker-entrypoint-initdb.d/ script

This question is based off the top answer to a previous question on the same topic 该问题基于对同一主题的先前问题的最高答案

My question is, in my custom /docker-entrypoint-initdb.d/ init script, how can I reference env variables that are declared in docker-compose's .env file? 我的问题是,在自定义/docker-entrypoint-initdb.d/初始化脚本中,如何引用在docker-compose的.env文件中声明的env变量? Meaning, env variables besides MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD. 意思是,除了MONGO_INITDB_ROOT_USERNAME和MONGO_INITDB_ROOT_PASSWORD之外的env变量。

for example: 例如:

mongo --eval "db.getSiblingDB('sibling').createUser({user: '$SIBLING_USER', pwd: '$SIBLING_PASSWORD', roles: [{ role: 'readWrite', db: 'sibling' }]})"

I did the following for an Reverse Proxy using NGINX where based on a env variable it loads a different config file. 我对使用NGINX的反向代理做了以下操作,其中基于env变量加载了不同的配置文件。

Docker-compose.yml: Docker-compose.yml:

https-proxy:
build: 
  context: ./https-proxy
  dockerfile: ./Dockerfile
  args:
    - MY_VAR=TRUE

Dockerfile: Dockerfile:

FROM nginx

ARG MY_VAR
ENV MY_VAR=${MY_VAR}
RUN bash ./etc/nginx/config.sh

config.sh: config.sh:

#!/bin/bash
if [ $MY_VAR == true ]; then
    echo 'My Var is True'
else
   echo 'My Var is False'

You could also define an .env file aside your Docker-compose.yml, so you don't have to change that file and only define the values on a different place where Docker-compose will look for them 您还可以在Docker-compose.yml旁边定义一个.env文件,因此您不必更改该文件,而只需在Docker-compose查找它们的其他位置定义值。

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

相关问题 Docker Compose MongoDB docker-entrypoint-initdb.d 不起作用 - Docker Compose MongoDB docker-entrypoint-initdb.d is not working Docker docker-entrypoint-initdb.d/ 带有 Mongodb 的脚本不起作用 - Docker docker-entrypoint-initdb.d/ scripts with Mongodb aren't working 重新创建MongoDB容器不会在docker-entrypoint-initdb.d中重新运行config.js文件 - Recreation of MongoDB container doesnt re-run config.js file in docker-entrypoint-initdb.d 使用 docker-entrypoint-initdb.d 创建的 Mongodb 用户不会保留在数据库中 - Mongodb user created with docker-entrypoint-initdb.d doesn't persist in the database 如何将环境变量传递给 mongo docker-entrypoint-initdb.d? - How can I pass environment variables to mongo docker-entrypoint-initdb.d? 存储 [main] 在 File::open() 中,::open for '/docker-entrypoint-initdb.d/create_user.js' 失败,权限被拒绝 - STORAGE [main] In File::open(), ::open for '/docker-entrypoint-initdb.d/create_user.js' failed with Permission denied mongodb与docker-compose [SocketTimeoutException] - mongodb with docker-compose [SocketTimeoutException] 通过docker-compose创建MongoDB数据库 - Create a MongoDB database through docker-compose 无法使用 Docker-compose 连接到 MongoDB - cannot connect to MongoDB with Docker-compose 使用 Docker-Compose 在 MongoDB 上导入数据 - Import Data on MongoDB using Docker-Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM