V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
daijinming
V2EX  ›  程序员

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

  •  
  •   daijinming · 2019-02-22 16:04:49 +08:00 · 2387 次点击
    这是一个创建于 1880 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    脚本:
    #!/bin/bash
    sed -i "s?mydb?${ENV_DATABASE}?g" `grep "mydb" -rl /usr/local/tomcat/webapps/`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3587 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:07 · PVG 19:07 · LAX 04:07 · JFK 07:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.