【问题标题】:Docker Golang onbuild custom action at docker rundocker run 上的 Docker Golang onbuild 自定义操作
【发布时间】:2016-09-28 17:13:34
【问题描述】:

我有一个用于 Go 应用程序的相当简单的 Dockerfile:

FROM golang:onbuild
EXPOSE 5000 5001

现在,当我运行它时(不是在构建图像时!)我想更改静态提供的 html 文件中的值,可能使用 sed 来使用先前使用 -e 指定的环境变量到 docker跑。我该怎么做?

【问题讨论】:

    标签: go docker dockerfile


    【解决方案1】:

    你可以试试这样的:

    FROM golang:onbuild
    COPY ./docker-entrypoint.sh /
    
    EXPOSE 5000 5001
    ENTRYPOINT ["/docker-entrypoint.sh"]
    

    注意:您需要将带有 sed 脚本的入口点文件复制到容器中(在第 2 行完成)

    docker-entrypoint.sh 与您的 sed 脚本

    # execute sed only **if variable exists**
    sed -ri "s/<title>.*/<title>$ENVIRONMENT_VARIABLE</title>/" /path_to_html_file/index.html
    

    - index.html 文件中带有新标题的替换行的简单示例(作为环境变量传入运行)

    注意:请记住,仅当环境变量作为 @STLMikey 显示时才运行 sed。

    并使用 $ENVIRONMENT_VARIABLE 运行 docker 容器

    例如:

    docker run -d -t -i -e ENVIRONMENT_VARIABLE='New website title' --name=container_name IMAGE_NAME 
    

    【讨论】:

      【解决方案2】:

      一个选项是在 docker run 期间使用--entrypoint 选项:

      docker run --entrypoint /some/sh/script.sh <imagename>
      

      其中 script.sh 是您的容器的一个文件,其中包含以下内容:

      #!/bin/env bash    
      if [[ "$myenvvar" == 'myvalue' ]]; then
          # sed your file here
      else
          #don't do that
      fi
      

      如果您不喜欢在运行时覆盖入口点的想法,您可以考虑在 Dockerfile 中使用 CMDENTRYPOINT 选项。

      【讨论】:

        猜你喜欢
        • 2015-11-05
        • 1970-01-01
        • 2020-06-04
        • 1970-01-01
        • 2016-06-18
        • 2021-10-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多