Docker自定义Entrypoint调用MySQL原入口后容器立即退出问题
解决mysql/mysql-server自定义Entrypoint脚本导致容器退出的问题
问题核心是自定义脚本调用原entrypoint的方式不对:直接执行/entrypoint.sh时,原entrypoint会作为bash脚本的子进程运行,容器的PID1是bash脚本。当原entrypoint完成启动mysqld的操作后,bash脚本因无后续命令退出,进而导致容器停止。
修复后的自定义脚本
修改custom-entrypoint.sh,用exec调用原entrypoint并传递所有参数:
#!/bin/bash # 这里添加你的自定义逻辑 echo "执行自定义前置操作..." # 用exec替换当前bash进程为原entrypoint,确保mysqld成为PID1 exec /entrypoint.sh "$@"
为什么这样有效?
exec命令会用指定程序替换当前shell进程,而非创建子进程。原entrypoint启动的mysqld会成为容器的PID1进程,容器会持续运行直到mysqld停止。"$@"会传递docker-compose或docker run命令中传入的所有参数给原entrypoint,保证原entrypoint能正常处理启动参数。
docker-compose配置注意事项
确保custom-entrypoint.sh已挂载到容器内且拥有可执行权限:
services: mysql: image: mysql/mysql-server entrypoint: ["/custom-entrypoint.sh"] volumes: - ./custom-entrypoint.sh:/custom-entrypoint.sh:ro # 其他配置如环境变量、端口映射等
首次使用前给脚本添加可执行权限:
chmod +x custom-entrypoint.sh
内容的提问来源于stack exchange,提问作者Pichler
相关产品推荐
相关产品推荐

