[英]Adding interactive user input e.g., `read` in a Docker container
I want to make a Docker image that can perform the following:我想制作一个可以执行以下操作的 Docker 镜像:
read
read
获取用户输入并将其存储在局部变量中Using that I have the following Dockerfile:使用它,我有以下 Dockerfile:
FROM ubuntu
RUN ["echo", "'Input something: '"]
RUN ["read", "some_var"]
RUN ["echo", "You wrote $some_var!"]
which, when running docker build
, yields the following output:在运行
docker build
,会产生以下输出:
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM ubuntu
---> 4e2eef94cd6b
Step 2/4 : RUN ["echo", "'Input something: '"]
---> Using cache
---> a9d967721ade
Step 3/4 : RUN ["read", "some_var"]
---> Running in e1c603e2d376
OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"read\": executable file not found in $PATH": unknown
read
seems to be a built-in bash "function" since which read
yields nothing. read
似乎是一个内置的 bash “函数”,因为which read
不会产生任何结果。 I replaced ["read", "some_var"]
with ["/bin/bash -c read", "some_var"]
and ["/bin/bash", "-c", "read", "some_var"]
but both yield the following:我取代
["read", "some_var"]
与["/bin/bash -c read", "some_var"]
和["/bin/bash", "-c", "read", "some_var"]
,但两者都产生以下结果:
...
Step 3/4 : RUN ["/bin/bash -c read", "some_var"]
---> Running in 6036267781a4
OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/bin/bash -c read\": stat /bin/bash -c read: no such file or directory": unknown
...
Step 3/4 : RUN ["/bin/bash", "-c", "read", "some_var"]
---> Running in 947dda3a9a6c
The command '/bin/bash -c read some_var' returned a non-zero code: 1
In addition, I also replaced it with RUN read some_var
but which yields the following:此外,我还用
RUN read some_var
替换了它,但结果如下:
...
Step 3/4 : RUN read some_var
---> Running in de0444c67386
The command '/bin/sh -c read some_var' returned a non-zero code: 1
Can anyone help me with this?谁能帮我这个?
One solution is to use an external shell script and use ENTRYPOINT .一种解决方案是使用外部 shell 脚本并使用ENTRYPOINT 。
Contents of run.sh
: run.sh
:
#!/bin/bash
echo "Input something!"
read some_var
echo "You wrote ${some_var}!"
Contents of Dockerfile
: Dockerfile
内容:
FROM ubuntu
COPY "run.sh" .
RUN ["chmod", "+x", "./run.sh"]
ENTRYPOINT [ "./run.sh" ]
This will allow ./run.sh
to run when the container is spun:这将允许
./run.sh
在容器旋转时运行:
$ docker build -t test .
Step 1/4 : FROM ubuntu
---> 4e2eef94cd6b
Step 2/4 : COPY "run.sh" .
---> 37225979730d
Step 3/4 : RUN ["chmod", "+x", "./run.sh"]
---> Running in 5f20ded00739
Removing intermediate container 5f20ded00739
---> 41174edb932c
Step 4/4 : ENTRYPOINT [ "./run.sh" ]
---> Running in bed7717c1242
Removing intermediate container bed7717c1242
---> 554da7be7972
Successfully built 554da7be7972
Successfully tagged test:latest
$ docker run -it test
Input something!
Test message
You wrote Test message!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.