(类似于昨天未回复的帖子,但我已经放大了我的问题的原因,因此在此处重新发布并删除昨天的帖子并减少文字)
使用 nginx 镜像可以传递全局环境变量。 Documentation .在链接上有一个部分“在 nginx 配置中使用环境变量(1.19 中的新功能)”。 运行此容器时,上述有关全局变量的功能确实按预期工作。
如果我执行到正在运行的容器中,我会在目录系统 docker-entrypoint.sh
的根级别看到一个脚本。从研究来看,听起来 nginx docker 用来传递全局变量的方法依赖于容器启动时运行的脚本,这是一个自动过程。
问题是,我的 docker-compose 有一个命令 command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx - g\"守护程序关闭;\"'"
。当我包含此命令并运行时,它会破坏全局变量的功能。通过搜索,添加命令会破坏任何入口点脚本吗?如果我不向我的 docker-compose 添加任何命令,我似乎只能使用全局变量功能,因为它会停止运行 docker-entrypoint.sh。
是否有解决此问题的“正确”方法?
有效 - 全局变量传递给 nginx:
version: "3.5"
networks:
collabora:
services:
nginx:
image: nginx
depends_on:
- certbot
- collabora
volumes:
- ./data/nginx/templates:/etc/nginx/templates
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
ports:
- "80:80"
- "443:443"
env_file: .env
networks:
- collabora
不起作用,全局变量未通过,我得到的是 default.conf 模板:
version: "3.5"
networks:
collabora:
services:
nginx:
image: nginx
depends_on:
- certbot
- collabora
volumes:
- ./data/nginx/templates:/etc/nginx/templates
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
ports:
- "80:80"
- "443:443"
env_file: .env
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
networks:
- collabora
最佳答案
看看 entrypoint script在容器内部,特别是保护大部分代码的 if
语句:
if [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then
...
fi
只有当 $1
是 nginx
或 nginx-debug
时,模板和其他入口点脚本才会运行。对于您的 Dockefile 而言,情况并非如此,其中 $1
将是 /bin/sh
。
最简单的选择是将入口点脚本替换为无条件运行入口点脚本的修改版本。有几种方法可以做到这一点,但最简单的可能是:
在您的本地目录中创建自定义
docker-entrypoint.sh
脚本(确保它是可执行的)。将其安装在
docker-compose.yml
中的库存版本之上:version: "3.5" networks: collabora: services: nginx: image: nginx depends_on: - certbot - collabora volumes: # Here is where we orverride the entrypoint script. - ./data/docker-entrypoint.sh:/docker-entrypoint.sh - ./data/nginx/templates:/etc/nginx/templates - ./data/certbot/conf:/etc/letsencrypt - ./data/certbot/www:/var/www/certbot ports: - "80:80" - "443:443" env_file: .env command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'" networks: - collabora
移除守卫 if
语句给我们:
#!/bin/sh
# vim:sw=4:ts=4:et
set -e
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
exec 3>&1
else
exec 3>/dev/null
fi
if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then
echo >&3 "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration"
echo >&3 "$0: Looking for shell scripts in /docker-entrypoint.d/"
find "/docker-entrypoint.d/" -follow -type f -print | sort -V | while read -r f; do
case "$f" in
*.sh)
if [ -x "$f" ]; then
echo >&3 "$0: Launching $f";
"$f"
else
# warn on shell scripts without exec bit
echo >&3 "$0: Ignoring $f, not executable";
fi
;;
*) echo >&3 "$0: Ignoring $f";;
esac
done
echo >&3 "$0: Configuration complete; ready for start up"
else
echo >&3 "$0: No files found in /docker-entrypoint.d/, skipping configuration"
fi
exec "$@"
关于Docker-compose 与 nginx 模板一起传递全局变量。向我的撰写添加命令会破坏此功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66732240/