简体   繁体   English

容器启动后在 docker 中运行多个命令

[英]run multiple commands in docker after container start

how can I run /bin/run1.sh and /bin/run2.sh after the container startup, also.如何在容器启动后运行 /bin/run1.sh 和 /bin/run2.sh 。 if you can tell me how can I send the logs of /bin/run1.sh and /bin/run2.sh to container logs!!如果您能告诉我如何将 /bin/run1.sh 和 /bin/run2.sh 的日志发送到容器日志!

Docker file 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"]

entrypoint.sh入口点.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 "$@"

run1.sh运行1.sh

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

run2.sh运行2.sh

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

You can change ENTRYPOINT ["entrypoint.sh"] to ENTRYPOINT ["entrypoint.sh", "run1.sh", "run2.sh"] which will run your custom bash scripts您可以将ENTRYPOINT ["entrypoint.sh"]更改为ENTRYPOINT ["entrypoint.sh", "run1.sh", "run2.sh"]这将运行您的自定义 bash 脚本

you also need to create the log.txt file if it does not exist and to add exec "$@" to resume the container's main process如果log.txt文件不存在,您还需要创建它并添加exec "$@"以恢复容器的主进程

so your runX.sh files should look like this所以你的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 navigate to /logs/{your environment}.log ) 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