• 请不要在回答技术问题时复制粘贴 AI 生成的内容
daijinming
V2EX  ›  程序员

Dockerfile 中能否修改应用的连接字符串

  •  
  •   daijinming · Feb 22, 2019 · 3155 views
    This topic created in 2649 days ago, the information mentioned may be changed or developed.

    我考虑在编写 Dockerfile 的时候,将应用的连接字符串作为环境变量传入,在命令 ENTRYPOINT ["dotnet", "aspnetapp.dll"] 前,根据传入的环境变量修改容器中的配置文件,不知道如何编写,主要是对 linux 命令语法不是熟悉。Dockerfile 文件 http://elef.top/6731092196594862111.code ,应用配置文件 appsettings.json http://elef.top/2642614837288795575.code

    7 replies    2019-02-22 16:43:30 +08:00
    cospotato
        1
    cospotato  
       Feb 22, 2019
    还是写个脚本处理吧
    daijinming
        2
    daijinming  
    OP
       Feb 22, 2019
    @cospotato 我考虑应用的镜像是一个,但是每次 docker run 的时候传入 不同的连接字符串,你说的脚本怎么个意思
    Sothoth
        3
    Sothoth  
       Feb 22, 2019
    可以写个脚本读环境变量然后用 sed 替换你的配置文件,在脚本的最后调用你的 dotnet,ENTRYPOINT 里面用脚本启动就行了。
    daijinming
        4
    daijinming  
    OP
       Feb 22, 2019
    @Sothoth 写个能接收参数的脚本也好,能否指定写个小段
    kiddingU
        5
    kiddingU  
       Feb 22, 2019   ❤️ 1
    加个 entrypoints.sh,在脚本里面 sed 替换
    daijinming
        6
    daijinming  
    OP
       Feb 22, 2019
    @kiddingU 求个例子朋友,linux 新手,真实不太懂
    Sothoth
        7
    Sothoth  
       Feb 22, 2019   ❤️ 1
    @daijinming 大概这样 注意替换的对象要唯一 如果有重复要多加点字符做匹配
    环境变量:
    ENV_DATABASE=testdb

    脚本:
    #!/bin/bash
    sed -i "s?mydb?${ENV_DATABASE}?g" `grep "mydb" -rl /usr/local/tomcat/webapps/`
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3206 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 14:06 · PVG 22:06 · LAX 07:06 · JFK 10:06
    ♥ Do have faith in what you're doing.