简体   繁体   English

如何在 dockerfile CMD 脚本中执行 npm start

[英]How to execute npm start within dockerfile CMD script

The following Dockerfile runs a simple script which is supposed to start a React application and a python application:下面的 Dockerfile 运行一个简单的脚本,它应该启动一个 React 应用程序和一个 python 应用程序:

# syntax=docker/dockerfile:1

FROM nikolaik/python-nodejs

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY RPDC-front-end/package.json RPDC-front-end/package.json
RUN cd RPDC-front-end && npm install

COPY . .

EXPOSE 1234

RUN chmod u+r+x wrapper-script.sh

CMD ./wrapper-script.sh

The ./wrapper-script.sh file is as follows: ./wrapper-script.sh 文件如下:

#!/bin/sh

python3 -m launcher.py
cd RPDC-front-end && npm start

I can see that both the python and node packages install within their appropriate locations, but only the python application starts.我可以看到 python 和 node 包都安装在其适当的位置,但只有 python 应用程序启动。 The npm start command from the wrapper-script does not seem to work.包装脚本中的npm start命令似乎不起作用。 However, if I open up the terminal of the running container and manually run npm start within the RDPC-front-end directory (where all of the node packages and associated code are), the application builds and is accessible via port 1234. How can I fix my script/dockerfile so that the npm start command works?但是,如果我打开正在运行的容器的终端并在 RDPC-front-end 目录(所有节点包和相关代码所在的目录)中手动运行npm start ,则应用程序将构建并可以通过端口 1234 访问。我修复了我的脚本/dockerfile 以便npm start命令起作用吗?

Although you should NOT use the same container to run more than one service, the issue here is that the call to虽然你不应该使用同一个容器来运行多个服务,但这里的问题是调用

python3 -m launcher.py

is (I think) blocking, which means that the shell will not continue to execute commands until this one exits.是(我认为)阻塞,这意味着在此命令退出之前,shell 不会继续执行命令。

Instead try to put a & after the python command to run it in background:而是尝试在 python 命令之后放置一个 & 以在后台运行它:

python3 -m launcher.py &

Try changing your Dockerfile to this:尝试将您的 Dockerfile 更改为:

# syntax=docker/dockerfile:1

FROM nikolaik/python-nodejs

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY RPDC-front-end/package.json RPDC-front-end/package.json
RUN cd RPDC-front-end && npm install

COPY . .

EXPOSE 1234

RUN chmod u+r+x wrapper-script.sh

CMD ["python3", "-m launcher.py"]

RUN cd RPDC-front-end && npm start

If you want to keep your script file, you can check why 'npm start' is failing, just change the script to this:如果你想保留你的脚本文件,你可以检查为什么 'npm start' 失败,只需将脚本更改为:

#!/bin/sh

python3 -m launcher.py
cd RPDC-front-end ; npm start 2> errorLog.txt

The error message will be stored in errorLog.txt if the command fails.如果命令失败,错误消息将存储在 errorLog.txt 中。

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

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