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

查看:46
本文介绍了在 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 command not found.

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 1.19 在根目录下有一个文件夹 /docker-entrypoint.d 放置由docker-执行的启动脚本entrypoint.sh 脚本.您还可以阅读日志上的执行情况.

NGINX 1.19 has a folder /docker-entrypoint.d on the root where place startup scripts executed by thedocker-entrypoint.sh script. You can also read the execution on the log.

/docker-entrypoint.sh:/docker-entrypoint.d/不为空,会尝试执行配置

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

/docker-entrypoint.sh:在其中寻找 shell 脚本/docker-entrypoint.d/

/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

/docker-entrypoint.sh:正在启动

/docker-entrypoint.sh: Launching

[........]

/docker-entrypoint.sh:配置完成;准备启动

/docker-entrypoint.sh: Configuration complete; ready for start up

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

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