如何“docker exec"从头开始构建的容器? [英] How to 'docker exec' a container built from scratch?

查看:17
本文介绍了如何“docker exec"从头开始构建的容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 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,我希望了解文件是否已成功从我的主机复制到容器(我在 Dockerfile<中有一个 COPY/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).

推荐答案

如果您的暂存容器正在运行,您可以将 shell(和其他需要的实用程序)复制到其文件系统中,然后执行它.shell 需要是静态二进制文件.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 镜像错误https://github.com/GoogleContainerTools/distroless/issues/168#issuecomment-371077961

这篇关于如何“docker exec"从头开始构建的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆