使用Java应用程序和Web服务器运行Docker容器不起作用

我是一个新的docker,我想创build一个容器运行多个服务,使用此文档: 在一个容器中运行多个服务

我已经设法在容器上安装Java和Nodejs,最终导致在Dockerfile的末尾运行这个脚本作为入口点:

#!/bin/bash # Start the first process /tmp/cliffer/bin/startup.sh & status=$? if [ $status -ne 0 ]; then echo "Failed to start my_first_process: $status" exit $status fi # Start the second process npm start & status=$? if [ $status -ne 0 ]; then echo "Failed to start my_second_process: $status" exit $status fi # Naive check runs checks once a minute to see if either of the processes exited. # This illustrates part of the heavy lifting you need to do if you want to run # more than one service in a container. The container will exit with an error # if it detects that either of the processes has exited. # Otherwise it will loop forever, waking up every 60 seconds while /bin/true; do PROCESS_1_STATUS=$(ps aux |grep -q my_first_process |grep -v grep) PROCESS_2_STATUS=$(ps aux |grep -q my_second_process | grep -v grep) # If the greps above find anything, they will exit with 0 status # If they are not both 0, then something is wrong if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then echo "One of the processes has already exited." exit -1 fi sleep 60 done 

这两个服务都在后台运行,结果是npm启动,启动一个web服务器,但立即closures。 这是从npm start &

 [--:--:--][CONSOLE] [09:19:11] [Start] Listening at port 3000 [09:19:11] [Stop] Shutting down 

当我将容器与每个服务分开运行在一个容器中时,它是完美的。

任何想法为什么?

Docker需要一个进程在前台运行,否则会退出。 您的前台程序是睡眠60秒的入口点,并检查这两个进程是否仍在后台。

该检查是针对进程名称“my_first_process”。 这应该是为你的实例像“java”

所以而不是

 ps aux |grep -q my_first_process |grep -v grep ps aux |grep -q my_second_process |grep -v grep 

尝试

 ps aux |grep -q java |grep -v grep ps aux |grep -q npm |grep -v grep 

Joel的评论仍然有效,为您的用例运行两个不同的Docker容器更有意义。