Docker入门点脚本未采购文件
我有一个带有Docker的入门点脚本,该脚本正在执行。但是,它只是没有运行源命令来源源,以源为ENV值。
这是来自Dockerfile的相关部分,
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["-production"]
我尝试了2个版本的入门点脚本。他们俩都没有工作。
版本1
#!/bin/bash
cat >> /etc/bash.bashrc <<EOF
if [[ -f "/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env" ]]
then
echo "${SERVICE_NAME}.env found ..."
set -a
source "/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env"
set +a
fi
EOF
echo "INFO: Starting ${SERVICE_NAME} application, environment:"
exec -a $SERVICE_NAME node .
版本2
ENV_FILE=/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env
if [[] -f "$ENV_FILE" ]; then
echo "INFO: Loading environment variables from file: ${ENV_FILE}"
set -a
source $ENV_FILE
set +a
fi
echo "INFO: Starting ${SERVICE_NAME} application..."
exec -a $SERVICE_NAME node .
上述版本2 prints to log已找到文件,但是,源命令根本没有将文件内容加载到内存中。我通过运行env
命令来检查内容是否已加载。
我已经尝试了3天了,我一直在尝试几件事,但没有进步。请有人可以帮我吗?请注意,我是Docker的新手,这使事情变得非常困难。
I have an entrypoint script with docker which is getting executed. However, it just doesn't run the source command to source a file full of env values.
Here's the relevant section from tehe dockerfile
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["-production"]
I have tried 2 version of entrypoint script. Neither of them are working.
VERSION 1
#!/bin/bash
cat >> /etc/bash.bashrc <<EOF
if [[ -f "/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env" ]]
then
echo "${SERVICE_NAME}.env found ..."
set -a
source "/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env"
set +a
fi
EOF
echo "INFO: Starting ${SERVICE_NAME} application, environment:"
exec -a $SERVICE_NAME node .
VERSION 2
ENV_FILE=/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env
if [[] -f "$ENV_FILE" ]; then
echo "INFO: Loading environment variables from file: ${ENV_FILE}"
set -a
source $ENV_FILE
set +a
fi
echo "INFO: Starting ${SERVICE_NAME} application..."
exec -a $SERVICE_NAME node .
Version 2 of above prints to the log that it has found the file however, source command simply isn't loading the contents of file into memory. I check if contents have been loaded by running the env
command.
I've been trying few things for 3 days now with no progress. Please can someone help me? Please note I am new to docker which is making things quite difficult.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的第二版快到了。
通常,Docker根本不阅读或使用Shell Dotfiles。对于Docker来说,这并不是什么特别的,只是您在序列的任何时候都没有运行“交互式”或“登录”外壳。在您的第一个形式中,您会写出
.bashrc
文件,然后exec node
,而那里的任何内容都没有重新阅读互联网。您在问题中提到您使用
env
命令来检查环境。如果这是通过docker exec
的,它将在容器内启动一个新过程,但它不是入口点脚本的孩子,因此任何发生的设置都不会可见docker exec
。这通常不是问题。我可以建议一些清理工作,这可能会使看到的影响更容易。最大的是将
节点
从入口点脚本拆分。如果您同时具有entrypoint
和cmd
,则 docker将cmd
作为参数传递给entry> entrypoint
;如果将入口点脚本更改为exec“ $@”
结束,则它将运行任何传递的内容。然后在Dockerfile中,将
Node
调用作为主命令,重要的是,很容易覆盖命令,但请将入口点完整。因此,如果运行
将启动临时容器,但是将
env
作为命令传递,而不是node。
。这将通过入口点脚本中的步骤,包括设置环境,然后立即打印出环境并立即退出。这将使您观察变化。I think your second version is almost there.
Normally Docker doesn't read or use shell dotfiles at all. This isn't anything particular to Docker, just that you're not running an "interactive" or "login" shell at any point in the sequence. In your first form you write out a
.bashrc
file but thenexec node
, and nothing there ever re-reads the dotfile.You mention in the question that you use the
env
command to check the environment. If this is viadocker exec
, that launches a new process inside the container, but it's not a child of the entrypoint script, so any setup that happens there won't be visible todocker exec
. This usually isn't a problem.I can suggest a couple of cleanups that might make it a little easier to see the effects of this. The biggest is to split out the
node
invocation from the entrypoint script. If you have both anENTRYPOINT
and aCMD
then Docker passes theCMD
as arguments to theENTRYPOINT
; if you change the entrypoint script to end withexec "$@"
then it will run whatever it got passed.And then in the Dockerfile, put the
node
invocation as the main commandThe important thing with this is that it's easy to override the command but leave the entrypoint intact. So if you run
that will launch a temporary container, but passing
env
as the command instead ofnode .
. That will go through the steps in the entrypoint script, including setting up the environment, but then print out the environment and exit immediately. That will let you observe the changes.