我有一个 C 程序在 Docker 容器中运行并调用函数 getkey() 如下:
int getkey() {
int ch;
struct termios orig_term_attr, new_term_attr;
/* set terminal to raw mode */
tcgetattr(fileno(stdin), &orig_term_attr);
memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
new_term_attr.c_lflag &= ~(ECHO|ICANON);
new_term_attr.c_cc[VTIME] = 0;
new_term_attr.c_cc[VMIN] = 0;
tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
ch = fgetc(stdin); /* read one char from stdin stream without blocking. Returns EOF (-1) if none available */
tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr); /* restore original terminal attributes */
return ch;
}
在主机或 VM 终端窗口中这工作正常,但在容器内 fgetc(stdin) 输入被忽略。忽略我的意思是,如果我按下键,它们会在程序完成后显示在容器命令行中。 docker run命令行为:
docker run -it keybd_test /bin/bash
我尝试过使用 --init 和 -a stdin -a stdout -a stderr。我怀疑这与暂时将终端置于原始模式有关,但在搜索中我能找到的最接近的是ctrl-C handling
我在 docker run 命令中遗漏了什么吗?容器内的终端原始模式是否存在已知问题?
Docker --version 显示 20.10.17,build 100c70180f