简体   繁体   English

在 Docker 容器中添加交互式用户输入,例如“读取”

[英]Adding interactive user input e.g., `read` in a Docker container

I want to make a Docker image that can perform the following:我想制作一个可以执行以下操作的 Docker 镜像:

  1. Get user input and store it in a local variable using read使用read获取用户输入并将其存储在局部变量中
  2. Utilize that variable for a later command将该变量用于以后的命令

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.

相关问题 Linux程序(例如bash或python脚本)如何知道它是如何启动的:从命令行还是交互式GUI? - How can Linux program, e.g. bash or python script, know how it was started: from command line or interactive GUI? Linux - 更快地读取或收集文件内容(例如每秒 cpu 温度) - Linux - read or collect file content faster (e.g. cpu temp every sec.) 提供凭据作为命令行 arguments 安全吗? (例如 wget --user --password) - Is providing credentials as command line arguments safe? (e.g. wget --user --password) 如何在 Linux 上获得总体 RAM 使用率(例如 57%) - How to get overall RAM usage (e.g. 57%) on Linux 如何计算好CPU百分比,例如在顶部? - How is nice cpu percentage calculated, e.g. in top? 从git导入代码到Linux ide,例如kdevelop - Import code from git to Linux ide e.g. kdevelop Bash:检查是否给出了参数(例如,是否有参数“-a”?) - Bash: Check if argument is given (e.g. is there the argument "-a" ?) 使用 Golang 在交互式 shell 中访问 docker 容器 - Access docker container in interactive shell using Golang 如何搜索 Linux 手册页(例如使用 grep) - How to search Linux man pages (e.g. with grep) 拆分字符串(例如使用bash)但跳过部分字符串 - split string (e.g. with bash) but skip part of it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM