Docker-compose 与 nginx 模板一起传递全局变量。向我的撰写添加命令会破坏此功能

Dou*_*Fir 3 nginx docker docker-compose

(类似于昨天未答复的帖子,但我后来放大了问题的原因,因此用更少的文字重新发布在这里并删除了昨天的帖子)

通过 nginx 镜像,我们可以传递全局环境变量。文档。链接上有一个部分“在 nginx 配置中使用环境变量(1.19 中新增)”。运行此容器时,上述有关全局变量的功能确实按预期工作。

如果我执行到正在运行的容器中,我会在目录系统的根级别看到一个脚本docker-entrypoint.sh。根据研究,听起来 nginx docker 用于传递全局变量的方法依赖于容器启动时运行的脚本,并且这是一个自动过程。

问题是,我的 docker-compose 有一个 command command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"。当我包含此命令并运行时,它会破坏全局变量的功能。通过进行一些搜索,添加命令会破坏任何入口点脚本吗?如果我不向 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
Run Code Online (Sandbox Code Playgroud)

不起作用,全局变量没有传递,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
Run Code Online (Sandbox Code Playgroud)

lar*_*sks 6

看一下容器内的入口点脚本if,特别是保护大部分代码的语句:

if [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then
   ...
fi
Run Code Online (Sandbox Code Playgroud)

模板和其他入口点脚本仅在$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
    
    Run Code Online (Sandbox Code Playgroud)

删除守卫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 "$@"
Run Code Online (Sandbox Code Playgroud)