gpt4 book ai didi

docker - 无法在容器启动时运行自定义脚本

转载 作者:行者123 更新时间:2023-12-02 19:03:27 29 4
gpt4 key购买 nike

我正在尝试设置Clair(Docker图像漏洞扫描程序工具)。 https://github.com/coreos/clair

我让Clair使用docker-compose在本地正常工作。问题是,当我将其部署到AWS时,我需要指定postgres服务器地址,用户名和密码等。构建镜像时未知postgres服务器地址,因此在构建Clair docker镜像时无法将其包括在内。它需要在容器/镜像启动时自定义。

对于其他使用数据库的应用程序,我通常只是自定义docker镜像,并添加一个脚本(该脚本在启动时运行),该脚本使用SED将正确的值(来自环境变量)插入到应用程序配置文件中。

例如:

Dockerfile

FROM quay.io/coreos/clair:latest

COPY /docker/clair/runtime.sh /runtime.sh
RUN chmod +x /runtime.sh

CMD ["/runtime.sh"]

runtime.sh
sed -i -e "s,#POSTGRES_SERVER#,$POSTGRES_SERVER,g" config.yaml
由于某种原因,上述方法不适用于Clair docker 镜像。这种方法适用于许多其他主流图像,所以我认为这对Clair图像来说是很特别的。

我没有任何错误,它只是忽略了我的dockerfile中的 CMD ["/runtime.sh"]并像通常那样启动。

有谁能指出如何运行自定义脚本或指出实现同一目标的另一种方法?

=========== 解决方案更新 ===========

问题在于,Clair图像基于BusyBox,默认情况下使用 ash shell,而我编写/使用的shell脚本是为 bash shell编写的。也许这应该是显而易见的,但是我对编写Linux Shell脚本还是有些陌生,并且还没有碰到这一点。

在测试了mchawre的答案之后,我意识到了这一点,这避免了我遇到的问题,因为它不使用shell脚本。

因此,我使用的解决方案是将 bash安装到镜像中,然后可以在容器启动时使用我通常的bash shell脚本。

Dockerfile
FROM quay.io/coreos/clair:latest

RUN apk --no-cache upgrade
RUN apk add --no-cache curl py-pip bash postgresql-client figlet \
&& curl -L https://github.com/optiopay/klar/releases/download/v2.4.0/klar-2.4.0-linux-amd64 \
> /usr/local/bin/klar \
&& chmod +x /usr/local/bin/klar \
&& pip install awscli

# Copy in custom Clair config file
COPY /docker/clair/config.yaml /etc/clair/config.yaml

# Env Vars for use with Klar CLI
ENV CLAIR_ADDR http://127.0.0.1:6060

# Copy runtime script & make it executable
COPY /docker/clair/runtime.sh /runtime.sh
RUN chmod +x /runtime.sh

# Override the parent images ENTRYPOINT
# Run a script on container startup which does a few things in addition to starting Clair at the end.
# Note, this script is a BASH script. It is critical that you install bash into the docker image or this script will
# fail with errors that are not very helpful.
ENTRYPOINT ["/runtime.sh"]

runtime.sh (小片段)
#!/bin/bash

echo "======= Configuring config.yaml ====="
sed -i -e "s,#POSTGRES_USER#,$POSTGRES_USER,g" /etc/clair/config.yaml
sed -i -e "s,#POSTGRES_PASSWORD#,$POSTGRES_PASSWORD,g" /etc/clair/config.yaml
sed -i -e "s,#POSTGRES_URL#,$POSTGRES_URL,g" /etc/clair/config.yaml

/clair -config=/etc/clair/config.yaml

最佳答案

请检查this

sed命令放在dockerfile CMD中应该可以工作。
CMD sed -i "s/localhost/$DB_HOST/" /config/config.yaml && exec /clair -config=/config/config.yaml

关于docker - 无法在容器启动时运行自定义脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610661/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com