我必须在 docker 文件上执行两个命令,但这两个命令都附加到终端并阻止下一个命令的执行。
docker文件:
FROM sinet/nginx-node:latest
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
RUN git clone https://name:[email protected]/joaocromg/front-web-alferes.git
WORKDIR /usr/src/app/front-web-alferes
RUN npm install
RUN npm install bower -g
RUN npm install gulp -g
RUN bower install --allow-root
COPY default.conf /etc/nginx/conf.d/
RUN nginx -g 'daemon off;' & # command 1 blocking
CMD ["gulp watch-dev"] # command 2 not executed
有人知道我该如何解决这个问题吗?
尝试创建这样的脚本:
#!/bin/sh
nginx -g 'daemon off;' &
gulp watch-dev
然后在你的CMD中执行它:
CMD /bin/my-script.sh
另外,请注意你的最后一行将不起作用:
CMD ["gulp watch-dev"]
它必须是:
CMD gulp watch-dev
或:
CMD ["gulp", "watch-dev"]
另请注意,
RUN
用于执行将更改图像状态的命令(如RUN apt install curl
),而不是用于执行运行容器时需要运行的程序。来自文档:
RUN指令将在当前映像之上的新层中执行任何命令并提交结果。生成的提交映像将用于 Dockerfile 中的下一步。
我建议你在这种情况下尝试supervisord。 http://supervisord.org/
编辑:这是 httpd 和 ssh 守护进程的 docker 化示例:https://riptutorial.com/docker/example/14132/dockerfile-plus-supervisord-conf
这里的答案是
RUN nginx -g 'daemon off;'
故意在前台启动nginx,这会阻止你的第二个命令。该命令旨在启动 docker 容器并将其作为前台进程。运行 RUN nginx
将启动 nginx,创建主节点和子节点,并(希望)以零状态代码退出。尽管如上所述,这不是 run 的预期用途,因此 bash 脚本在这种情况下效果最好。
运行 npm install Bower -g && \ npm install gulp -g && \ Bower 安装 --allow-root