Py学习  »  docker

如何在docker镜像中替换conf文件中的文本

Yogesh Kushwaha • 6 年前 • 2404 次点击  

我试图建立一个Docker映像,在那里我需要在父目录下用逗号分隔的目录列表,并在复制到容器中的配置文件中设置,但在conf文件中文本永远不会被替换。下面是Docker的图片。或 Github Link

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中执行命令/setup.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

γ更新 我删除了intermediate 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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44980
文章 [ 3 ]  |  最新文章 6 年前
BMitch
Reply   •   1 楼
BMitch    7 年前

容器是运行进程的包装器(该包装器是命名空间和cgroup)。正在运行的进程由dockerfile的entrypoint和cmd行定义。您可以重写运行容器时要运行的映像的默认进程,对于cmd的值,重写涉及在映像名称后传递另一个命令。

所以当你的dockerfile以:

COPY setup.sh /setup.sh
RUN ["chmod", "+x", "/setup.sh"]
CMD ["/setup.sh"]

您已经在图像中定义了cmd的默认值。但是当你跑的时候:

docker build -t docker-test .; docker run -it docker-test bash

这个 ./setup.sh CMD值替换为 bash . 这意味着 setup.sh 永远不会跑。


你可以用几种方法解决这个问题。

  1. 你可以跑你的 塞特普什 作为图像构建的一部分。如果脚本不依赖于容器的运行方式(例如外部卷、传入的环境变量等),则这是更好的选择。

  2. 将脚本移动到入口点,并通过运行提供的命令完成脚本。当你同时定义一个入口点和一个CMD时,容器只运行一个进程,所以Docker的行为是把CMD作为参数传递给入口点。要运行cmd,需要在entrypoint脚本中执行该操作。


选项1看起来像您已经完成的解决方案,我建议您的答案是:

COPY setup.sh /setup.sh
RUN ["chmod", "+x", "/setup.sh"]
RUN ["/setup.sh"]
CMD bash

您需要在脚本的顶部包含shell,以便Linux知道如何运行它:

#!/bin/sh
# The #!/bin/sh above is important, you can also replace that with the path to bash
# 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

如果每次运行容器时/mnt/extra addons/changes,选项2很有用。这看起来像:

COPY setup.sh /setup.sh
RUN ["chmod", "+x", "/setup.sh"]
ENTRYPOINT ["/setup.sh"]
CMD ["bash"]

在安装脚本中添加一行:

#!/bin/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

# this next line runs the passed arguments as pid 1, replacing this script
# this is how you run an entrypoint and fall through to a cmd
exec "$@"
Yogesh Kushwaha
Reply   •   2 楼
Yogesh Kushwaha    7 年前

我想诀窍是执行.sh文件

不工作

CMD ["/setup.sh"]

工作

RUN /bin/bash -c "/setup.sh"

最终结果

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)
#RUN sed -i -e "s#__addons__path__#NEW_PATH#" /etc/odoo/odoo.cfg

COPY setup.sh /setup.sh
RUN ["chmod", "+x", "/setup.sh"]
RUN /bin/bash -c "/setup.sh"
Kamil Cuk
Reply   •   3 楼
Kamil Cuk    7 年前

试试这个:

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. 使用``的语法已弃用,请使用 $( ... )