如何告诉docker-compose exec从stdin读取?

时间:2017-10-03 08:17:43

标签: docker docker-compose

docker-compose exec阅读

stdin失败:

$ echo "use mydb; show measurements;" | docker-compose exec influxdb influx
Connected to http://localhost:8086 version 1.2.4
InfluxDB shell version: 1.2.4
> Traceback (most recent call last):
  File "/usr/local/bin/docker-compose", line 9, in <module>
    load_entry_point('docker-compose==1.9.0', 'console_scripts', 'docker-compose')()
  File "/usr/local/lib/python2.7/dist-packages/compose/cli/main.py", line 65, in main
    command()
  File "/usr/local/lib/python2.7/dist-packages/compose/cli/main.py", line 117, in perform_command
    handler(command, command_options)
  File "/usr/local/lib/python2.7/dist-packages/compose/cli/main.py", line 462, in exec_command
    pty.start()
  File "/usr/local/lib/python2.7/dist-packages/dockerpty/pty.py", line 338, in start
    io.set_blocking(pump, flag)
  File "/usr/local/lib/python2.7/dist-packages/dockerpty/io.py", line 32, in set_blocking
    old_flag = fcntl.fcntl(fd, fcntl.F_GETFL)
ValueError: file descriptor cannot be a negative integer (-1)

使用-T标志摆脱了问题,但它似乎禁止从标准输入读取:

$ echo "use mydb; show measurements;" | docker-compose exec -T influxdb influx

(我需要杀死该命令,因为它没有显示任何内容且无法从stdin读取)

这似乎是一个docker-compose错误,也许我应该提交这个?

$ docker-compose --version
docker-compose version 1.9.0, build 2585387

修改

相关:

1 个答案:

答案 0 :(得分:5)

https://webpack.js.org/api/module-variables/#webpack_modules-webpack-specific-中,endzyme建议使用docker命令执行此操作。我根据他的建议在我的bash配置文件中创建了一个小的bash函数:

# Like `docker-compose exec` but forwarding stdin to the container
# See https://github.com/docker/compose/issues/3352
function docin() {
  local service="$1"; shift
  docker exec -i $(docker-compose ps -q $service) $@
}

在您的情况下,您可以使用它:

echo "use mydb; show measurements;" | docin influxdb influx

这是一个很好的解决方案,直到他们在docker-compose修复此问题。