从 sh 传递变量以期望不使用 docker-compose

Passing variables from sh to expect not working with docker-compose

Dockerfile 包含:

ENV VAR 1
COPY ./setup.exp /tmp/
RUN chmod a+x /tmp/setup.exp

预期文件:

#!/usr/bin/expect
set timeout -1
spawn setup -v
expect "Enter variable: "
send -- "$env(VAR)\r"

shell 文件 (main.sh):

#!/bin/sh
/tmp/setup.exp $VAR

当我从容器内的 shell 运行 ./main.sh 时,它工作得很好。

然而,当我 运行 docker-compose upentrypoint: ./main.sh 时,它会打印出这个错误:

send: spawn id exp4 not open
    while executing
"send -- "$env(VAR)\r""
    (file "/tmp/setup.exp" line 5)

如果我直接将变量作为 entrypoint: /tmp/setup.exp ${VAR} 传递,它会打印此警告:

WARNING: The VAR variable is not set. Defaulting to a blank string.

我也尝试了 set VAR [lindex $argv 0]; 然后 send -- "$VAR\r" 没有任何成功。

脚本似乎可以从容器内部加载 docker 的 env 变量。

有什么建议吗?

感谢@glennjackman 指出这一点。我 运行 expect -d 以获得更详细的输出。

事实证明程序退出的原因是因为 python 引发了这个异常:

ValueError: invalid width 0 (must be > 0)

在 Dockerfile 中设置变量 say ENV COLUMNS 100 解决了我的问题。