简体   繁体   English

是否可以在 docker 容器中运行 shell 脚本,然后运行 ​​node app.js

[英]is it possible to run shell script and then node app.js inside docker container

i have to run some shell script which creates some certificates before the node app.js command is called in Dockerfile .Dockerfile 中调用node app.js命令之前,我必须运行一些 shell 脚本来创建一些证书。 how can i do it so it won't exit after running a shell script and will continue to run the node app.js web app.我该怎么做才能让它在运行 shell 脚本后不会退出并继续运行node app.js web 应用程序。

What I have done in the past is have my entrypoint defined in the Dockerfile be a shell script and then run the node command from within that script.我过去所做的是让我在 Dockerfile 中定义的入口点是一个 shell 脚本,然后从该脚本中运行node命令。

So in my Dockerfile I have this -所以在我的 Dockerfile 我有这个 -

ENTRYPOINT ["./docker-start.sh"]

And my docker-start.sh script contains this:我的docker-start.sh脚本包含以下内容:

#! /bin/bash

# Initialization logic can go here

echo "Starting node..."
node start.js $* # The $* allows me to pass command line arguments that were passed to the docker run command. 

# Cleanup logic can go here

Do you want your certificates to be stored in the image, so the certs are the same for every container you run from the image?您是否希望将证书存储在映像中,以便从映像运行的每个容器的证书都相同? If so then you can just do that in the Dockerfile:如果是这样,那么您可以在 Dockerfile 中执行此操作:

COPY create-certs.sh /create-certs.sh
RUN /create-certs.sh
CMD ["node", "etc."]

When you docker build , the output of the RUN instruction will be saved in the image, so your certs are permanently stored and you don't need to create them when the container starts.当您docker buildRUN指令的输出将保存在镜像中,因此您的证书将被永久存储,您无需在容器启动时创建它们。

If you need to create certs on demand for each container, then you can use a bootstrap shell script which does multiple steps - check if the certs exist, create them if needed, then start the Node app.如果您需要为每个容器按需创建证书,那么您可以使用执行多个步骤的引导 shell 脚本 - 检查证书是否存在,如果需要创建它们,然后启动 Node 应用程序。 The bootstrap shell script is what you use for your CMD . bootstrap shell 脚本是您用于CMD脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM