簡體   English   中英

使用Java應用程序和Web服務器運行Docker容器不起作用

[英]running a Docker container with Java application and a webserver doesnt work

我是Docker的新手,我正在嘗試使用以下文檔創建運行多個服務的容器: 在容器中運行多個服務

我設法在容器中安裝了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

當我在自己的容器中分別運行包含每個服務的容器時,它可以完美地工作。

有什么想法嗎?

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容器更有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM