Docker入门点脚本未采购文件

发布于 2025-01-26 01:27:37 字数 1107 浏览 4 评论 0原文

我有一个带有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2025-02-02 01:27:37

我认为您的第二版快到了。

通常,Docker根本不阅读或使用Shell Dotfiles。对于Docker来说,这并不是什么特别的,只是您在序列的任何时候都没有运行“交互式”或“登录”外壳。在您的第一个形式中,您会写出.bashrc文件,然后exec node,而那里的任何内容都没有重新阅读互联网。

您在问题中提到您使用env命令来检查环境。如果这是通过docker exec的,它将在容器内启动一个新过程,但它不是入口点脚本的孩子,因此任何发生的设置都不会可见docker exec。这通常不是问题。

我可以建议一些清理工作,这可能会使看到的影响更容易。最大的是将节点从入口点脚本拆分。如果您同时具有entrypointcmd,则 docker将cmd作为参数传递给entry> entrypoint ;如果将入口点脚本更改为exec“ $@”结束,则它将运行任何传递的内容。

#!/bin/sh
# (trying to avoid bash-specific constructs)

# Read the environment file
ENV_FILE="/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env"
if [ -f "$ENV_FILE" ]; then
  . $ENV_FILE
fi

# Run the main container command
exec "$@"

然后在Dockerfile中,将Node调用作为主命令,

ENTRYPOINT ["./entrypoint.sh"] # must be JSON-array syntax
CMD ["node", "."]              # could be shell-command syntax

重要的是,很容易覆盖命令,但请将入口点完整。因此,如果运行

docker run --rm your-image env

将启动临时容器,但是将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 then exec 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 via docker 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 to docker 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 an ENTRYPOINT and a CMD then Docker passes the CMD as arguments to the ENTRYPOINT; if you change the entrypoint script to end with exec "$@" then it will run whatever it got passed.

#!/bin/sh
# (trying to avoid bash-specific constructs)

# Read the environment file
ENV_FILE="/usr/local/etc/${SERVICE_NAME}/${SERVICE_NAME}.env"
if [ -f "$ENV_FILE" ]; then
  . $ENV_FILE
fi

# Run the main container command
exec "$@"

And then in the Dockerfile, put the node invocation as the main command

ENTRYPOINT ["./entrypoint.sh"] # must be JSON-array syntax
CMD ["node", "."]              # could be shell-command syntax

The important thing with this is that it's easy to override the command but leave the entrypoint intact. So if you run

docker run --rm your-image env

that will launch a temporary container, but passing env as the command instead of node .. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文