我想在一个码头容器中运行M/Monit (https://mmonit.com/),并找到以下Dockerfile:https://github.com/mlebee/docker-mmonit/blob/master/Dockerfile
在我的测试环境中,我使用的是一个简单的docker-compose.yml:
version: '3'
services:
mmonit:
build: .
ports:
- "8080:8080"
#volumes:
#- ./db/:/opt/mmonit/db/它确实有效,但我希望扩展Dockerfile,以便将路径/opt/mmonit/db/作为卷导出。我正在努力实现以下行为:
/opt/mmonit/db/的卷为空时(例如,在第一次安装时),应该将安装存档中的文件写入卷。db文件夹是存档的一部分。/opt/mmonit/db/mmonit.db已经存在于卷中时,在任何情况下都不应该覆盖它。我确实知道如何在bash中编写所需的操作/检查脚本,但我甚至不确定用自定义启动脚本替换ENTRYPOINT更好,还是应该仅通过修改Dockerfile来完成。这就是为什么我要求采用推荐的方法。
发布于 2020-05-02 14:06:18
一般来说,您所制定的策略是正确的路径;它本质上就是标准的Docker数据库映像所做的。
您链接到的图像是一个社区图像,因此您不应该感到与该图像的决定有特别的联系。由于GitHub存储库中缺少任何类型的许可文件,您可能无法按原样复制它,但它也不是特别复杂。
Docker支持要运行的命令的两部分,即ENTRYPOINT和CMD。CMD很容易在Docker命令行上提供,如果两者都有,则将码头工人把他们结合在一起转换成一个命令。因此,一个非常典型的模式是将实际的命令运行(mmmonit -i)作为CMD,并让ENTRYPOINT作为包装器脚本执行所需的设置,然后是exec "$@"。
#!/bin/sh
# I am the Docker entrypoint script
# Create the database, but only if it does not already exist:
if ! test -f /opt/mmonit/db/mmonit.db; then
cp -a /opt/monnit/db_base /opt/monnit/db
fi
# Replace this script with the CMD
exec "$@"在您的Dockerfile中,您将同时指定CMD和ENTRYPOINT
# ... do all of the installation ...
# Make a backup copy of the preinstalled data
RUN cp -a db db_base
# Install the custom entrypoint script
COPY entrypoint.sh /opt/monit/bin
RUN chmod +x entrypoint.sh
# Standard runtime metadata
USER monit
EXPOSE 8080
# Important: this must use JSON-array syntax
ENTRYPOINT ["/opt/monit/bin/entrypoint.sh"]
# Can be either JSON-array or bare-string syntax
CMD /opt/monit/bin/mmonit -i我肯定会在Dockerfile中进行这样的更改,要么启动FROM,要么创建自己的社区映像。
https://stackoverflow.com/questions/61560148
复制相似问题