簡體   English   中英

容器啟動后在 docker 中運行多個命令

[英]run multiple commands in docker after container start

如何在容器啟動后運行 /bin/run1.sh 和 /bin/run2.sh 。 如果您能告訴我如何將 /bin/run1.sh 和 /bin/run2.sh 的日志發送到容器日志!

Docker文件

FROM ruby:2.5
COPY run1.sh /bin
COPY run2.sh /bin
RUN chmod +x /bin/run1.sh
RUN chmod +x /bin/run2.sh
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]

入口點.sh

#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

運行1.sh

#!/bin/sh
echo `date` $@ >> /log.txt;
cat log.txt;

運行2.sh

#!/bin/sh
echo `date` $@ >> /log2.txt;
cat log.txt;

您可以將ENTRYPOINT ["entrypoint.sh"]更改為ENTRYPOINT ["entrypoint.sh", "run1.sh", "run2.sh"]這將運行您的自定義 bash 腳本

如果log.txt文件不存在,您還需要創建它並添加exec "$@"以恢復容器的主進程

所以你的runX.sh文件應該是這樣的

#!/bin/sh
touch log.txt;
echo `date` $@ >> log.txt;
cat log.txt;
exec "$@"

note that if you are running DB migration on rails they should be logged in your application log which can be accessed through the docker container itself (get the docker id from docker ps then access the bash docker exec -it /bin/bash where you can導航到/logs/{your environment}.log

暫無
暫無

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

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