如何从头构建的容器'docker exec'? [英] How to 'docker exec' a container built from scratch?
问题描述
我正在尝试 docker exec
一个从头开始构建的容器(例如,NATS容器)。似乎很简单,但是由于它是从头开始构建的,因此我无法访问 / bin / bash
, / bin / sh
以及几乎任何此类命令。
I am trying to docker exec
a container that is built from scratch (say, a NATS container). Seems pretty straight-forward, but since it is built from scratch, I am unable to access /bin/bash
, /bin/sh
and literally any such command.
我收到错误: oci运行时错误
(未找到命令) ,找不到文件等,具体取决于我输入的命令。)
I get the error: oci runtime error
(command not found, file not found, etc. depending upon the command that I enter).
我尝试了一些命令,例如:
I tried some commands like:
docker exec -it <container name> /bin/bash
docker exec -it <container name> /bin/sh
docker exec -it <container name> ls
我的问题是,如何 docker exec
一个从头开始构建且仅包含二进制文件的容器?通过执行 docker exec
,我希望确定文件是否已成功从主机复制到容器(我有 COPY
Dockerfile
中的code>)。
My question is, how do I docker exec
a container that is built from scratch and consisting only of binaries? By doing a docker exec
, I wish to find out if the files have been successfully copied from my host to the container (I have a COPY
in the Dockerfile
).
推荐答案
如果暂存容器正在运行,您可以将外壳程序(和其他所需的utils)复制到其文件系统中,然后执行它。该外壳将需要是静态二进制文件。 Busybox是一个不错的选择,因为它可以使其他二进制文件成倍增加。
If your scratch container is running you can copy a shell (and other needed utils) into its filesystem and then exec it. The shell would need to be a static binary. Busybox is a great choice here because it can double as so many other binaries.
完整示例:
# Assumes scratch container is last launched one, else replace with container ID of
# scratch image, e.g. from `docker ps`, for example:
# scratch_container_id=401b31621b36
scratch_container_id=$(docker ps -ql)
docker run -d busybox:latest sleep 100
busybox_container_id=$(docker ps -ql)
docker cp "$busybox_container_id":/bin/busybox .
# The busybox binary will become whatever you name it (or the first arg you pass to it), for more info run:
# docker run busybox:latest /bin/busybox
# The `busybox --install` command copies the binary with different names into a directory.
docker cp ./busybox "$scratch_container_id":/busybox
docker exec -it "$scratch_container_id" /busybox sh -c '
export PATH="/busybin:$PATH"
/busybox mkdir /busybin
/busybox --install /busybin
sh'
对于Kubernetes,我认为星历容器提供或将提供等效的功能。
For Kubernetes I think Ephemeral Containers provide or will provide equivalent functionality.
参考文献:
distroless java docker image error
https://github.com/GoogleContainerTools/distroless/issues/168#issuecomment-371077961
这篇关于如何从头构建的容器'docker exec'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!