简体   繁体   中英

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:

# 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:

#!/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. The npm start command from the wrapper-script does not seem to work. 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?

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.

Instead try to put a & after the python command to run it in background:

python3 -m launcher.py &

Try changing your Dockerfile to this:

# 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:

#!/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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