You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Gitlab CI/CD环境变量传入Mongo的docker-entrypoint-initdb.d?

Solution for Using GitLab CI/CD Env Vars in MongoDB Init Scripts

Got it, let's work through this— I’ve dealt with exactly this scenario when setting up MongoDB with GitLab CI/CD, so I know the quirks of the Docker entrypoint scripts. The issue you’re hitting is that the official MongoDB Docker entrypoint runs scripts in docker-entrypoint-initdb.d in a specific context (against a temporary, unauthenticated mongod instance) and plain shell scripts need to account for that to work properly.

Here are two reliable approaches to get your GitLab CI/CD env vars into the initialization logic:

Approach 1: Use envsubst to Generate a Dynamic JS Script

This method uses a template file with placeholder variables, then substitutes them with your GitLab CI/CD env vars at runtime before executing the script. It’s clean and handles special characters in passwords well.

  1. Create a template JS file (e.g., init.js.tpl) with placeholders for your env vars:
db.createUser({
  user: "${MONGO_ADMIN_USER}",
  pwd: "${MONGO_ADMIN_PWD}",
  roles: [{ role: "root", db: "admin" }]
});

// Add any other initialization logic here, using ${VAR_NAME} for env vars
  1. Write a shell script (e.g., 00-init.sh) to handle substitution and execution. Place both files in docker-entrypoint-initdb.d:
#!/bin/bash
set -euo pipefail

# Substitute env vars into the template to create a valid init.js
envsubst < /docker-entrypoint-initdb.d/init.js.tpl > /docker-entrypoint-initdb.d/init.js

# Wait for the temporary mongod instance (started by the entrypoint) to be ready
until mongo admin --eval "db.adminCommand('ping')" >/dev/null 2>&1; do
  echo "Waiting for MongoDB to be ready..."
  sleep 2
done

# Execute the generated init script
mongo admin /docker-entrypoint-initdb.d/init.js
  1. Set permissions in your Dockerfile:
COPY docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/00-init.sh
  1. Pass GitLab CI/CD vars to your container:
    In your GitLab CI/CD job, define the variables in your project’s CI/CD settings, then pass them to the Docker run command (or docker-compose) using -e flags:
docker run -d \
  -e MONGO_ADMIN_USER=$MONGO_ADMIN_USER \
  -e MONGO_ADMIN_PWD=$MONGO_ADMIN_PWD \
  your-mongo-image:latest

Approach 2: Directly Inject Env Vars into a Shell Script

If you prefer not to use a template, you can construct the MongoDB commands directly in your shell script, leveraging bash variable expansion. Just be careful with special characters in your env vars (e.g., $, !).

Here’s what your 00-init.sh would look like:

#!/bin/bash
set -euo pipefail

# Wait for MongoDB to be ready (critical for the entrypoint context)
until mongo admin --eval "db.adminCommand('ping')" >/dev/null 2>&1; do
  echo "Waiting for MongoDB to be ready..."
  sleep 2
done

# Run the user creation command with env vars
mongo admin --eval "
db.createUser({
  user: '$MONGO_ADMIN_USER',
  pwd: '$MONGO_ADMIN_PWD',
  roles: [{ role: 'root', db: 'admin' }]
});
"

Key Fixes for Your Earlier Failed Attempt

The most common reason shell scripts fail with the MongoDB entrypoint is not waiting for the temporary mongod instance to be ready. The entrypoint starts this instance in the background before running your scripts, so you need the until mongo ping loop to ensure the database is accessible before running your commands.

Also, make sure your GitLab CI/CD variables are properly passed to the container— double-check they’re visible in your job by adding echo $MONGO_ADMIN_USER to your CI script to verify they’re set.

内容的提问来源于stack exchange,提问作者Javier Guzmán

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 18:42:58