shell - 如何在docker镜像中替换conf文件中的文本
问题描述
我正在尝试构建一个 Docker 映像,我需要在其中获取父目录下用逗号分隔的目录列表,并将其设置在复制到容器中的配置文件中,但文本永远不会在 conf 文件中替换。下面是泊坞窗图像。或Github 链接
FROM ubuntu:16.04
LABEL maintainer="TEST"
RUN apt-get update && apt-get install vim git -y
COPY odoo.conf /etc/odoo/odoo.cfg
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world1
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world2
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world3
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world4
COPY setup.sh /setup.sh
RUN ["chmod", "+x", "/setup.sh"]
CMD ["/setup.sh"]
搜索和替换的事情发生在setup.sh但进入 shell 永远不会显示替换。但是,如果我在容器 shell 中执行命令 /setup.sh,它就可以完成这项工作。
有兴趣知道,如何做到这一点以及我做错了什么?
安装程序.sh
# get addons path
addons_path=`ls -d /mnt/extra-addons/* | paste -d, -s`
# can't use / because directory name contains, using #
sed -i -e "s#__addons__path__#${addons_path}#" /etc/odoo/odoo.cfg
/etc/odoo/odoo.conf
[options]
addons_path = __addons__path__
data_dir = /var/lib/odoo
.......
预期 /etc/odoo/odoo.conf
[options]
addons_path = /mnt/extra-addons/hellow-world1,/mnt/extra-addons/hellow-world2,/mnt/extra-addons/hellow-world3,/mnt/extra-addons/hellow-world4
data_dir = /var/lib/odoo
## 更新 我删除了中间 setup.sh 并在 Dockerfile 中完成了整个事情,看起来像
FROM ubuntu:16.04
LABEL maintainer="TEST"
RUN apt-get update && apt-get install vim git -y
COPY odoo.conf /etc/odoo/odoo.cfg
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world1
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world2
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world3
RUN git clone https://github.com/kelseyhightower/helloworld.git /mnt/extra-addons/hellow-world4
ENV addons_path=$(ls -d /mnt/extra-addons/* | paste -d, -s) ## Fails here it sets blank so sed command works but the variable addons_path doesn't have the value probably I am defining variable wrongly?
RUN sed -i -e "s#__addons__path__#$addons_path#" /etc/odoo/odoo.cfg
解决方案
尝试这个:
addons_path=$(find /mnt/extra-addons/ -type d -maxdepth 1 | tr '\n' ',')
sed -i -e "s#__addons__path__#${addons_path}#" /etc/odoo/odoo.cfg
#
如果文件名包含或换行符,这将不起作用。paste
将两个流合并为一个。你只有一个流。tr
例如,用于将换行符替换为另一个字符。- 不要解析 ls 输出。
- 不推荐使用使用 ` ` 的语法,请使用
$( ... )
.
推荐阅读
- c - 如何在 C 中撤消井字游戏中的多个步骤
- arrays - 无法循环遍历 laravel 集合数组
- sql - Pivot SQL - 标头包含 DISTINCT 和 DatePart(数据转换)字段
- html - 如何使文本框彼此居中对齐?
- r - 如何将 R 文本变量插入到保持正常格式的 Latex 表达式中
- node.js - 重新加载后无法触发消息
- python - imageio.ffmpeg.download() 需要不同的imageio安装,imageio不会安装不同的版本
- c# - 为什么我的 EF Core DbContext 不受 DI 约束?
- wordpress - 如果用户不是帖子的作者,则重定向到 URL
- asp.net-core - 由于应用程序尝试访问数据库而导致使用 EF 工具出现问题