镜像自动注入配置解析

标准设计

         每个标准的镜像根目录都有一个启动脚本和自定义执行脚本目录

        image

        entrypoint.sh   这个脚本名字一般是固定的

        docker-entrypoint.d

        docker-entrypoint-initdb.d

        不同服务镜像的启动脚本目录的名字有可能不太一样,但是启动的机制是完全一致的。

   nginx镜像模板配置示例

FROM registry.harbor.com:58443/library/nginx:latest
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone
COPY html /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx.conf.template /etc/nginx/templates/taishi.conf.template
COPY server.crt /usr/share/nginx/server.crt
COPY server.key.unsecure /usr/share/nginx/server.key.unsecure
Dockerfile

     image

#!/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 [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then
    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
fi

exec "$@"
docker-entrypoint.sh
#!/bin/sh

set -e

ME=$(basename $0)

auto_envsubst() {
  local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
  local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
  local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"

  local template defined_envs relative_path output_path subdir
  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
  [ -d "$template_dir" ] || return 0
  if [ ! -w "$output_dir" ]; then
    echo >&3 "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
    return 0
  fi
  find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do
    relative_path="${template#$template_dir/}"
    output_path="$output_dir/${relative_path%$suffix}"
    subdir=$(dirname "$relative_path")
    # create a subdirectory where the template file exists
    mkdir -p "$output_dir/$subdir"
    echo >&3 "$ME: Running envsubst on $template to $output_path"
    envsubst "$defined_envs" < "$template" > "$output_path"
  done
}

auto_envsubst

exit 0
20-envsubst-on-templates.sh

    image

     image

     docker run  -e PROXY_PASS_URL=http://192.168.30.123:30089/ registry.harbor.com:58443/zhonghaiyou/web:latest

     进入容器查看生成的配置文件

     image

      动态指定配置文件的路径

          docker run  -e PROXY_PASS_URL=http://192.168.30.123:30089/ -e NGINX_ENVSUBST_TEMPLATE_DIR=/etc/nginx/templates -e NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx  registry.harbor.com:58443/zhonghaiyou/web:latest

     image

    image

       如果需要对一个镜像进行定制化功能的开发,首先应该先研究容器根目录下的docker-entrypoint.sh 和docker-entrypoint.d下的脚本文件 通过修改这两个地方的脚本可以大大扩展原来镜像的功能

posted @ 2026-08-13 15:24  不懂123  阅读(5)  评论(0)    收藏  举报