人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

docker容器內(nèi)要啟動兩個進程時Dockerfile的實現(xiàn)代碼

發(fā)布日期:2022-01-03 03:44 | 文章來源:源碼中國

近期想做一個cron定時任務的docker,在Dockerfile中做如下定義

FROM library/alpine:latest
RUN apk --update add rsync openssh bash
VOLUME ["/data"]
ADD start.sh /
CMD ["/bin/bash","/start.sh"]

在start.sh中用crontab 加載定時任務run.cron,然后啟動crond:

/usr/bin/crontab /run.cron

/usr/sbin/crond

docker build Dockerfile后,采用docker run –name xxx -d 運行容器,發(fā)現(xiàn)start.sh執(zhí)行后容器就退出了,根本無法啟動定時任務,網(wǎng)上各種辦法有說用nohup,有死循環(huán),還有說用信號,發(fā)現(xiàn)都不靠譜。

分析了一下docker的機制,一個docker容器同時只能管理一個進程,這個進程退出后,容器也就退出了。這并不意味著一個容器里只能同時運行一個進程(那樣太浪費了),只是最后一個運行的進程不能退出。

這個案例在容器啟動運行start.sh,crond的缺省設置是后臺運行,這樣導致start.sh運行結(jié)束,容器跟著start.sh退出而退出。

因此,在start.sh中,crond 應強制采用前臺運行:crond -f。

這樣start.sh就不會退出, docker run -d 運行時就可以保持容器后臺運行。

start.sh總結(jié)總結(jié):

(1)容器中運行多個守護進程時,前面的進程要用后臺方式運行(或添加 &),否則后面的服務無法啟動

(2)容器中最后一個守護進程一定要用前臺方式運行,否則start.sh退出,容器退出,所有的服務就白啟動了

FROM ubuntu:latest
RUN mkdir -p "/usr/src/pdas" \
  mkdir -p "/usr/src/pdas/reload"
COPY bin.tar /usr/src/pdas
COPY config.tar /usr/src/pdas
COPY lib.tar /usr/src/pdas
WORKDIR /usr/src/pdas
RUN tar -xvf lib.tar && \
  tar -xvf bin.tar && \
  tar -xvf config.tar
ENV LD_LIBRARY_PATH /usr/src/pdas/lib/libxml/lib:/usr/src/pdas/lib/curl/lib:$LD_LIBRARY_PATH
WORKDIR /usr/src/pdas/bin
RUN chmod +x start.sh && \
  chmod +x f_recv && \
  chmod +x f_send
VOLUME /behb/diqu
VOLUME /var/log/pdas
ENTRYPOINT ./start.sh

其中 ./start.sh腳本如下

#!/bin/bash
./f_recv &
./f_send

以上是docker鏡像啟動腳本的一點心得。

補充知識:Docker中運行多個進程時的處理

通常,Docker容器適合運行單個進程,但是很多時候我們需要在Docker容器中運行多個進程。這時有兩種不同方法來運行多進程容器:使用shell腳本或者supervisor,兩種方法都很簡單,各有優(yōu)劣,只是有一些值得注意的細節(jié)。這里只講用腳本的處理方法。

寫一個腳本multiple_thread.sh,腳本功能運行兩個python程序,將運行結(jié)果保存到log文件中。腳本內(nèi)容如下

#!/bin/bash
# Start the first process
nohup python -u /tmp/thread1.py > /tmp/thread1.log 2>&1 &
ps aux |grep thread1 |grep -q -v grep
PROCESS_1_STATUS=$?
echo "thread1 status..."
echo $PROCESS_1_STATUS
if [ $PROCESS_1_STATUS -ne 0 ]; then
echo "Failed to start my_first_process: $PROCESS_2_STATUS"
exit $PROCESS_1_STATUS
fi
sleep 5
# Start the second process
nohup python -u /tmp/thread2.py > /tmp/thread2.log 2>&1 &
ps aux |grep thread2 |grep -q -v grep
PROCESS_2_STATUS=$?
echo "thread2 status..."
echo $PROCESS_2_STATUS
if [ $PROCESS_2_STATUS -ne 0 ]; then
echo "Failed to start my_second_process: $PROCESS_2_STATUS"
exit $PROCESS_2_STATUS
fi
# 每隔60秒檢查進程是否運行
while sleep 60; do
ps aux |grep thread1 |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep thread2 |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they 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

下一步制作Dockerfile:

FROM centos:latest
 
COPY thread1.py /tmp/thread1.py
COPY thread2.py /tmp/thread2.py
COPY multiple_thread.sh /tmp/multiple_thread.sh
 
CMD bash /tmp/multiple_thread.sh

以上這篇docker容器內(nèi)要啟動兩個進程時Dockerfile的實現(xiàn)代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持本站。

版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部