【问题标题】:How to replace content of a file when starting a nginx docker container启动nginx docker容器时如何替换文件的内容
【发布时间】:2021-10-13 18:29:59
【问题描述】:

我想在通过 docker-compose 启动 docker nginx 容器时更改配置文件的内容。我将包含 sed 命令的脚本复制到“docker-entrypoint.d”目录,以便在启动时执行。注意:nginx 1.19 引入了一个入口点和一些脚本来改变容器启动时的配置。

但是,我遇到了以下问题:

  1. “sed”总是返回“没有这样的文件或目录”,无论我将“env.js”放在哪个路径下。我已经尝试过不同的路径。
  2. 如果在 Dockerfile 中设置了“ENTRYPOINT [“nginx”、“-g”、“daemon off;”]”,则位于“docker-entrypoint.d”中的初始化脚本将被忽略(不执行)。因此,在发布的 Dockerfile 中注释了 ENTRYPOINT。

以下是相关文件:

Dockerfile:

# 1.) build
FROM node:14.16.1-alpine3.13 AS builder
WORKDIR /app
COPY . .

# 2.) target image
FROM nginx:1.21.3-alpine
COPY --from=builder /app/myinit.sh ./docker-entrypoint.d
COPY --from=builder /app/env.js /usr/share/nginx/html

#ENTRYPOINT ["nginx", "-g", "daemon off;"]

myinit.sh:

sed -i 's~__env.apiUrlRoot.*~__env.apiUrlRoot = '$MYENV';~g' /usr/share/nginx/html/env.js

env.js:

(function (window) {
  window.__env.apiUrlRoot = 'http://localhost:9090';

}(this));

docker-compose.yml:

version: '3.8'

services:
  mytest:
    image: 'tst:latest'
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: tst1
    environment:
      - MYENV=SomeValue

docker-compose 命令:

docker-compose -f docker-compose.yml up --build

【问题讨论】:

    标签: docker nginx docker-compose


    【解决方案1】:

    我找到了第一个问题的根本原因:“myinit.sh”包含 windows 样式的 CRLF 而不是 unix 样式的 LF。我改变了它并且shell脚本正确执行。顺便说一句:您可以运行“dos2unix”将 CRLF 替换为 LF。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 2020-01-28
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      相关资源
      最近更新 更多