如何在Dockerfile中告诉我input“docker run”命令时要执行的命令?

这是我的dockerfile的开始:

FROM ubuntu:latest RUN apt-get update RUN apt-get install node RUN apt-get install npm RUN apt-get install mongo RUN mkdir app COPY . app/ WORKDIR app 

当我inputdocker run <image name> ,我的容器必须运行node index.js我必须做什么呢?

Janez的评论将工作,但是,如果你的容器必须以'节点index.js'开始,你将要使用ENTRYPOINT:

 FROM ubuntu:latest RUN apt-get update RUN apt-get install node RUN apt-get install npm RUN apt-get install mongo RUN mkdir app COPY . app/ WORKDIR app ENTRYPOINT ["node", "index.js"] 

如果你想了解更多关于两者之间的区别,或者如何将它们结合起来,那么在Stackoverflow中已经有了关于ENTYRYPOINT和CMD的广泛的讨论。

 ENTRYPOINT ["node"] CMD ["index.js"] 

与此相反,你可以调用一个替代的JS文件即容器

 docker run -it <image-name> test.js 

现在容器会启动并运行node test.js