当前位置: > > > > Docker容器不断在终端注册Enter Key
来源:stackoverflow
2024-05-01 12:06:38
0浏览
收藏
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《Docker容器不断在终端注册Enter Key》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
问题内容
我正在使用 dockerfile 构建图像。我正在提取最新的 ubuntu 和 golang 映像。
导入所有目录并使用镜像内的 go build 构建可执行文件后,我想运行该可执行文件。因此,我尝试使用 entrypoint
或 cmd
,以便可执行文件在容器启动时运行。
问题是,当我这样做并且在附加或分离模式下运行容器时,它会一直重复注册 enter key
(cpu 使用率变得疯狂)。我可以理解这一点,因为我的脚本等待注册一个键,然后等待一些输入终止,但由于 enter 键立即再次注册,它会打印一条消息,然后再次发生相同的循环。
当我在不执行(没有 cmd
或 entrypoint
)二进制文件的情况下构建图像时,我使用 bash 终端运行容器(二进制文件仍然构建在图像内部),然后执行二进制文件,一切正常。 ,无需任何输入key
一直注册。
有什么想法为什么会发生这种情况吗???
我的 dockerfile 的简要描述:
# import images from ubuntu:18.04 from golang:1.10 # open ports export ... # copy dependencies to gopath in docker file copy github.com/user/dependencies /go/src/github.com/user/dependencies ... # set working directory and build executable workdir /go/src/github.com/user/app-folder run go build # run the binary (or not) cmd ["app_name"] -----or----- cmd ["./app_name"] -----or----- entrypoint app_name -----or----- entrypoint /go/src/github.com/user/app-folder/app_name
最后,我尝试了所有这些,一次一个,我只是像这样将它们包括在内以进行显示。结果总是一样的。终端中的结果是:
... are you sure you want to exit? y/n running. press enter to stop. are you sure you want to exit? y/n running. press enter to stop. ...
go脚本如下:
// running flag is set to true and then it scans for a newline for running { fmt.println("running. press enter to stop.") fmt.scanln() fmt.println("are you sure you want to exit? y/n") if models.confirmuseraction() { running = false close(models.dbbuffer) } }
以及包含 confirmuseraction
函数的模型包:
//ConfirmUserAction waits (blocks) for user input, returns true if input was Y/y, else false. func ConfirmUserAction() bool { var confirm string fmt.Scanln(&confirm) if confirm == "y" || confirm == "Y" { return true } return false }
解决方案
我找到了一种方法来绕过这个问题,方法是在容器内创建一个 shell 脚本,然后运行可执行文件(可能有点 hacky 方式,但 enter 键不再一直注册)。
现在,我不是在 dockerfile 上的 entrypoint
处运行可执行文件,而是在 entrypoint
处运行 shell 脚本,其中仅包含如下内容:
#! /bin/sh sleep 1; echo "Starting Metrics Server..." ./metrics_server
metrics_server 是我编译的可执行文件,我将 dockerfile 内的工作目录 workdir
设置为可执行文件和 shell 脚本。
值得一提的是,我已经在我的 dockerfile 中导入了 ubuntu 映像 (from ubuntu:18.04
),因为我无论如何都需要它。我这么说是因为没有它它可能无法工作(不完全确定,我没有尝试过)。
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持!更多关于Golang的相关知识,也可关注公众号。