多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
* **验证容器的启动是作为Docker Daemon的子进程。** ``` // 容器没启动时,环境中docker进程只有一个 [root@iZbp1bum6107bp8mgzkeunZ ~]# ps uax | grep docker root 18808 0.0 1.4 1257996 54516 ? Ssl 22:16 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock root 21195 0.0 0.0 112812 980 pts/1 S+ 22:36 0:00 grep --color=auto docker [root@iZbp1bum6107bp8mgzkeunZ ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b8464578be3d elasticsearch:7.17.2 "/bin/tini -- /usr/l…" 6 months ago Exited (143) 20 minutes ago elasticsearch inspiring_lumiere [root@iZbp1bum6107bp8mgzkeunZ ~]# docker start b8464578be3d b8464578be3d // 容器启动之后,可以看到es进程是通过docker-entrypoint.sh fork出来的 [root@iZbp1bum6107bp8mgzkeunZ ~]# ps -ef | grep docker root 18808 1 0 22:16 ? 00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock root 21277 18808 0 22:37 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9300 -container-ip 172.17.0.2 -container-port 9300 root 21289 18808 0 22:37 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9200 -container-ip 172.17.0.2 -container-port 9200 root 21322 21303 0 22:37 ? 00:00:00 /bin/tini -- /usr/local/bin/docker-entrypoint.sh eswrapper admin 21351 21322 18 22:37 ? 00:00:40 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -XX:+ShowCodeDetailsInExceptionMessages -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j2.formatMsgNoLookups=true -Djava.locale.providers=SPI,COMPAT --add-opens=java.base/java.io=ALL-UNNAMED -XX:+UseG1GC -Djava.io.tmpdir=/tmp/elasticsearch-7059923856959665534 -XX:+HeapDumpOnOutOfMemoryError -XX:+ExitOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m -Des.cgroups.hierarchy.override=/ -Xms64m -Xmx512m -XX:MaxDirectMemorySize=268435456 -XX:G1HeapRegionSize=4m -XX:InitiatingHeapOccupancyPercent=30 -XX:G1ReservePercent=15 -Des.path.home=/usr/share/elasticsearch -Des.path.conf=/usr/share/elasticsearch/config -Des.distribution.flavor=default -Des.distribution.type=docker -Des.bundled_jdk=true -cp /usr/share/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch -Ediscovery.type=single-node root 22040 8188 0 22:40 pts/1 00:00:00 grep --color=auto docker // docker-entrypoint.sh的父进程是/usr/bin/containerd fork 出来的 [root@iZbp1bum6107bp8mgzkeunZ diff]# ps -ef | grep 21303 root 21303 1 0 22:37 ? 00:00:00 /usr/bin/containerd-shim-runc-v2 -namespace moby -id b8464578be3dcb1b260e31cf9a934232cf837fb37a6c8aa98c6eeafce8d38459 -address /run/containerd/containerd.sock root 21322 21303 0 22:37 ? 00:00:00 /bin/tini -- /usr/local/bin/docker-entrypoint.sh eswrapper root 28155 7821 0 23:34 pts/0 00:00:00 grep --color=auto 21303 ``` * **当容器启动之后,宿主机的进程中的`/usr/local/bin/docker-entrypoint.sh `是什么?** ``` // 当容器启动之后,容器进程的父进程为/usr/local/bin/docker-entrypoint.sh ,但在宿主机中却找不到 [root@iZbp1bum6107bp8mgzkeunZ ~]# ls /usr/local/bin/docker-entrypoint.sh ls: cannot access /usr/local/bin/docker-entrypoint.sh: No such file or directory [root@iZbp1bum6107bp8mgzkeunZ ~]# locate docker-entrypoint.sh // 进入容器,发现此文件的存在 [root@iZbp1bum6107bp8mgzkeunZ 4aec93483c243d1e0047a0a400ad88d2b41da647d7130911df2d6b274dd9b2f3]# docker exec -it b8464578be3d /bin/bash root@b8464578be3d:/usr/share/elasticsearch# ls /usr/local/bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh // 而在宿主机中,docker-entrypoint.sh文件存在 /var/lib/docker/overlay2/容器ID目录下 [root@iZbp1bum6107bp8mgzkeunZ ~]# locate docker-entrypoint.sh /var/lib/docker/overlay2/11d2d17336e03edf186c8fc6a896ab346a80b8430ec21b6555f681b0b31c9d4f/diff/docker-entrypoint.sh /var/lib/docker/overlay2/2c7d4c0ece6493c1d0f4a697292c530f0bb0591ef3e3932bdc5b82c7df5e0d7b/diff/usr/local/bin/docker-entrypoint.sh /var/lib/docker/overlay2/395159cde79b078f64a2dc2d63865d8883958253bdb26fac3370ebe0b5542684/diff/usr/local/bin/docker-entrypoint.sh /var/lib/docker/overlay2/f6016d6ffbf46722a0af6c29c9a7a55f5d71f469600f428a64566c659ab2ec6e/diff/usr/local/bin/docker-entrypoint.sh ```        这是因为,很多著名库的 Dockerfile 文件中,里面的ENTRYPOINT 字段是这样定义的,而ENTRYPOINT 正是容器命令。 ``` ENTRYPOINT ["docker-entrypoint.sh"] ```        比如:elasticsearch的dockerfile中的启动命令。        docker-entrypoint.sh脚本的作用在于,在启动容器前,可以通过 此脚本执行预处理逻辑,比如设置环境变量之类的操作,最后把启动容器入口正式交给dockerfile。        即,需要容器启动预处理的,都可以使用docker-entrypoint.sh机制。 ``` // https://github.com/cgswong/docker-elasticsearch/blob/main/1.7.0/Dockerfile # Start container ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] CMD [""] ```         进入容器进行验证,发现1号进程的确是`docker-entrypoint.sh`进程 ![](https://img.kancloud.cn/bd/47/bd4790c7de43cef348f15b236c0459ac_1427x133.png)