2017-06-21 115 views
0

我是个新的码头工人,容器泊坞窗,我想创建一个容器中运行多个服务, 使用该文档:Run multiple services in a container与运行Java应用程序和Web服务器不工作

我已经成功地得到Java和安装在containe的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 start,启动一个web服务器,但立即关闭。 这是输出IM从npm start &

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

让我单独在自己的容器它的作品完美运行与每个服务的容器。

有什么想法为什么?

+0

不回答你的问题,但值得一提的是,一般建议每个容器运行1个服务,如果你想要几个服务,你可以使用docker-compose。话虽如此,当你说他们单独工作时,它是否与ENTRYPOINT指向一个bash脚本?或者直接指向“npm start”? – Joel

回答

0

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 

乔尔的评论仍然有效,更有意义运行两个不同的码头工人集装箱为你的使用情况。

+0

第一个服务监视进入网络服务器的数据包,这就是为什么运行它在2可能不会工作.. –

+0

你可以监视内部码头网络,链接码头容器或使用一个共同的卷,但如果你的两个进程明确属于一起,你有理由将它们放在一个容器中。 – sleepyhead

相关问题