首页 > 解决方案 > 如何在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

标签: shelldockersed

解决方案


尝试这个:

addons_path=$(find /mnt/extra-addons/ -type d -maxdepth 1 | tr '\n' ',')
sed -i -e "s#__addons__path__#${addons_path}#" /etc/odoo/odoo.cfg
  1. #如果文件名包含或换行符,这将不起作用。
  2. paste将两个流合并为一个。你只有一个流。tr例如,用于将换行符替换为另一个字符。
  3. 不要解析 ls 输出。
  4. 不推荐使用使用 ` ` 的语法,请使用$( ... ).

推荐阅读