如何在 Dockerfile 中指定图像是交互式的?

How to specify in Dockerfile that the image is interactive?

我创建了一个运行命令行工具的 docker 容器。该容器应该是交互式的。我是否能够以某种方式在 Dockerfile 中指定 容器始终以交互模式启动?

作为参考,这是 docker 文件:

FROM ubuntu:latest

RUN apt-get update && apt-get -y install curl

RUN mkdir adr-tools && \
    cd adr-tools && \
    curl -L https://github.com/npryce/adr-tools/archive/2.2.0.tar.gz --output adr-tools.tar.gz && \
    tar -xvzf adr-tools.tar.gz && \
    cp */src/* /usr/bin && \
    rm -rf adr-tools

CMD ["/bin/bash"]

编辑: 我知道 run 命令的 -it 选项。我明确要求在 docker 文件中执行此操作。

编辑2: 这不是 的重复,因为我的问题解决了如何避免指定给 docker run 的参数以支持在 Dockerfile 中指定它们的问题,而假定的重复解决了在docker 自己构建图像。

您可以使用 docker run 命令。

docker build -t curly .
docker run -it curly curl https://whosebug.com

惯例是:

docker run -it IMAGE_NAME [COMMAND] [ARG...]

其中 [COMMAND]curl[ARG...]curl 参数,在我的示例中是 https://whosebug.com

-i 启用交互式进程模式。您不能在 Dockerfile.
中指定此项 -t 为容器分配伪 TTY。

您在寻找 -it 选项吗?

来自Docker documentation

For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process.

因此,例如,您可以 运行 它像:

docker run -it IMAGE_NAME [COMMAND] [ARG...]

实际上,在Ubuntu,我在后台运行 Apache Server。

但是对于你来说,尝试使用下面的命令,你应该能够进入 docker 容器。

docker exec -i -t your_container_name bash

许多 docker run 选项只能在命令行或通过更高级别的包装器(shell 脚本、Docker Compose、Kubernetes 等)指定。除了端口映射和网络设置,“interactive”和“tty”选项只能在 运行 时设置,不能在 Dockerfile.

中强制设置。