在NGINX docker容器中启动之前运行bash脚本 [英] Running a bash script before startup in an NGINX docker container

查看:725
本文介绍了在NGINX docker容器中启动之前运行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用docker在localhost:8000上运行一个javascript应用程序。我想做的部分是基于docker run命令交换一些配置文件,我想将环境变量传递到容器中,以便bash脚本可以将其用作参数。

I'm trying to run a javascript app on localhost:8000 using docker. Part of what I would like to do is swap out some config files based on the docker run command, I'd like to pass an environment variable into the container so that the bash script can use that as a parameter.

我的dockerfile看起来是这样的:

What my dockerfile is looking like is this:

FROM nginx
COPY . /usr/share/nginx/html
CMD ["bash","/usr/share/nginx/html/runfile.sh"]

bash脚本如下所示:

And the bash script looks like this:

#!/bin/bash
if [ "$SECURITY_VERSION" = "OPENAM" ]; then
    sed -i -e 's/localhost/openam/g' authConfig.js
fi

docker run -p 8000:80 missioncontrol:latest -e SECURITY_VERSION="TEST"

Docker给我一个例外,说找不到-e exec命令。

Docker gives me an exception saying -e exec command not found.

但是,如果我将dockerfile更改为使用ENTRYPOINT而不是CMD,则-e标志有效但网络服务器无法启动。

However if I change the dockerfile to use ENTRYPOINT instead of CMD, the -e flag works but the webserver does not start up.

这里有什么我想念的吗?是否覆盖ENTRYPOINT?

Is there something I'm missing here? Is the ENTRYPOINT being overriden or something?

编辑:

所以我更新了我的dockerfile以使用 ENTRYPOINT [bash,/ usr / share / nginx / html / runfile.sh,;,nginx -g daemon off;]

So I've updated my dockerfile to use ENTRYPOINT ["bash","/usr/share/nginx/html/runfile.sh", ";", " nginx -g daemon off;"]

但是docker容器仍然关闭。是否有我遗漏的东西?

But the docker container still shuts down. Is there something I'm missing?

推荐答案

nginx容器已经定义了ENTRYPOINT。如果你定义了CMD,它将把它们组合起来,就像'ENTRYPOINT CMD'一样,CMD成为ENTRYPOINT的参数。这就是为什么你需要重新定义ENTRYPOINT以使其正常工作。

nginx container already defines ENTRYPOINT. If you define also CMD it will combine them both like 'ENTRYPOINT CMD' in such way that CMD becomes argument of ENTRYPOINT. That is why you need to redefine ENTRYPOINT to get it working.

通常ENTRYPOINT以这种方式定义,如果你也传递CMD,它将由ENTRYPOINT脚本执行。但是,每个容器都可能不是这种情况。

Usually ENTRYPOINT is defined in such way, that if you also pass CMD, it will be executed by ENTRYPOINT script. However this might not be case with every container.

这篇关于在NGINX docker容器中启动之前运行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆