【问题标题】:How to execute npm start within dockerfile CMD script如何在 dockerfile CMD 脚本中执行 npm start
【发布时间】:2021-07-16 08:21:43
【问题描述】:

以下 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

./wrapper-script.sh 文件如下:

#!/bin/sh

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

我可以看到 python 和 node 包都安装在它们适当的位置,但只有 python 应用程序启动。包装脚本中的 npm start 命令似乎不起作用。但是,如果我打开正在运行的容器的终端并在 RDPC-front-end 目录(所有节点包和相关代码所在的位置)中手动运行npm start,则应用程序将构建并可以通过端口 1234 访问。如何我可以修复我的脚本/dockerfile 以便 npm start 命令正常工作吗?

【问题讨论】:

  • 我认为您必须在 sh 文件中的 npm start 之前添加 npm build。
  • python3 -m launcher.py 可能会阻止您拨打下一行电话?尝试将其作为后台进程运行python3 -m launcher.py &
  • 你能在两个独立的容器中运行这两个独立的任务吗(一个图像FROM python 和一个FROM node)?

标签: python node.js docker npm


【解决方案1】:

虽然您不应该使用同一个容器来运行多个服务,但这里的问题是调用

python3 -m launcher.py

是(我认为)阻塞,这意味着shell不会继续执行命令,直到这个退出。

而是尝试在 python 命令之后放置一个 & 以在后台运行它:

python3 -m launcher.py &

【讨论】:

    【解决方案2】:

    尝试将您的 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
    

    如果你想保留你的脚本文件,你可以检查为什么 'npm start' 失败,只需将脚本更改为:

    #!/bin/sh
    
    python3 -m launcher.py
    cd RPDC-front-end ; npm start 2> errorLog.txt
    

    如果命令失败,错误信息将存储在errorLog.txt中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多