首页 > 其他分享 >docker-entrypoint.sh 文件的用处

docker-entrypoint.sh 文件的用处

时间:2022-11-21 14:55:28浏览次数:39  
标签:ip redis sh entrypoint mysql docker

参考出处
很多著名库的 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 文件路径与命名。

标签:ip,redis,sh,entrypoint,mysql,docker
From: https://www.cnblogs.com/exmyth/p/16911385.html

相关文章

  • docker常用命令
    启动一个容器dockerrun-itd-m10g--namepython3--restart=always--nethost-v/root/python_env:/root/python_envdocker.io/python:3.6.8-itd其中-d:后台启动......
  • arcgis合并多个shp文件
    1.splitbyattribute.先按照属性分成多个多边形shp2.intersect.求每个多边形shp所包含的点要素3.union还是merge?.把点要素合并。 >>4.selectbylocation.  ......
  • 基于miniconda的docker file
    FROMcontinuumio/miniconda3WORKDIR/workdir#Createtheenvironment:#COPYenvironment.yml.COPY..RUNcondaenvcreate-fenvironment.ymlRUNpwd#......
  • 生产环境实现Docker部署宝塔面板
    生产环境中,为了避免极小概率的数据丢失,我们将容器内的宝塔文件映射到宿主机的目录中(您之后安装的Nginx、MySQL等服务均会挂载到宿主机目录)。该方法是Docker部署宝塔面......
  • 解决在 Linux Deploy 中安装老版本的 Debian 无法开启 ssh 的错误
    这几天在废弃的手机和平板上尝试安装完整的Linux系统,折腾了好几天,发现很多软件对arm的支持相当不完善。源里缺很多包,自己编译也编译不过。有些软件的老版本反而是稳定的,新......
  • docker学习
    1、查看centos系统版本cat/etc/redhat-release   2、添加阿里云镜像源yum-config-manager--add-repohttp://mirrors.aliyun.com/docker-ce/linux/centos/d......
  • 虚拟机centos7上安装docker+jenkins
    虚拟机centos7上安装docker+jenkins学习某册子的CICD时,安装了docker和jenkins,记录的安装过程和中间碰到的问题。使用的虚拟机为ParallelsDesktop,配置为2核4G,系统为cento......
  • ShaderGraph之基础光照
    在ShaderGraph中如果想要自定义一个纹理输入,需要添加SampleTexture2D节点,输出到片元着色器的BaseColor节点。还需要自定义一个Texture2D属性,用于外部修改纹理。新建材......
  • The SSL connection could not be established, see inner exception.
    C#请求HTTPS地址的故障分析和TLS知识点总结  背景介绍近期收到同事反馈,在C#程序中通过HTTPClient请求一个HTTPS的地址时,在本地开发环境和测试环境均能正常执行,而......
  • Unity Shader 4 ShaderLab
    通用基础的ShaderUnlit不发光的纹理VertexLit顶点光照Diffuse漫反射Normalmapped法线贴图,增加一个或更多纹理和几个着色器结构Specular镜面高光,模拟......