如何 docker 专门在 Ubuntu docker image/container 上执行 docker 容器的 shell 内置函数

How to docker exec a shell builtin of docker container specifically on Ubuntu docker image/container

感谢您阅读我的 post。

问题:

# docker ps
CONTAINER ID        IMAGE                  COMMAND             
35c8b832403a        ubuntu1604:1   "sh -c /bin/sh"    

# docker exec -i -t 35c8b832403a type type
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:262: starting container process caused "exec: \"type\": executable file not found in $PATH"

# Dockerfile
FROM ubuntu:16.04

ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN apt-get update && apt-get -y upgrade
ENTRYPOINT ["sh", "-c"]
CMD ["/bin/bash"]

描述:

我的 objective 是让 "type" shell builtin 以如下方式执行 docker exec

docker exec -i -t 35c8b832403a 类型类型(失败)

docker exec -i -t 35c8b832403a sh -c "type type"(通过)

我用谷歌搜索,在容器中做了一些修改(更改 /etc/profile、/etc/environment、bashrc)但失败了。

根据 docker 文档本身,它声明:

COMMAND will run in the default directory of the container. It the underlying image has a custom directory specified with the WORKDIR directive in its Dockerfile, this will be used instead.

COMMAND should be an executable, a chained or a quoted command will not work. Example: docker exec -ti my_container "echo a && echo b" will not work, but docker exec -ti my_container sh -c "echo a && echo b" will.

但是当我能够从 DOCKER FEDORA (Dockerfile: FROM fedora:25)

# docker ps
CONTAINER ID        IMAGE                  COMMAND             
2a17b2338518        fedora25:1   "sh -c /bin/sh" 

# docker exec -i -t 2a17b2338518 type type
type is a shell builtin

问题:

有没有办法在 Ubuntu docker 上启用此功能? Image/Container 微调?流浪文件配置?请帮忙。

其他:

使用 docker 运行,由于 Dockerfile 中的 "ENTRYPOINT",我能够获得正确的输出。然而图像需要保存而不是导出

如果你运行 docker execCMD和ENTRYPOINT是没有意义的。其余参数作为命令在已经存在的容器中执行。

也许您想使用 docker run

以防万一,为了能够按预期执行类型,它需要成为路径的一部分。作为 shell 内置函数无济于事,因为正如您所说,您不想执行 /bin/bash -c 'type type'

如果你想让type作为内置shell命令执行,这意味着你需要执行shell/bin/bash/bin/sh和然后在其上执行 'type type',使其成为 /bin/bash -c 'type type'

毕竟,正如@Henry 所说,docker exec 是将要执行的完整命令,上面没有 CMDENTRYPOINT 的位置。