参考出处
很多著名库的 Dockerfile 文件中,通常都是 ENTRYPOINT 字段会是这样:

ENTRYPOINT [“docker-entrypoint.sh”]

这里我们参考分析下 MySQL 的 Dockerfile 文件,来认识下 docker-entrypoint.sh 的用处。

MySQL 8.0 Dockerfile
网址:https://github.com/docker-library/mysql/tree/223f0be1213bbd8647b841243a3114e8b34022f4/8.0

里面的 Dockerfile 、 docker-entrypoint.sh 都写了很多的 shell 代码

这里通过 1 个例子,快速的了解 docker-entrypoint.sh 的使用方法

例子:MySQL 容器自建数据库
网址:https://hub.docker.com/_/mysql/ 中,章节[ Initializing a fresh instance ] 中提到,可以在MySQL容器启动时,初始化自定义数据库:

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

原理就是如下:

Dockerfile 中定义:

ENTRYPOINT [“docker-entrypoint.sh”]
1
docker-entrypoint.sh 中在启动 mysql-server 前,创建数据库:

ls /docker-entrypoint-initdb.d/ > /dev/null
for f in /docker-entrypoint-initdb.d/*; do
process_init_file “$f” “${mysql[@]}”
done

/docker-entrypoint-initdb.d/ 中文件哪里来呢?
可以像这样:

FROM mysql:5.5
COPY db.sql /docker-entrypoint-initdb.d/
1
2
docker-entrypoint.sh 的用处
通过上述例子,可以清楚的看到,在启动容器时,可以通过 shell 脚本执行些预处理逻辑,然后通过:

exec $@
1
把启动容器入口正式交给使用者

即,需要容器启动预处理的,都可以使用 docker-entrypoint.sh 机制

再举个例子
比如本人遇到的一个项目,所以配置都在配置文件中,不走程序启动参数,也不走环境变量设置的。

那么打成 docker 镜像后,就是死配置了。

那么如何在不修改代码的情况下,达成可变配置呢。

使用 docker-entrypoint.sh 即可达成目的。

比如如下这样的 docker-entrypoint.sh :

#!/bin/bash
if [[ $redis_ip ]]; then
sed -i ‘s/redis_ip=”[0-9.]*”/redis_ip=”‘$redis_ip'”/’ config.ini
fi
if [[ $redis_port ]]; then
sed -i ‘s/redis_port=”[0-9]*”/redis_port=”‘$redis_port'”/’ config.ini
fi
echo “1” > /proc/sys/kernel/core_uses_pid
echo $CORE_PATH”/core-%e-%p-%t” > /proc/sys/kernel/core_pattern
exec “$@”

docker 启动脚本如下:

docker run -d –restart=always \
–ulimit core=-1 –privileged=true\
-e redis_ip=$REDIS_IP \
-e redis_port=$REDIS_PORT \
xxx

以上,就可以达成自定义 redis ip/port ,并在启动容器时,设置了 core 文件路径与命名。

原文地址:http://www.cnblogs.com/exmyth/p/16911385.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性