docker 的 hello-world 这种运行完就退出的容器,有什么方法进去看看?

讨论 未结 16 67
shijingshijing
shijingshijing 会员 2022年8月9日 10:27 发表
<p>体积这么小,纯粹比较好奇想进去看看。</p> <p>今天尝试了一下,不管是 attach 还是 exec 好像都不行。</p>
收藏(0)  分享
相关标签: 灌水交流
注意:本文归作者所有,未经作者允许,不得转载
16个回复
  • registerrr
    2022年8月9日 10:27
    你想看啥?里边有啥东西 dockerfile 不都写着了么
    0 0
  • Vegetable
    2022年8月9日 10:27
    它本身就一层一条命令,所以小。 docker run -it --rm hello-world sh
    0 0
  • Nitroethane
    2022年8月9日 10:27
    用 Go 写一个项目,然后静态编译。写 dockerfile 的时候 from 指定 scratch ,然后把编译得到的静态链接的二进制文件扔进去,这样就得到了一个非常精简的镜像
    0 0
  • Noicdi
    2022年8月9日 10:27
    我有一个想法,用 hello-world 镜像做底,写 dockerfile ,带一个不断 sleep 的脚本,然后拉起来,再进去看看
    0 0
  • shijingshijing
    2022年8月9日 12:00
    -it 不行的,自己试一下就知道了。官方的 dockerfile 里面只有一个底包,然后 CMD ["/hello"],这个 /hello 应该是编译好的二进制文件,直接执行。 其实我的初衷是已经执行了 docker run hello-world ,这时候生成了一个 dc73aabc215 的 container ,我能有什么办法再进这个 container 里面去完成一些操作,比如把这个二进制的 hello 拷贝出来,或者添加一个 shell 脚本让 hello 循环跑几次? attach 和 exec /bin/bash 好像都不行。
    0 0
  • rrfeng
    2022年8月9日 12:00
    这玩意里面根本没有 shell ,进不去的。 scratch 直接加二进制文件,整个镜像其实就只有你那个 hello-world 程序,没什么好看的。 但是可以附加一个调试容器到运行中的容器上,共享一下 ns ,就能用调试容器的工具去调试它了。 可以参考 kubectl debug ...
    0 0
  • kkocdko
    2022年8月9日 13:28
    bing 搜索 “docker shell”,第一条 stackoverflow 就是正确答案。我看了下第二条的文章更简洁: 总结就是如下命令,新建一个基于 ubuntu 的容器,进入容器的 bash: sudo docker rename $(sudo docker run --network=host -h docker -d ubuntu tail -f /dev/null) hi sudo docker exec -it hi bash
    0 0
  • kkocdko
    2022年8月9日 13:28
    哦上边已经有人提到了,scratch 没有任何东西,也没有 core-utils ,那就别折腾了呗。
    0 0
  • shijingshijing
    2022年8月9日 14:26
    结果: docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "tail": executable file not found in $PATH: unknown. 你给的那个例子里面 image 是 ubuntu ,应该能跑起来。hello-world 这个起不来,有点意思。。。 我最后还有一个思路,把 hello-world 生成的 container 重新 build 成一个 image ,然后在这个 image 基础上,加一层 fs ,跑一下 /bin/bash
    0 0
  • shijingshijing
    2022年8月9日 14:26
    #11 提到的 busybox 估计也是可以的,毕竟自带一套完整的装备。
    0 0
  • cdlnls
    2022年8月9日 14:26
    `docker history hello-world` 镜像里面只有一个可执行文件,用`docker run --rm -it hello-world sh`当然执行不了,执行 CMD 的前提是镜像里面有对应的文件。 类似 golang 打包后,只有一个可执行文件,就可以用这个可执行文件基于 scratch 制作成一个镜像。 Dockerfile: FROM scratch COPY abcd /abcd CMD abcd 这样最后的效果和 hello-world 一样。
    0 0
  • cdlnls
    2022年8月9日 14:57
    还有一种情况,当 dockerfile 里面使用 ENTRYPOINT 指定启动脚本时,再使用`docker run -it image:tag bash` 的时候,这里的 bash 会被认为是 ENTRYPOINT 的参数,这个时候如果要进入容器内,就需要:`docker run -it --entrypoint /bin/sh image:tag` 进入容器,用--entrypoint 覆盖原来 dockerfile 里面的 entrypoint 。
    0 0
  • littlewing
    2022年8月9日 14:57
    直接说你到底想干啥?只是好奇还是想实现某些功能?
    0 0