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

时间:2017-06-21 09:42:03

标签: java node.js docker

我是docker的新手,我正在尝试创建一个运行多个服务的容器, 使用此文档:Run multiple services in a container

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



#!/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服务器但立即关闭它。 这是来自npm start &

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

当我在每个容器中分别运行容器并将其自身运行时,它完美无缺。

任何想法为什么?

1 个答案:

答案 0 :(得分: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

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